x86-64 mul 指令不能正确乘法?

x86-64 mul instruction does not multiply properly?

我在 x86-64 上遇到了 MUL 指令的奇怪问题。看起来这条指令没有正确地乘以寄存器中的 64 位内容。我正在尝试制作一个简单的代码,将 2 提高到调用者定义的幂(在 RDI 中传递)。由于某种原因,RAX 中的值似乎卡在了神奇的 28 限制。我可能又在这里遗漏了一些非常明显的东西(可能误解了指令本身)。下面是我的代码和附加的输出:

twotopwr:
    ;RDI contains the power
    CALL printfcall ;this prints the good value of 10 when testing with number 10 in RDI
    MOV RAX, 2;The 2 here
    PUSH RBX
    MOV RBX, 2;Multiplier
    CMP RDI, 0;If 0 then return 1
    JE ret1

loop:
    ;CALL printfcall
    DEC RDI ;Subtract the exponent
    CMP RDI, 0 ;Check if 0
    JE ret ;if 0 then return the value in rax
    MUL RBX ;Multiply rax by 2
    ;Print the rax content
    PUSH RDI
    MOV RDI, RAX
    CALL printfcall ;Debugging print
    POP RDI

    JMP loop ;Again
ret1:
    MOV RAX, 1;Exponent was 0
ret:
    POP RBX
    ;return value in rax
    RET

产出

The int is 10
The int is 4
The int is 26
The int is 28
The int is 28
The int is 28
The int is 28
The int is 28
The int is 28
The int is 28

我的打印功能:

printfcall:;Pass in RDI

        MOV RSI, RDI
        PUSH RDI
        MOV RDI, formatStrdec
        MOV AL, 0 
        CALL printf
        POP RDI
        RET

printf 将 return 在 RAX 寄存器中打印出的字符数,即先打印此 The int is 4 后为 13,这就是为什么你得到 26在下一次迭代和那次迭代中 printf returns 14 作为写入的字符数,乘以 2 后为 28,并且这种情况一遍又一遍地进行。