Search code examples
assemblyx86type-conversionattinteger-promotion

IA32 Assembly code for type casting from signed/unsigned char to unsigned/signed int


This is from Computer Systems, a Programmer's perspective (2nd edition)

In question 3.4, the student is asked to determine the assembly instruction needed to cast from a source data type to a destination that is referred to by a pointer

This answer given at the back of the chapter states that we need to sign extend the source type (left column) to the destination type (middle column).

enter image description here

There isn't much information to pull a pattern from this question. Is it always the case that the type of extension is determined by the source type and not the destination type, as in this example?


Solution

  • The answer is in your book.

    When performing a cast that involves both a size change and a change of “signedness” in C, the operation should change the size first. (Section 2.2.6).

    -- CSAPP 2e, Practice Problem 3.4

    So these two conversions work like this:

    char --> int --> unsigned int
    unsigned char --> unsigned int --> int