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

标签 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
        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

最佳答案

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

关于assembly - 通过堆栈将参数传递给 MASM 中的过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37013536/

相关文章:

汇编说明,以查找在多核系统中启用了多少个线程

汇编语言——LEA指令可以用来加载值吗

assembly - MASM中有转义字符吗?

linux - 在自修改汇编代码中调用 mprotect 后仍然出现段错误

c - 在 x86 中使用 BIOS 中断

assembly - 无法从 MASM 中的文件读取数字

c - 需要有关基本 ASM 的帮助

c++ - MASM str 和 substr?

winapi - helloworld.exe 有时仅在命令行上给出输出

post - BIOS如何初始化DRAM?