CSE 410 20wi Midterm Guide

The midterm will be online. It will be mostly multiple choice and T/F. It will cover material up through class on Friday, 1/31, so compiling up to but not including linking.

To study, you should look at lecture slides and homeworks.

We haven't had a homework on compiling, so I suggest the following to verify you understand how the compiler translates C code to machine/assembly: use the compiler and experiment.

On klaatu, create an test C file, for instance:

int x;
int main(int argc, char *argv[]) {
      int y;
      y = x + 6;
      return y;
}

Now use the compiler to create a .asm file:

$ CtoAsm test.c

You'll get something like this:

	.text
	.comm	x,4,4
main:
	addi	sp,sp,-48
	sw	s0,44(sp)
	addi	s0,sp,48
	sw	a0,-36(s0)
	sw	a1,-40(s0)
	lui	a5,%hi(x)
	lw	a5,%lo(x)(a5)
	addi	a5,a5,6
	sw	a5,-20(s0)
	 addi 	 a5, x0, 0 # 	li	a5,0
	 addi 	 a0, a5, 0 # 	mv	a0,a5
	lw	s0,44(sp)
	addi	sp,sp,48
	 jal 	 x0,0(ra) # 	jr	ra

You should be able to understand why most of this code is here. The main exception is the lui instruction and the %hi and %lo directives. Those have to do with the linker, which we haven't yet talked about and isn't on the midterm. The idea, though, is that the compiler does not know what address global variable x will be at when the program is run, and so generates instructions and annotations that ask the linker to "fix up" this code once it figures out where x will be. If you read them just as "load x into a5" that is perfect for the midterm.

Edit the source file to try out a few C program instruction types until you feel comfortable with what is going. DO NOT try to memorize the translation from C to assembler for every C statement. You should feel like you could just create the needed assembly instruction sequence for a small bit of C.