Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 210 Semester 2 - 2014 Tutorial 10 Exercises from CH 14.

Similar presentations


Presentation on theme: "COMPSCI 210 Semester 2 - 2014 Tutorial 10 Exercises from CH 14."— Presentation transcript:

1 COMPSCI 210 Semester Tutorial 10 Exercises from CH 14

2 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++; }

3 Exercise 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

4 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) ZEROLDR 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

5 Exercise 10.2 Consider the following codes: int main() { int i; int j; // point 1 i = -8; j = 7; // point 2 print_facts(i, j); // point 5// point return 0; } void print_facts(int num1, int num2) { double the_avg; // point // point 3 the_avg = (num1+num2)/2; // point // point 4 } 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.

6 Exercise 10.2 – Solution the_avg main’s frame pointer Return address for main Return value num1 ( -8 ) num2 ( 7 ) i ( -8 ) j ( 7 ) OS’s frame pointer Return address for OS Return value i ( -8 ) j ( 7 ) OS’s frame pointer Return address for OS Return value i j OS’s frame pointer Return address for OS Return value Local variables Activation Record for Print_fact Activation Record for main Stack at Point 3: Stack at Point 2:Stack at Point 1:

7 Exercise 10.2 – Solution the_avg (-0.5) main’s frame pointer Return address for main Return value num1 ( -8 ) num2 ( 7 ) i ( -8 ) j ( 7 ) OS’s frame pointer Return address for OS Return value Stack at Point 4:

8 Exercise 10.2 – Solution i ( -8 ) j ( 7 ) OS’s frame pointer Return address for OS Return value Stack at Point 5:


Download ppt "COMPSCI 210 Semester 2 - 2014 Tutorial 10 Exercises from CH 14."

Similar presentations


Ads by Google