2

I am at the moment studying the assembly code compiled from simple c programs using objdump, but this confuses me:

 4004f1:       c7 45 fc 02 00 00 00    movl   $0x2,-0x4(%rbp)
 4004f8:       83 45 fc 05             addl   $0x5,-0x4(%rbp)

Doesn't the 'l' prefix in the instructions stand for long, for the movl it looks fine, but the addl seems to use a single byte for the operand, why is this?

Jontahan
  • 113
  • 8
  • 2
    The instruction was encoded as a [ADD r/m32, imm8](http://www.felixcloutier.com/x86/ADD.html) . Since 5 fits in a byte the assembler chose to encode it that way. The value 5 gets sign extended to 32-bits and stored in the destination. – Michael Petch Aug 27 '16 at 20:40
  • Stored = added oops. – Michael Petch Aug 27 '16 at 21:01

1 Answers1

4

Many instructions that operate on immediates can either have an 8 bit immediate or a 32 bit immediate (mov r/m32,i32 notably does not). The purpose of this design is likely to reduce code size. However, the immediate is implicitly sign-expanded to 32 bit. In the case of add, opcode 83 /0 is add with an 8 bit immediate, opcode 81 /0 is add with a 32 bit immediate. Your assembler should automatically choose the shortest encoding. You can assemble this little snippet and then disassemble the result to observe the difference:

.byte 0x83,0xc0,0x00 # addl $0,%eax with an 8 bit immediate
.byte 0x81,0xc0,0x00,0x00,0x00,0x00 # addl $0,%eax with a 32 bit immediate
fuz
  • 88,405
  • 25
  • 200
  • 352
  • @PeterCordes I usually look into [this one](http://ref.x86asm.net/geek32-abc.html#A). Less noise, more compact information. – fuz Aug 27 '16 at 21:23
  • 1
    That's good if you just care about the encoding. I'd forgotten it had columns for modified / undefined flags, though. That's one major reason for looking up instructions. But anyway, often I want to double-check the details of the Operation section (for more complicated instructions). Also, the HTML extracts from the Intel manual are good for linking to other people, who may not be familiar with how good / detailed the Intel manuals are. – Peter Cordes Aug 27 '16 at 21:26