80 likes | 193 Views
COMPSCI 210 Semester 1 - 2014. Tutorial 10 Exercises from CH 14. Exercise 10.1. Convert the following IF…ELSE statement to LC-3. Int a; Int b; Int c; If(a+1 < 0) { b= b+b ; } Else if (a+1 > 0 ){ c= c+c ; } Else { b++; c++ ; }. Exercise 10.1 - Solution.
E N D
COMPSCI 210Semester 1 - 2014 Tutorial 10 Exercises from CH 14
Exercise 10.1 Convert the following IF…ELSE statement to LC-3 Int a; Int b; Int c; If(a+1 < 0) { b=b+b; } Else if (a+1 > 0){ c=c+c; } Else { b++; c++; }
Exercise 10.1 - Solution .ORIG x3000 LDR R0, R5, #0 ADD R0, R0, #1 BRp POSITIVE BRz ZERO ;--IF (A+1<0)---------------------------------------------------- LDR R0, R5, #-1 ;LOAD B INTO R0 ADD R0, R0, R0 ;B=B+B STR R0, R5,#-1 BR FINISH
Exercise 10.1 – Solution (Continue) ;--IF (A+1>0)------------------------------------------------------ POSITIVE LDR R0, R5, #-2 ;LOAD C INTO R0 ADD R0, R0, R0 ;C=C+C STR R0, R5,#-2 BR FINISH ;--IF (A+1==0)----------------------------------------------------- ZERO LDR R0, R5, #-1 ;LOAD B INTO R0 ADD R0, R0, #1 STR R0, R5,#-1 ;B++ LDR R0, R5, #-2 ;LOAD C INTO R0 ADD R0, R0, #1 STR R0, R5,#-2 ;C++ FINISH HALT .END
Exercise 10.2 • int main(){ • inti; int j; • // point 1 • i = -8; j = 7; • // point 2 • print_facts(i, j); • // point 5 • return 0; • } • void print_facts(int num1, int num2) { • double the_avg; • // point 3 • the_avg = (num1+num2)/2; • // point 4 • } • Consider the following codes: Draw the stack for activation records at each point. Activation records in Stack at point 1, point 2 and point 3 have been drawn as examples in next slide.
Exercise 10.2 – Solution Stack at Point 1: Stack at Point 2: Stack at Point 3: j ( 7 ) j ( 7 ) j the_avg i ( -8 ) i ( -8 ) i OS’s frame pointer main’s frame pointer OS’s frame pointer OS’s frame pointer Activation Record for Print_fact Return address for OS Return address for main Return address for OS Return address for OS Return value Return value Return value num1 ( -8 ) num2 ( 7 ) Return value Local variables Activation Record for main
Exercise 10.2 – Solution Stack at Point 4: j ( 7 ) the_avg (-0.5) i ( -8 ) main’s frame pointer OS’s frame pointer Return address for main Return address for OS Return value Return value num1 ( -8 ) num2 ( 7 )
Exercise 10.2 – Solution Stack at Point 5: j ( 7 ) i ( -8 ) OS’s frame pointer Return address for OS Return value