IA32 assembly language - move char to int
In Exercise CSAPP 3.4
src_t v;
dest_t *p;
*p = (dest_t) v;
If src_t
- char
, and dest_t
- int
, answer
movsbl %al, (%edx)
( v
stored in %eax
or %ax
or %al
, p
stored in %edx
). And when src_t
- char
, a dest_t
- unsigned
, the answer
movsbl %al, (%edx)
also.
Why should we use movsbl
instead movzl
when the type is char
non-negative?
source to share
The choice of the extension command reflects the sign of the symbol type.
gcc allows you to control the signing of symbols, so you can see what conversions it will do for each feature easily enough.
Source:
unsigned char_to_int(char *s) {
return *s;
}
Default output:
movl 4(%esp), %eax
movsbl (%eax), %eax
ret
Exit with -funsigned-char
:
movl 4(%esp), %eax
movzbl (%eax), %eax
ret
Conclusion with -fsigned-char
:
movl 4(%esp), %eax
movsbl (%eax), %eax
ret
Keep in mind that this is just the output of one compiler on one platform. The default signature may be different for a different compiler or for running gcc on a different platform.
source to share