1

I have some assembly code which I'm trying to understand:

   0x0000000000401024 <+55>:    mov    $0x0,%ecx                                    
   0x0000000000401029 <+60>:    mov    $0x0,%edx                                    
   0x000000000040102e <+65>:    add    $0x1,%edx              
   0x0000000000401031 <+68>:    cltq   
   0x0000000000401033 <+70>:    mov    0x402360(,%rax,4),%eax       

But I'm having a hard time really understanding this code. I read that cltq converts a 4 byte word to a 8 byte word. Is this always for eax and rax? So essentially rax gets filled with eax taking the sign into account, and the other information in rax gets lost. Is this correct?

Furthermore, what is then the consequence of the next line of code? I think the value in %rax gets multiplied by 4 and added to 0x402360, after which the value corresponding to that address gets put in eax.

What am I thinking wrong and what right? Thanks!

Jester
  • 56,577
  • 4
  • 81
  • 125
Tijmen
  • 11
  • 4
  • Yes, it is just indexing an array of 4 byte values. – Jester Sep 17 '20 at 10:22
  • If the memory at 0x401031 cotains 0x48,0x98, it is the instruction CDQE https://www.felixcloutier.com/x86/cbw:cwde:cdqe which sign-extends EAX to RAX. I don't know why gas does not keep the mnemonic proposed by Intel and uses `cltq` instead. – vitsoft Sep 17 '20 at 10:30
  • 3
    For the record, `gas` also accepts `cdqe` but the gnu tools disassemble as `cltq`. – Jester Sep 17 '20 at 10:31
  • Found some exact duplicates with google on `site:stackoverflow.com cltq`. – Peter Cordes Sep 17 '20 at 16:16

0 Answers0