assembly - Pythagorean triple test, yasm x64 -
i'm using yasm assembler on 64-bit linux. following program prints "false", while think should print "true":
section .data 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 contains a^2 imul ebx, ebx ;ebx contains b^2 imul ecx, ecx ;ecx contains c^2 add eax, ebx ;eax contains a^2 + b^2 sub eax, ecx ;eax 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
is there problem flags? apparently "not equal" evaluating true.
your variables byte sized load 4 bytes each. use movzx
sign-extend, such as:
movzx eax, byte [a] movzx ebx, byte [b] movzx ecx, byte [c]
ps: learn use debugger.
Comments
Post a Comment