6

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

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35
  • x86-64 version with mostly the same instructions [How to determine the appropriate MOV instruction suffix based on the operands?](https://stackoverflow.com/q/57937348) – Peter Cordes Jan 09 '21 at 11:00

2 Answers2

5

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)

0

In step 4 the target is not the espregister, but the memory it points to. Thus the b is valid and means move the byte with the value -17h to the location where esp points currently to.

Devolus
  • 21,661
  • 13
  • 66
  • 113