Chapter 8 Exercises.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Ch. 8 Functions.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Chapter 8 Evaluating the Instruction Set Architecture of H1: Part 2.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
1 Writing a Good Program 8. Elementary Data Structure.
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.
1 Workin’ with Pointas An exercise in destroying your computer.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Pointers What is the data type of pointer variables?
Popping Items Off a Stack Using a Function Lesson xx
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
William Stallings Computer Organization and Architecture 6th Edition
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Pointers & Arrays.
Bill Tucker Austin Community College COSC 1315
Engineering Problem Solving with C++, Etter
Sorting Algorithms.
Pointers and Pointer-Based Strings
Pointers Psst… over there.
In this lecture Global variables Local variables System stack
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
void Pointers Lesson xx
Lecture 8 – 9 Arrays with in a class
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Variables with Memory Diagram
Random Number Generation
Array of objects.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Arrays Skill Area 315 Part A
Chapter 5 Function Basics
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
ECEG-3202 Computer Architecture and Organization
Understanding Program Address Space
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Chapter 4 Implementing Free Functions
Computer Skills2 for Scientific Colleges
Parameter Passing in Java
Array of objects.
Dynamic Memory A whole heap of fun….
CHAPTER 2 Arrays and Vectors.
Pointers and Pointer-Based Strings
Dynamic Memory A whole heap of fun….
Pointers & Arrays.
CHAPTER 2 Arrays and Vectors.
Array-Based Lists & Pointers
Chapter 9: Pointers and String
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CS 144 Advanced C++ Programming March 21 Class Meeting
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Chapter 8 Exercises

Construct a new H1 assembler instruction Exercise 1: Construct a new H1 assembler instruction with semantics: retn x ; o <= x <= 255 pc = mem[sp++]; sp = sp + x;

Create the following H1 instruction: Exercise 2: Create the following H1 instruction: Use this instruction to implement the C++ code cora ; ac = sp + ac; # cora (convert relative address) void foo () { int x = 6; int * p; p = &x cout << *p << endl; } int main() { foo();

Exercise 3: Draw a picture of H1 memory upon completion of the line labeled (A) in the following program: #include <iostream> using namespace std; int x = 100; void fr(int &z) { z = z+ 5; // A } void ft(int &y) { y = y + 1; fr(y); void main() { ft(x); cout << “x = “ << x << endl;

Prof Dos Reis suggested the following solution to this problem: Exercise 4: In the Chapter 7 quiz I asked that you write the H1 assembler code that can execute a function be calling the address of the function as it is stored in another variable. Prof Dos Reis suggested the following solution to this problem: foo: ... ; whatever code is appropriate for foo ret p: dw foo @call dw E000 ; opcode for call ;;; now we execute foo() ld p add @call ; construct “call *p” st *+1 ; place it in the line of execution exec: dw 0 ; execute it now ... ; rest of code

Exercise 4 (continued): In C++ it is possible to define a pointer variable of type “function”. For example, if is a function then the following is a variable, p, that can hold the address of this function as in void foo() {...} void (*p)(); p = &f;

Exercise 4 (continued): Draw the H1 memory configuration diagram as if this program were written in H1 and currently executing the line labeled (X). #include <iostream> using namespace std; void a() { cout << “A” << endl; } void b() { cout << “B” << endl; // (X) void foo(void (*p)()) { p(); int main () { foo(b); foo(a);

Exercise 4 (continued): Write the H1 program of the C++ program on the previous slide.

This is Problem 8.41. What is happening here? Exercise 5: This is Problem 8.41. What is happening here? #include <iostream> using namespace std; int x = 1, y = 2; void f(int &x) { x = x+ 5; cout << x << endl; } int main() { f(x+y+5); cout << x << " " << y << endl;

Exercise 6: In C and C++ arrays do not “know” their own length. So when we pass an array to a function we also need to pass the “length” of the array meaning the number of entries in the array or the maximum index value that are currently in use. Write an H1 assembler program that implements the following C++ function, creates a global array and then populates some but not all of its elements. // finds the index of the largest element in // arg1 in the index range [0,end], end >= 0. int getMaxIndex(int [] a, int end) { int temp = a[0], ndx = 0; for (int i = 1; i <= end; i++) { if (a[i] > temp) { ndx = i; temp = a[i]; } return ndx;

Title: Write