Search code examples
assemblyx86att

Chosing suffix (l-b-w) for mov instruction


I am new to assembly.I am reading computers system programmer's perspective. I don't understand how I choose suffix for mov instruction. I know each register and bit count. Suffix usage is determined by bit count (32 bit l, 16 bit w, 8 bit b). Few example is not valid for prior sentence. For example %esp is 32-bit register but for 4. step suffix b is used instead of l. Please give an explanation for using suffix.

enter image description here

questions :

enter image description here

answer : l-w-b-b-l-w-l

Source: Computer Systems: A Programmer's Perspective (CSAPP) by Bryant, O'Hallaron


Solution

  • In movb $-17,(%esp) the destination is not the register %esp but the memory location whose address is in %esp. Because of the b in movb, a single byte will be stored at that memory location. The value stored there will be -17 (which is equivalent to the unsigned byte 0xef).

    movw $-17,(%esp) and movl $-17,(%esp) would also be legal instructions and they'd do different things, storing the 2 or 4 byte values 0xffef or 0xffffffef at memory locations %esp through %esp+1 or %esp+3.

    This instruction needs the b or w or l to disambiguate the meaning, unlike your other examples, because neither $-17 nor (%esp) is a fixed-size entity. If you try mov $-17,(%esp) the assembler will complain.

    UPDATE: I just noticed question #5, push $0xFF which also seems like it could be ambiguous (pushl $0xFF and pushw $0xFF are both legal), but there is a special rule for push that assumes l whenever there is an ambiguity. 16-bit pushes are very rare (the sysv ABI keeps everything aligned on the stack in multiples of 4 bytes so you always push 32 bits for a function argument, even if it's a short or char)