Sign extension of (signed) char

In the assignment sheet it was mentioned that “All integers in TinyC are signed.” Now I need to sign extend a char in scenarios where it is assigned to an int (like int x = 'x';), but there is no dedicated instruction for this in MIPS and the trick of left and right shifting does not work because SLL is not implemented in the MipsAsmGen, only SLLV. How are we supposed to do this?

but it is:

emitInstruction(ImmediateInstruction.SLL, dest, src, imm)

Why do you need that?

E. g. if you have

char c = 20;
int x = c;

the generated MIPS code for the second line will use LB to load c from the stack into some register, which will sign-extend it automatically to fit it for the 32-bit register.

If you assign a character literal, you do so (probably) by just loading the ASCII code directly into some register with some instruction equivalent to li, which should work for an int assignment without any problems too.

Am I missing another case in which shifting is required for the assignements?

You are right, that’s what I noticed too

Remember that conversion are performed at a few places:

  • Stores
  • Loads
  • Function returns
  • Function calls

Especially for the latter two, you should be careful. Especially for function calls, where the caller should do the conversion.

Yes, conversion (or at least for this simplified compiler, the size of a type) needs to be taken into account for memory operations (load/store)

1 Like