1 / 8

LC-3 Assembly Language IF...ELSE Conversion and Stack Activation Records

This tutorial covers the conversion of IF...ELSE statements into LC-3 assembly language, specifically focusing on Exercise 10.1. Using integer variables a, b, and c, the exercise demonstrates how to handle conditional logic in LC-3, implementing three cases based on the value of (a + 1). Additionally, it provides insights into function calls and activation records using an example function `print_facts`. Stack activation records at various points illustrate how local variables and return addresses are managed during execution.

binta
Télécharger la présentation

LC-3 Assembly Language IF...ELSE Conversion and Stack Activation Records

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 210Semester 1 - 2014 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 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

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

  5. 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.

  6. 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

  7. 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 )

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

More Related