首页 > 解决方案 > 在汇编 x86 中使用 INVOKE 和 PROTO

问题描述

我正在创建一个计算 GCD 的程序。代码在主过程中运行良好,当我创建一个单独的文件来尝试使用 INVOKE 时,它不再工作,说存在堆栈溢出

TITLE GCD Calculator

INCLUDE Irvine32.inc
INCLUDE gcd.inc

.data

.code
GCD PROC,
    num1: DWORD,; how to use PROTO? mov eax, num1
    num2: DWORD; mov ebx = num2

mov eax, num1
mov ebx, num2

push ebp
mov ebp, esp
mov eax, [ebp + 12]
mov ebx, [ebp + 8]
cmp ebx, 0
je L1

mov edx,0
div ebx
mov eax, edx
push ebx ; Overflow occurs here
push eax
call GCD

L1:

pop ebp
ret 8

GCD ENDP
END

如果需要,这是我的主要功能,我提示用户输入两个数字,然后通过调用 GCD 函数计算 GCD

TITLE Project 4

INCLUDE Irvine32.inc
INCLUDE gcd.inc

.data
input1 BYTE "Enter the First Integer:", 0
input2 BYTE "Enter the Second Integer:", 0
result BYTE "The GCD of the Two Numbers is: ", 0

.code 
main PROC

mov edx, OFFSET input1
call WriteString
call Crlf
call ReadInt
push eax; pushes the first input onto stack

mov edx, OFFSET input2
call WriteString
call Crlf
call ReadInt
push eax; pushes the second input onto stack

INVOKE GCD, eax, ebx; how to use INVOKE?

mov edx, OFFSET result
call WriteString
call Crlf
call WriteDec

main ENDP
end main

标签: assemblyx86masm

解决方案


.data
array DD 5 DUP (?)
average PROTO a: PTR DWORD

.code
main PROC
INVOKE average,  ADDR array
call DumpRegs
exit
main ENDP
average PROC  a: PTR DWORD
;;;;WORKING
ret
average ENDP
END main

推荐阅读