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

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Based on Mike Feeley’s original slides; Modified by George Tsiknis Unit 11 Local Variables, Parameters and the Stack Relevant Information CPSC 213 Companion.
Programming In C++ Spring Semester 2013 Practice 1-3 Lectures Programming In C++, Lecture 3 By Umer Rana.
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
Spring Semester 2013 Lecture 5
Slides created by: Professor Ian G. Harris Efficient C Code  Your C program is not exactly what is executed  Machine code is specific to each ucontroller.
Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines.
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
Chapter 5 The LC-3.
Based on Mike Feeley’s and Tamara Munzner’s original slides; Modified by George Tsiknis Parameters and Local Variables Relevant Information CPSC 213 Companion.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Method Parameters and Overloading. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
COMPSCI 210 Semester Tutorial 6 – Revision.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Assembler Parameter Passing Exercises. Assembler Exercise 1: Convert the following C++ code to H1 assembler. int foo(int a, char ch = ‘a’) { if ch ==
S. Barua – CPSC 240 CHAPTER 5 THE LC-3 Topics Memory organization Registers Instruction set Opcodes.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Run-time Environment and Program Organization
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
COMPSCI 210 Semester Tutorial 8 – C programming language.
C Programming Chapters 11, . . .
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Computer Science 210 Computer Organization
Chapter 14 Functions.
Compsci 210 Tutorial Five.
CS 140 Lecture Notes: Virtual Memory
Compsci 210 Tutorial Four.
Chapter 17 Recursion.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 12 Variables and Operators
Yung-Hsiang Lu Purdue University
Chapter 14 Functions.
CS 140 Lecture Notes: Virtual Memory
Chapter 14 Functions.
Chapter 17 Recursion.
Computer Science 210 Computer Organization
Stack Memory 2 (also called Call Stack)
Computer Science 210 Computer Organization
Dynamic Memory A whole heap of fun….
CS 140 Lecture Notes: Virtual Memory
Computer Science 210 Computer Organization
Chapter 14 Functions.
Computer Science 210 Computer Organization
Dynamic Memory A whole heap of fun….
Chapter 17 Recursion.
When a function is called...
Dynamic Memory A whole heap of fun….
Yan Shi CS/SE 2630 Lecture Notes
Dynamic Memory And Objects
The Stack.
10/6: Lecture Topics C Brainteaser More on Procedure Call
CS 140 Lecture Notes: Virtual Memory
EECE.3170 Microprocessor Systems Design I
Chapter 14 Functions.
Chapter 17 Recursion.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

COMPSCI 210 Semester 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 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) 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

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.

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:

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:

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