Example 3- C to LC3 translation

advertisement
Examine the code segments below. These are LC3 instructions generated by a C compiler. What are the C statements that resulted in this LC3 code. From layout of memory and runtime stack, any address (R5) + offset (negative) refers to a local variable. Any address (R4)+offset (positive) refers to a global/static variable. Therefore, we have three local variables: x,y,z with offsets (from R5) of 0, ‐1, and ‐2 (i.e., addresses R5‐0, R5‐1, R5‐2). We have two global variables a,b with offsets (from R4) of 0 and 2 (i.e., addresses R4+0 and R4+2). AND R0, R0, #0 - this instruction clears register R0
ADD R0, R0, #5 - add 5 to R0, i.e., R0 now contains 5
STR R0, R5, #0 - store contents of R0 into address at
(R5)-1. This is the address of a local
variable. Therefore, this corresponds
to a C statement x=5;
AND R1, R1, #0 – Clear R1, and add 3 to R1. Thus R1=3
ADD R1, R1, #3
STR R1, R4, #0 – Store contents of R1 (=3) into address
(R4)+0 which is address of a global variable
a. Therefore, this corresponds to a=3;
ADD R2, R1, R0 - Add R0 to R1 and store in R2.
STR R2, R5, #-1 – store contents of R2 into address (R5)-1
- This is a local variable y. R1 contains
value of a and R0=5. Therefore y=a+5;
LDR R0, R5, #-1 - Load value of local variable y into R0
ADD R1, R1, #5
- Add 5 to R1, to get R1=8.
STR R1, R5, #-1 – store value in R1 into address R5-1 which
is local var y. R1 contained a,
therefore y= a+5;
LDR R0, R4, #2
STR R0, R5, #-1
- load global variable b into R0
- store R0 into local variable R5-1,
So it sets y= b;
LDR R0, R5, #0 - load x into R0
STR R0, R5, #-2
- store x into z, i.e., z=x;
Download