IA32汇编中的简单除法函数
我正在为我的班级做作业。我的 C++ 代码调用 _Divide
函数来获取 2 个值进行除法并返回到“Result”。首先,我将 -1 移至 eax 中。一旦我完成这个过程,我的 eax 就会不断返回“-1”作为值。我可能做错了什么?这是我的汇编代码:
public _Divide
.386
.model flat
.code
_Divide proc
mov eax, -1
mov ebx, 1
cdq
idiv ebx
ret
_Divide endp
end
这是我的 C++ 代码,
#include <iostream>
using namespace std;
extern "C" long Divide (long, long, long *);
void main ()
{
long Result;
long Remainder;
long Dividend;
long Divisor;
do {
cout << "Enter Dividend" << endl;
cin >> Dividend;
cout << "Enter Divisor" << endl;
cin >> Divisor;
Result = Divide (Dividend, Divisor, &Remainder);
cout << "Result is " << Result << " and Remainder is " << Remainder << endl;
} while ((Result >= 0) || (Remainder != 0));
Result = Divide (Dividend, Divisor, 0);
cout << "Result is " << Result << " and Remainder is not used" << endl;
}
谢谢。
I am working on an assignment for my class. My C++ code calls the _Divide
function to take in 2 values to divide and return into 'Result'. To start I move -1 into eax. Once I'm done with the process, my eax keeps returning '-1' as the value. What could I be doing wrong? Here's my assem code:
public _Divide
.386
.model flat
.code
_Divide proc
mov eax, -1
mov ebx, 1
cdq
idiv ebx
ret
_Divide endp
end
Here's my C++ code
#include <iostream>
using namespace std;
extern "C" long Divide (long, long, long *);
void main ()
{
long Result;
long Remainder;
long Dividend;
long Divisor;
do {
cout << "Enter Dividend" << endl;
cin >> Dividend;
cout << "Enter Divisor" << endl;
cin >> Divisor;
Result = Divide (Dividend, Divisor, &Remainder);
cout << "Result is " << Result << " and Remainder is " << Remainder << endl;
} while ((Result >= 0) || (Remainder != 0));
Result = Divide (Dividend, Divisor, 0);
cout << "Result is " << Result << " and Remainder is not used" << endl;
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码将
-1
除以1
。答案是-1
,这就是您返回的内容。让我们分解代码(我将只添加内联注释):如果您想划分传递给此函数的参数,则需要以某种方式访问它们。您没有向我们展示 C++ 函数签名,因此我无法为您提供更多详细信息。
提示:如果您正在编写 IA32 代码(看起来您正在这样做),则参数将位于堆栈上。
Your code divides
-1
by1
. The answer is-1
, so that's what you return. Let's break down the code (I'll just put inline comments):If you want to divide arguments passed to this function, you need to access them somehow. You didn't show us the C++ function signature, so I can't help you with any more details than that.
Hint: If you're writing IA32 code, which it looks like you're doing, the arguments will be on the stack.
如果您想在汇编中编写函数,您应该首先了解调用约定: http://en.wikipedia.org /wiki/X86_calling_conventions 。基本上,调用约定是调用者和函数之间关于如何传递和返回值的一组协议。
C++ 的常用约定通常是
__cdecl
或__stdcall
。两者都需要通过EAX
传递返回值。由于 EAX 始终为 -1,这就是您得到的结果。If you want to write function in assembly you should learn about calling conventions first: http://en.wikipedia.org/wiki/X86_calling_conventions . Basically calling convention is the set of agreement between caller and function on how they will pass and return values.
Usual convention for C++ is usually
__cdecl
or__stdcall
. Both require return value to be passed viaEAX
. SinceEAX
always have -1 this is what you get.