Assembly 通过堆栈将参数传递给MASM中的过程

Assembly 通过堆栈将参数传递给MASM中的过程,assembly,masm,masm32,Assembly,Masm,Masm32,我试图将3个参数传递给一个过程,添加它们,并将它们返回到MASM中的税务登记簿中。然而,结果是一个随机的巨大数字。我尝试使用C风格的调用约定,在这里我将3个变量传递给一个函数。我做错了什么?这是我的密码: INCLUDE PCMAC.INC .MODEL SMALL .586 .STACK 100h .DATA .CODE EXTRN GetDec :NEAR, PutDDec : NEAR, PutHex : NEAR Main PROC _Begin

我试图将3个参数传递给一个过程,添加它们,并将它们返回到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在他更透彻的回答中所指出的,你并不真的需要一个局部变量来求和

我做错了什么

您没有对代码进行注释,也没有使用调试器

mov [ebp-4], edi
add [ebp-4], esi
add eax, [ebp-8] ; <---- what is this ebp-8 here?
add eax, [ebp-4]
或者,如果不需要堆栈帧,则只需:

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

谢谢你的回答。当我调用“call putdec”时,结果不是加法。你知道原因吗?谢谢,我们不知道
putdec
是如何期望参数的,也许它也想在堆栈上使用参数。在调用PutDDec之前添加
mov[esp],eax
可能值得一试。PutDDec打印出eax registerWell的十进制版本,打印的是什么?
781106505
我正在跟踪
mov-eax,[esp+4]添加eax,[esp+8]添加eax,[esp+12]ret
,因为我的功能我似乎你把答案放错地方了。它属于您的另一个/后面的问题:“MASM:如何通过引用传递值”。同时,当@Jester帮助你解决问题时,接受你自己的答案也不是很好!
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