毕达哥拉斯三重测试,yasm x64

时间:2016-02-05 21:58:35

标签: assembly x86

我在64位Linux上使用yasm汇编程序。以下程序打印" False",而我认为它应打印" True":

section .data
    a   db  3
    b   db  4
    c   db  5
    succ    db  "True", 0x0A
    fail    db  "False", 0x0A

section .text
 global _start

_start:
    mov eax, [a]
    mov ebx, [b]
    mov ecx, [c]

    imul eax, eax   ;eax now contains a^2
    imul ebx, ebx   ;ebx now contains b^2
    imul ecx, ecx   ;ecx now contains c^2

    add  eax, ebx   ;eax now contains a^2 + b^2
    sub  eax, ecx   ;eax now contains (a^2 + b^2) - c^2
    cmp  eax, 0
    jne  failure

success:
    mov  eax, 1     ;write call
    mov  edi, 1     ;stdout
    mov  esi, succ
    mov  edx, 5     ;write 5 bytes
    syscall
    jmp end

failure:
    mov  eax, 1     ;write call
    mov  edi, 1     ;stdout
    mov  esi, fail
    mov  edx, 6     ;write 6 bytes
    syscall

end:
    mov  eax, 60    ;64-bit exit call
    mov  edi, 0
    syscall 

旗帜有问题吗?显然"不平等"正在评估为真。

1 个答案:

答案 0 :(得分:3)

您的变量是字节大小的,但每个加载4个字节。使用movzx进行签名扩展,例如:

movzx eax, byte [a]
movzx ebx, byte [b]
movzx ecx, byte [c]

PS:学会使用调试器。