问题
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.
questions :
answer : l-w-b-b-l-w-l
Source: Computer Systems: A Programmer's Perspective (CSAPP) by Bryant, O'Hallaron
回答1:
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
)
回答2:
In step 4 the target is not the esp
register, 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
.
来源:https://stackoverflow.com/questions/20247944/chosing-suffix-l-b-w-for-mov-instruction