簡體   English   中英

在匯編中添加浮點/雙精度數

[英]Adding floating point/double numbers in assembly

我試圖嘗試內聯匯編,我試圖在內聯匯編中添加十進制數字(不,不是整數)。 問題是,當我調用以下函數時:

inline double ADD(double num1, double num2) {
  double res;
_asm{

    push eax; push the former state of eax onto stack
    mov eax, num1;
    add eax, num2;
    mov res, eax;
    pop eax; restore the former state of eax now that we are done   
     }  return res;}

編譯器抱怨內聯匯編中的操作數大小不合適(除了push和pop指令行之外的所有匯編行)。 所以我必須更改為整數類型,例如unsigned long,然后它可以工作,但當然只支持整數類型; 小數結果四舍五入。

有沒有辦法添加允許像8.4這樣的十進制結果的匯編?

我在十年內沒有完成x87組裝,但它應該是這樣的:

fld num1   ; load num1 and push it onto the fpu stack
fld num2   ; load num2 and push it onto the fpu stack
faddp      ; pop two numbers, add them, push sum on the stack
fstp res   ; pop sum from the stack and store it in res

你可能想要的指令是ADDSD,但我不確定。

這是英特爾指令集手冊的鏈接。 http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html/

他們過去常常免費郵寄你的硬拷貝,但看起來已經不再適用了。

您需要一組不同的指令來操作浮點數。 這是一個應該有用的介紹: x86匯編:浮點

上面的答案,您必須將操作數推入FP堆棧並彈出結果是正確的。

但是,“不正確的操作數大小”錯誤的直接原因是“擴展”寄存器,“e __”(例如eax)是32位,雙精度浮點數是64位。

嘗試這個:

_asm{

movq xmm0,[num1]
addpd xmm0, [num2];
movq [res],xmm0
// sse2
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM