0

Is the cltq its own instruction, or is it an alias for something else? I couldn't find it on the x86 page. As it does:

cltq  # fill high 32 bits with 1 if current (eax) MSB is 1 else 0
# movslq %eax, %rax

Are there other 'forms' of it, so that it can work on other byte-sizes or registers or does it only work on the accumulator register? If it's always fixed to that, why would it be so common to fix it to that? My only thought is if the return value is an int in C and its common to cast that to a long, but this is merely just a random guess.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David542
  • 104,438
  • 178
  • 489
  • 842
  • My answer on [What does cltq do in assembly?](https://stackoverflow.com/a/45386217) explains the history of why x86 has redundant short-forms of sign extending within RAX. (The mnemonic is AT&T syntax for `cdqe`, which you could also have found out with a quick google, or by checking the opcode.) – Peter Cordes Sep 27 '20 at 05:24
  • @PeterCordes oh I see, it's stored there, thanks for pointing that out: https://www.felixcloutier.com/x86/cbw:cwde:cdqe – David542 Sep 27 '20 at 05:24
  • 2
    I believe 8086 originally had two such instructions, each with its own opcode: `cbw`, extending `al` into `ax`, and `cwd`, extending `ax` into `dx:ax`. These would have been useful as precursors to 8-bit and 16-bit `idiv` respectively, when your divisor is only 8/16 bits. Each begot its own family of 32- and 64-bit versions with appropriate operand size prefixes: `cbw` gave us `cwde` and `cdqe` extending `ax` into itself (`ax->eax, eax->rax`), and `cwd` led to `cdq` and `cqo`, extending `ax` into `dx` (`eax->edx:eax` and `rax->rdx:rax` respectively). – Nate Eldredge Sep 27 '20 at 05:31
  • @PeterCordes by the way: you mention 'checking the opscode' -- what's the quickest way to do that? Usually I'm in `gdb` and it shows the instruction and memory address but not the opcode, such as `0x0000000000400090 ? cltq`. – David542 Sep 27 '20 at 05:31
  • 1
    `objdump --disassemble` has a reasonable format that shows the machine code bytes along with the disassembly. – Nate Eldredge Sep 27 '20 at 05:33
  • GDB disassembly can show the machine code with `disas /r`, but IIRC that doesn't work with `layout reg` mode. As Nate says, use `objdump -drwC -Mintel`. (Intel-syntax disassembly will use Intel mnemonics, which is useful if you want to look instructions up in Intel manuals...) – Peter Cordes Sep 27 '20 at 05:37
  • 1
    For future reference, some other correspondences between AT&T and Intel mnemonics are documented [in the GAS manual](https://sourceware.org/binutils/docs/as/i386_002dMnemonics.html#i386_002dMnemonics). – Nate Eldredge Sep 27 '20 at 05:38
  • 1
    @NateEldredge What does the `c` stand for the in the `cltq`? I'm guessing the `ltq` part is long-to-quad, but what about `c` ? – David542 Sep 27 '20 at 06:59
  • 1
    I think it means `convert`, that is `cbw` means `convert (signed) byte to (signed) word`. – ecm Sep 27 '20 at 08:28

0 Answers0