当我尝试在 MASM 中通过引用传递参数时获得错误的值([程序集][x86])

问题描述 投票:0回答:1

我正在尝试学习如何在 MASM 中通过引用传递和访问值。

我编写了一个简单的程序,将使用 Irvine.inc 库显示

val1
val2

INCLUDE Irvine32.inc
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCall: DWORD

.data               ;date values
    array DWORD 100, 100, 200
    val1 DWORD 50
    val2 DWORD 6000
    charVal BYTE 'x', 0

.code               ;Code
main PROC
    
    mov eax, val1               ;this is a test to see if it works properly. 
    call WriteDec               ;writes eax in Decimal form to the console
    
    push OFFSET val1            ;push two values by reference onto the stack
    push OFFSET val2            
    call PrintNum

    INVOKE ExitProcess, 0
main ENDP

PrintNum PROC
    push ebp                    ;save ebp so we can go back to the program
    mov ebp, esp                ;esp should point to the top of the stack
    pushad                      ;why do we need to do this?
    mov eax, 0                  ;zero out eax because I though that might be the problem
    mov eax, [ebp+12]           ;this should move what is in the value of ebp+12 into eax (val1)
    call WriteDec               ;this writes what is in eax to the console
    mov eax, [ebp+8]            ;this should move what is in the value of ebp+8 into eax (val2)
    call WriteDec               ;this writes what is in eax to the console
    pop ebp                     ;get back to correct ebp section of the code
    popad                       ;needed to do this because pushad from before
    ret
PrintNum ENDP
End main

程序没有写出地址中的值。我做错了什么?

考虑到@Jester所说的话。我的代码适用于添加的

mov eax, [eax]

我现在尝试在不同的测试程序中再次执行此操作,并且无需

mov eax, [eax]
即可工作。这里有什么区别?

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCall: DWORD

.data               ;date values
    val1 DWORD 200
    val2 DWORD 100

.code               ;Code
main PROC
    push val1       ;needed to add the offset not the value
    push val2       ;needed to add the offset not the value
    call PrintVars


    INVOKE ExitProcess, 0
main ENDP

PrintVars PROC
    push ebp
    mov ebp, esp
    mov eax, [ebp+12]   ;mov address of val1 into eax
    ;mov eax, [eax]     ;use address to get actual value
    call WriteDec       ;write the dec to the console
    call Crlf           ;carage return
    mov eax, [ebp+8]    ;mov address of val2 into eax
    ;mov eax, [eax]     ;deference address to actual value
    call WriteDec       ;write the dec to console
    call Crlf           ;carage return
    pop ebp
    ret
PrintVars ENDP
End main
assembly reference x86 masm
1个回答
0
投票

错误在我的

mov eax, [ebx+12]
。仅仅遵守一行是不够的。另一种尊重必须准确无误。
mov eax, [eax]
。完成的代码如下。

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCall: DWORD

.data               ;date values
    val1 DWORD 200
    val2 DWORD 100

.code               ;Code
main PROC
    push val1       ;needed to add the offset not the value
    push val2       ;needed to add the offset not the value
    call PrintVars


    INVOKE ExitProcess, 0
main ENDP

PrintVars PROC
    push ebp
    mov ebp, esp
    mov eax, [ebp+12]   ;mov address of val1 into eax
    mov eax, [eax]     ;use address to get actual value
    call WriteDec       ;write the dec to the console
    call Crlf           ;carage return
    mov eax, [ebp+8]    ;mov address of val2 into eax
    mov eax, [eax]     ;deference address to actual value
    call WriteDec       ;write the dec to console
    call Crlf           ;carage return
    pop ebp
    ret
PrintVars ENDP
End main
© www.soinside.com 2019 - 2024. All rights reserved.