I'm reading the book "Computer Systems: A Programmer's Perspective". Right now I'm not really sure I understand when to use the different mov instructions. Here's the exercise:
Practice Problem 3.4
Assume variables v and p declared with types
src_t v;
dest_t *p;
where src_t and dest_t are data types declared with typedef. We wish to use the appropriate data movement instruction to implement the operation
*p = (dest_t) v;
where v is stored in the appropriately named portion of register %eax (i.e., %eax, %ax, or %al), while pointer p is stored in register %edx.
For the following combinations of src_t and dest_t, write a line of assembly code that does the appropriate transfer. Recall that when performing a cast that involves both a size change and a change of “signedness” in C, the operation should change the signedness first (Section 2.2.6).
I'm checking my solutions against this blog post and I'm not quite sure I understand problem 3:
src_t dest_t My Solution Blog's Solution
char unsigned movzbl %al, (%edx) movsbl %al, (%edx)
Number 3: I use movzbl while the blog's author uses movsbl. I don't understand the reasoning of movzbl vs movsbl in this case... If the char is a negative you'll end up with a wrong number either way, can anyone clarify why movsbl is the correct choice here?
charis signed or unsigned. That is implementation-defined, so both solutions are equally correct, given the matching implementation. - Jestercharis signed in this case, other problems haveunsigned charso I think the lack of it means this one is signed. - Augusto Dias Noronhacharis signed, you need to usemovsblto sign extend it. For example, if your char value is-1you want theunsigned intvalue to be0xffffffff(this is mandated by the C standard). - Jester