0

I'm trying to figure out this code's RAX's value at the end.

start:
mov $1024, %rax
mov $4096, %rbx
mov $2048, %rcx
xor %rdx, %rdx
sub %rcx, %rbx
cmp %rbx, %rax
jge loopa
jmp loopb
loopa:
cmp $4, %rdx
jg end
inc %rdx
loopb:
xchg %rax, %rbx
idiv %rbx
add %rdx, %rax
imul %rcx
jmp loopa
end:

what im doing is following the registers values, at the beginning RAX is defined as 1024, RBX as 4096, and RCX as 2048, but then the code uses XOR on RDX and later on compares it in loopa, but I don't seem to understand RDX initial value since it is not defined anywhere in the code, what am I missing?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Skity
  • 131
  • 1
  • 10

1 Answers1

1

The initial value of rdx is not important here, because xor %rdx, %rdx sets all bits of rdx to zero. It is functionally equivalent to mov $0, %rdx.

Ben Steffan
  • 1,095
  • 12
  • 16
  • Oh I see, thank you, cant choose it as best answer atm but I will. – Skity Apr 22 '17 at 14:47
  • 1
    Functionally equivalent in its effect on `RDX`, and [it is preferred because it is shorter and faster](http://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and). However, there *is* a significant difference: a `XOR` operation sets the flags, whereas a `MOV` operation does not affect the flags. Sometimes that matters, and when it does, you need `mov $0, %rdx`. – Cody Gray - on strike Apr 22 '17 at 16:32