【问题标题】:division issue with registers in inline asm内联 asm 中寄存器的除法问题
【发布时间】:2014-04-19 18:33:11
【问题描述】:

在 vs2010 下使用 inline asm 的 div 存在轻微的注册问题。

testScore 和gradeScale 是整数。

_asm
{
   mov  eax, testScore      //student's score - let's test 36
   mov  ebx, 40             //max possible score of 40

   xor  edx,edx             //prevented an integer overflow error.

   div  ebx                 //divide and multiple by 100 to get letter grade. 
                            //SHOULD BE 36/40 = .9 but 36 is in EDX instead.

   imul edx, 100            //should now be 90 in EDX, but it's at 3600.
   mov  gradeScale, edx    //move result to gradeScale
 }

36/40 应该在 EAX 中放置 0,在 EDX 中放置 0.9。然后将其乘以 100 并将其存储到gradescale。

应该很简单,但我在这里遗漏了一些东西......

感谢收看。

【问题讨论】:

  • 请问xor edx,edx是干什么用的?
  • 我收到一个整数溢出错误,我读到的another post 建议这是因为 EDX 没有初始化。我对 ASM 真的很陌生,所以我决定尝试一下,它奏效了。
  • 您应该查阅指令集参考以了解 div 的工作原理。

标签: assembly division


【解决方案1】:

EAX 和 EDX 是 整数 寄存器,因此 DIV 是一个整数除法。你不能指望像 0.9 这样的有理数。 DIV 在 EDX 中为您提供整数除法的余数。您可以使用 FPU 浮点寄存器或 - 更好 - 在 DIV 之前将 testScore 与 100 相乘:

#include <stdio.h>

int main ( void )
{
    int testScore = 36;
    int gradeScale = 0;

    _asm
    {
        mov  eax, testScore      //student's score - let's test 36
        mov  ebx, 40             //max possible score of 40

        imul eax,100

        xor  edx,edx             //prevented an integer overflow error.
        div  ebx                 // EAX = EDX:EAX / EBX Remainder: EDX

        mov  gradeScale, eax    //move result to gradeScale
    }


    printf("%i\n",gradeScale);

    return 0;
}

【讨论】:

    【解决方案2】:

    我编写汇编程序已经有一段时间了,但我认为 EDX 中的值是 36/40 的余数。该寄存器仅适用于整数,您无法获得像 0.9 这样的浮点值。

    编辑:顺便说一下 xor EDX,EDX 将寄存器设置为 0。见:XOR register,register (assembler)

    【讨论】:

    • 如果将edx归零,则div ebx使用64位数量edx:eax作为被除数。
    猜你喜欢
    • 2013-07-04
    • 2018-10-05
    • 2016-04-03
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多