通过堆栈将参数传递给 MASM 中的过程
Passing a parameter via stack to procedure in MASM
我正在尝试将 3 个参数传递给一个过程,添加它们,然后 return 它们在 MASM 的税务登记簿中。然而,结果是一个随机的巨大数字。我正在尝试使用 C 风格的调用约定,我将 3 个变量传递给一个函数。我究竟做错了什么?这是我的代码:
INCLUDE PCMAC.INC
.MODEL SMALL
.586
.STACK 100h
.DATA
.CODE
EXTRN GetDec :NEAR, PutDDec : NEAR, PutHex : NEAR
Main PROC
_Begin
push 10
push 20
push 30
call Test1
call PutDDec
add esp, 12
_Exit
Main ENDP
Test1 PROC
; *** Standard subroutine prologue ***
push ebp
mov ebp, esp
sub esp, 4
push edi
push esi
; *** Subroutine Body ***
mov eax, [ebp+8] ; parameter 1 / character
mov esi, [ebp+12] ; parameter 2 / width
mov edi, [ebp+16] ; parameter 3 / height
mov [ebp-4], edi
add [ebp-4], esi
add eax, [ebp-8]
add eax, [ebp-4]
; *** Standard subroutine epilogue ***
pop esi ; Recover register values
pop edi
mov esp, ebp ; Deallocate local variables
pop ebp ; Restore the caller’s base pointer value
ret
Test1 ENDP
End Main
在你的子程序体中,三个参数的总和可以这样写:
mov [ebp-4], edi ;Move EDI into your local var
add [ebp-4], esi ;Add ESI into your local var
add eax, [ebp-4] ;Finally, add the contents of your local var into EAX
;(note that EAX contains first param)
你的错误在[ebp-8]
。
此外,正如 Jester 在他更详尽的回答中指出的那样,您实际上不需要本地变量来求和。
What am I doing wrong?
您没有注释您的代码,也没有使用调试器。
mov [ebp-4], edi
add [ebp-4], esi
add eax, [ebp-8] ; <---- what is this ebp-8 here?
add eax, [ebp-4]
3个数相加只需要2次相加,为什么是3次?
你甚至不需要使用局部变量,你可以这样做:
push ebp
mov ebp, esp
mov eax, [ebp+8] ; parameter 1 / character
add eax, [ebp+12] ; parameter 2 / width
add eax, [ebp+16] ; parameter 3 / height
mov esp, ebp ; Deallocate local variables
pop ebp ; Restore the caller’s base pointer value
ret
或者,如果您不需要堆栈框架,则只需:
mov eax, [esp+4]
add eax, [esp+8]
add eax, [esp+12]
ret
我能够通过使用基指针的 ax 部分让程序工作,因为我推的是 2 DB,而不是 DW:
INCLUDE PCMAC.INC
.MODEL SMALL
.586
.STACK 100h
.DATA
sum DWORD ?
.CODE
EXTRN GetDec :NEAR, PutDec : NEAR, PutHex : NEAR
Main PROC
_Begin
push 10
push 20
call Test12
;and eax, 0ffffh
call PutDec
_Exit
Main ENDP
Test12 PROC
push bp
mov bp, sp
mov ax, [bp+6] ;
add ax, [bp+4] ;
pop bp
ret 4
Test12 ENDP
End Main
我正在尝试将 3 个参数传递给一个过程,添加它们,然后 return 它们在 MASM 的税务登记簿中。然而,结果是一个随机的巨大数字。我正在尝试使用 C 风格的调用约定,我将 3 个变量传递给一个函数。我究竟做错了什么?这是我的代码:
INCLUDE PCMAC.INC
.MODEL SMALL
.586
.STACK 100h
.DATA
.CODE
EXTRN GetDec :NEAR, PutDDec : NEAR, PutHex : NEAR
Main PROC
_Begin
push 10
push 20
push 30
call Test1
call PutDDec
add esp, 12
_Exit
Main ENDP
Test1 PROC
; *** Standard subroutine prologue ***
push ebp
mov ebp, esp
sub esp, 4
push edi
push esi
; *** Subroutine Body ***
mov eax, [ebp+8] ; parameter 1 / character
mov esi, [ebp+12] ; parameter 2 / width
mov edi, [ebp+16] ; parameter 3 / height
mov [ebp-4], edi
add [ebp-4], esi
add eax, [ebp-8]
add eax, [ebp-4]
; *** Standard subroutine epilogue ***
pop esi ; Recover register values
pop edi
mov esp, ebp ; Deallocate local variables
pop ebp ; Restore the caller’s base pointer value
ret
Test1 ENDP
End Main
在你的子程序体中,三个参数的总和可以这样写:
mov [ebp-4], edi ;Move EDI into your local var
add [ebp-4], esi ;Add ESI into your local var
add eax, [ebp-4] ;Finally, add the contents of your local var into EAX
;(note that EAX contains first param)
你的错误在[ebp-8]
。
此外,正如 Jester 在他更详尽的回答中指出的那样,您实际上不需要本地变量来求和。
What am I doing wrong?
您没有注释您的代码,也没有使用调试器。
mov [ebp-4], edi
add [ebp-4], esi
add eax, [ebp-8] ; <---- what is this ebp-8 here?
add eax, [ebp-4]
3个数相加只需要2次相加,为什么是3次? 你甚至不需要使用局部变量,你可以这样做:
push ebp
mov ebp, esp
mov eax, [ebp+8] ; parameter 1 / character
add eax, [ebp+12] ; parameter 2 / width
add eax, [ebp+16] ; parameter 3 / height
mov esp, ebp ; Deallocate local variables
pop ebp ; Restore the caller’s base pointer value
ret
或者,如果您不需要堆栈框架,则只需:
mov eax, [esp+4]
add eax, [esp+8]
add eax, [esp+12]
ret
我能够通过使用基指针的 ax 部分让程序工作,因为我推的是 2 DB,而不是 DW:
INCLUDE PCMAC.INC
.MODEL SMALL
.586
.STACK 100h
.DATA
sum DWORD ?
.CODE
EXTRN GetDec :NEAR, PutDec : NEAR, PutHex : NEAR
Main PROC
_Begin
push 10
push 20
call Test12
;and eax, 0ffffh
call PutDec
_Exit
Main ENDP
Test12 PROC
push bp
mov bp, sp
mov ax, [bp+6] ;
add ax, [bp+4] ;
pop bp
ret 4
Test12 ENDP
End Main