【问题标题】:How do you divide a register in assembly?你如何在汇编中划分一个寄存器?
【发布时间】:2016-09-19 04:51:35
【问题描述】:

请原谅我提出了一个愚蠢的语法问题,但我有两个变量(squares 和 horizCharsPerSquare),我正在尝试将 ecx 设置为 squares/horizCharsPerSquare。我试过了:

    mov ecx, squares/horizCharsPerSquare

    mov ecx squares
    div horizCharsPerSquare

    mov ecx, squares
    shr ecx, horizCharsPerSquare ;//(I know there are other issues with this, I was just giving it a shot

无论如何,我得到一个错误?我得到的构建错误是“预期为常数”的一切。关于我应该如何做到这一点的任何建议?

【问题讨论】:

  • 如果它们都是汇编时常量,mov ecx, squares/horizCharsPerSquare 将起作用。如果horizCharsPerSquare 是2 的幂,则右移是正确的选择。mov ecx squares 会组合,但那是not how div works。另请参阅x86 tag wiki

标签: assembly x86 divide arithmetic-expressions


【解决方案1】:

由于 squareshorizCharsPerSquare 都是变量,因此您通常会在对它们进行算术运算之前将它们移动到寄存器中。 这里只需要将第一个变量移动到寄存器,因为div 指令确实允许内存操作数:

mov eax, squares
xor edx, edx
div horizCharsPerSquare  ;Divide EDX:EAX by the dword variable
mov ecx, eax             ;Put quotient in ECX

【讨论】:

  • 谢谢!我不知道为什么我遇到这么多麻烦
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 2022-01-02
  • 2021-03-25
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多