2016-05-03 74 views
2

我正在嘗試將3個參數傳遞給過程,添加它們並將它們返回到MASM中的納稅註冊表中。然而,結果是一個隨機的數字。我正在嘗試使用C風格的調用約定,我將3個變量傳遞給一個函數。我究竟做錯了什麼?這裏是我的代碼:將參數通過堆棧傳遞給MASM中的過程

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 

回答

3

我在做什麼錯?

你沒有評論你的代碼,你沒有使用調試器。

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 
+0

謝謝您的回答。當我打電話給「打電話PutDDec」時,結果不是加法。你知道這個的原因嗎?謝謝 – user190494

+0

我們不知道'PutDDec'如何期待這個論點,也許它也想要這個論點。在調用PutDDec之前添加'mov [esp],eax'值得一試。 – Jester

+0

PutDDec打印出EAX寄存器的十進制版本 – user190494

0

在你子程序中,這三個參數的總和可以這樣進行:

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在他的更徹底的答案中指出的,你不需要一個局部變量來完成總和。

0

我能通過使用基本指針的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 
+1

我好像錯過了你的答案。它屬於你的其他/後面的問題:「MASM:如何通過引用傳遞價值」。接受你自己的答案也不是很好,同時@Jester幫你解決了問題! –

相關問題