IA32汇编中的简单除法函数

发布于 2024-09-26 08:35:34 字数 1060 浏览 3 评论 0原文

我正在为我的班级做作业。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2024-10-03 08:35:34

您的代码将 -1 除以 1。答案是 -1,这就是您返回的内容。让我们分解代码(我将只添加内联注释):

mov eax, -1      ; Put -1 in eax
mov ebx, 1       ; put  1 in ebx

cdq              ; sign extend eax -> edx:eax = -1
idiv ebx         ; divide edx:eax (-1) by ebx (1)
                 ;   result goes in eax, so now eax = -1
ret              ; return eax (-1) to caller

如果您想划分传递给此函数的参数,则需要以某种方式访问​​它们。您没有向我们展示 C++ 函数签名,因此我无法为您提供更多详细信息。

提示:如果您正在编写 IA32 代码(看起来您正在这样做),则参数将位于堆栈上。

Your code divides -1 by 1. The answer is -1, so that's what you return. Let's break down the code (I'll just put inline comments):

mov eax, -1      ; Put -1 in eax
mov ebx, 1       ; put  1 in ebx

cdq              ; sign extend eax -> edx:eax = -1
idiv ebx         ; divide edx:eax (-1) by ebx (1)
                 ;   result goes in eax, so now eax = -1
ret              ; return eax (-1) to caller

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.

贪恋 2024-10-03 08:35:34

如果您想在汇编中编写函数,您应该首先了解调用约定: 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 via EAX. Since EAX always have -1 this is what you get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文