0

Need help understanding what the values of the registers be after this code finish executing

   mov dword ptr [ebp-4], 1
   cmp dword ptr [ebp-4], 0
   jnz loc_40101C
   mov eax, [ebp-4]
   xor eax, 2
   mov [ebp-4], eax         
   jmp loc_401025
loc_40101C:
   mov ecx, [ebp-4]
   xor ecx, 3
   mov [ebp-4], ecx
loc_401025:
   mov ebx, [ebp-4]
   xor ebx, 5
   mov [ebp-4], ebx

From my current understanding, the code jumps to loc_40101C because the cmp instruction above did not return with a zero flag. So, instructions below jnz are ignored and will be skipped to loc_40101C.

Now at loc_40101C, the value 1 gets moved to ecx. ecx then gets XOR'd with 3 resulting in the value of 2. The code then finishes executing resulting in ecx having a value of 2.

My question is, would any value be assigned to eax and ebx after this code finishes executing?

  • 1
    "The code then finishes executing resulting in ecx having a value of 2." - the code goes further through `loc_401025` as there's nothing preventing it. So at least `ebx` will get set. – Paweł Łukasik Aug 02 '22 at 15:58
  • So in this case, would eax be set to anything? Since eax is generally used as an accumulator for arithmetic operations. – dingleberry Aug 02 '22 at 16:00
  • Nope. It would only be set if the `jnz loc_40101C` is not taken (not in this case). – Paweł Łukasik Aug 02 '22 at 16:02
  • 2
    Why not just assemble it and step through with a debugger? Let the computer tell you the answer. – Raymond Chen Aug 02 '22 at 16:12
  • `loc_401025` is the end of an if/else. Execution reaches it one way or another, whichever way the cmp/jne branches. Execution doesn't stop at labels (duplicate of [Code executes condition wrong?](https://stackoverflow.com/q/32872539) because that's your only misunderstanding). It will also fall off the end of this code into whatever's next. You could say execution leaves this code, which is technically the same as saying "this code finishes executing" (this time), although it doesn't mean the CPU has stopped executing entirely. There's no `_exit` system call visible. – Peter Cordes Aug 02 '22 at 19:28

0 Answers0