[英]Passing arguments from c++ to assembly in cout
我正在寫c ++和nasm匯編atm的簡單組合,並且不理解為什么結果在“ cout”的內部和外部是不同的。 也許這是一種例外,但是我想知道兩者之間的區別。感謝您的幫助。
C ++部分
#include <iostream>
#include <cstring>
using namespace std;
extern "C" unsigned int quot (unsigned int, unsigned int);
extern "C" unsigned int remainder (unsigned int, unsigned int);
int main()
{
unsigned int i=0, j=0, k=0;
cout << "Numbers 'x y'" << endl;
cin >> i >> j;
k = quot(i,j);
cout<< "Result: " <<k;
k = remainder(i,j);
cout <<" r. "<< k <<endl;
cout << "Result: "<<quot(i,j)<<" r. "<<remainder(i,j)<<endl;
return 0;
}
NASM的報價和提醒功能幾乎相同。 唯一的區別在於代碼中的注釋
section .data
section .text
global quot
quot:
; intro
push ebp
mov ebp,esp
xor edx, edx
mov eax, [ebp+8]
mov ebx,[ebp+12]
div ebx
; DIFFERENCE: in remainder we have additionaly
; mov eax, edx
mov esp,ebp
pop ebp
ret
結果對於12 5輸入,我們期望結果:2 r。 2但我們得到。
Result: 2 r. 2
Result: 2 r. 5
您必須在asm函數中保留ebx
值(請參閱http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl )。 違反調用約定可能導致各種錯誤,從細微變化到崩潰。
使用ecx
代替ebx
,或嘗試div dword ptr [ebp+12]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.