Assignment Statements
We have looked at basic assignment statements of the form:
a = 100; a = a + 1; a = b + c / d; ...In general, we know that an assignment statement of the form:
a = b + c;emits the following code:
t1 = b + c a = t1If the expression (e.g. b + c) is more complicated, we require more temporary variables remembering for now, we are not concerned about reusing temporary variable names.
I am not going to discuss every possible combination of assignment statements we might have, but I will discuss each of the following statements:
1) a = &b; 2) *p = a; 3) a[b] = c; 4) a = b++;So what might the code for each of the above statements look like given our runtime environment?
1) a = &b;
Draw a picture using basic memory that reflects this statement. Consider:
main () { int *a, b; b = 100; a = &b; } [ryand@Huskers compiler]$ pcc z.c 1 main () 2 { 3 int *a, b; 4 5 b = 100; 6 a = &b; 7 } [ryand@Huskers compiler]$ more z.q 1 27 0 0 0 0 0 0 2 22 0 3 0 0 0 0 2 26 2 1 0 0 3 2 2 26 3 2 0 0 3 3 2 26 4 3 0 0 3 1 2 23 0 0 0 0 0 0 1 21 0 0 0 0 0 1 1 28 0 0 0 0 0 0 2 0 1002) *p = a;
Draw a picture using basic memory that reflects this statement. Consider:
1 main () 2 { 3 int *p, a; 4 5 a = 100; 6 *p = a; 7 output(*p,a); 8 } [ryand@Huskers compiler]$ more quadfile 1 27 0 0 0 0 0 0 2 22 0 3 0 0 0 0 2 26 2 1 0 0 3 2 2 26 4 2 0 0 4 1 2 9 4 1 0 0 3 3 2 20 4 2 0 0 0 0 2 20 4 3 0 0 0 0 2 25 0 2 0 0 0 0 2 26 0 0 0 0 1 0 2 23 0 0 0 0 0 0 1 21 0 0 0 0 0 1 1 28 0 0 0 0 0 0 2 0 100 [ryand@Huskers compiler]$ inter a s ______________________________________________________________ TRACE: sp = 1, pc = 0, ap = 0 TRACE: quad = 27 0 0 0 0 0 0 TRACE: operand1 = 0, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint= step]: s _______________________________________________________________ R U N T I M E S T A C K 0A 100S 0 0 0 0 0 0 0 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 1, pc = 10, ap = 0 TRACE: quad = 21 0 0 0 0 0 1 TRACE: operand1 = 0, operand2 = 0, operand3 = 1 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 0A 100S 0 0 0 0 0 0 0 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 4, pc = 1, ap = 4 TRACE: quad = 22 0 3 0 0 0 0 TRACE: operand1 = 3, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 0 100 0 11 0S 0 0 0 0 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 7, pc = 2, ap = 4 TRACE: quad = 26 2 1 0 0 3 2 TRACE: operand1 = 100, operand2 = 0, operand3 = 6 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 0 100 0 11 0A 0 0 0S 0 0 TRACE: sp = 7, pc = 3, ap = 4 TRACE: quad = 26 4 2 0 0 4 1 TRACE: operand1 = 100, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 0 100 0 11 0A 0 100 0S 0 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 7, pc = 4, ap = 4 TRACE: quad = 9 4 1 0 0 3 3 TRACE: operand1 = 0, operand2 = 0, operand3 = 7 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 100 100 0 11 0A 0 100 0S 0 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 7, pc = 5, ap = 4 TRACE: quad = 20 4 2 0 0 0 0 TRACE: operand1 = 100, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 100 100 0 11 0A 0 100 100S 0 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 8, pc = 6, ap = 4 TRACE: quad = 20 4 3 0 0 0 0 TRACE: operand1 = 100, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 100 100 0 11 0A 0 100 100 100S 0 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 9, pc = 7, ap = 4 TRACE: quad = 25 0 2 0 0 0 0 TRACE: operand1 = 2, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 100 100 0 11 0A 0 100 100 100 100S _______________________________________________________________ output: 100, 100 TRACE: sp = 7, pc = 8, ap = 4 TRACE: quad = 26 0 0 0 0 1 0 TRACE: operand1 = 0, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 100 100 0 11 0A 0 100 100S 100 100 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 7, pc = 9, ap = 4 TRACE: quad = 23 0 0 0 0 0 0 TRACE: operand1 = 0, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 0 100 0 11 0A 0 100 100S 100 100 _______________________________________________________________ _______________________________________________________________ TRACE: sp = 1, pc = 11, ap = 0 TRACE: quad = 28 0 0 0 0 0 0 TRACE: operand1 = 0, operand2 = 0, operand3 = 0 TRACE: enter [s = stackdump b = breakpoint = step]: s _______________________________________________________________ R U N T I M E S T A C K 0A 100S 0 11 0 0 100 100 100 100 _______________________________________________________________ Normal Termination
© Douglas J. Ryan / ryandj@pacificu.edu