Wednesday, March 14, 2018

Size: px
Start display at page:

Download "Wednesday, March 14, 2018"

Transcription

1 Wednesday, March 14, 2018 Topics for today Arrays and Indexed Addressing Arrays as parameters of functions Multi-dimensional arrays Option A: Space-minimal solution Option B: Iliffe vectors Array bound checking Arrays: special cases Memo functions Arrays as parameters of functions We need to be able to translate functions that have arrays as parameters. This is not such a big leap because we already know how to pass scalars by reference and we can think of a scalar as being an array of size 1. When we pass an array as parameter we pass it by reference; we put its address on the stack. Separately, we typically have to pass its size (or at least the number of elements we need to process). This is what we have to do in most high-level languages also. For example, here is a C function for adding up the first N elements of an array. int sum (int V[], int N) { int j,temp = 0; for (j=0; j<n; j++) temp += V[j]; } return temp; If we have int T[320]; global array then the following are typical calls of sum total = sum(t,60); // address of array and number of elements to process all = sum(t,320); first = sum(t,1); Comp 162 Notes Page 1 of 15 March 14, 2018

2 Here is the call sum(t,60) in Pep/9 subsp subsp ldwa stwa ldwa stwa call 2,i ; for returned value 4,i ; for parameters; T,i ; address of array 0,s ; is the first parameter 60,i ; count of elements 2,s ; is the second parameter sum After sum creates space for local variables j and temp, the stack looks like j temp Return Address Address of T For return value To access the elements of the array parameter we combine two ideas we have already seen deferred addressing (we saw this when looking at "pass by reference") indexing (we saw this when accessing global and local arrays). The body of our function is for (j=0; j<n; j++) temp += V[j]; The assignment to temp translates to ldwx 0,s ; the local variable j in index register ldwa 2,s ; temp in Reg. A aslx ; index converted to byte offset adda 6,sfx ; add V[j] stwa 2,s ; update temp Comp 162 Notes Page 2 of 15 March 14, 2018

3 The new mode (,sfx) is stack-deferred indexed. The operand is memory[ memory [operand + SP] + register X ] It is used when the stack contains a pointer to an array. The operand indicates how far down the stack the pointer to the array is. The contents of Register X are used (as usual) to index within the array. Thus in the case of adda 6,sfx We are accessing the array that is pointed to from the word 6 bytes down from the top of the stack (below the local variables and return address) and using register X to select a particular element within the array, that is memory[ memory [6 + SP] + register X ] As far as our function sum is concerned it makes no difference whether the array being passed is global or local as long as the stack contains a pointer to the place where the array is stored. In the former case the array is stored in the global area in the latter case it is stored on the stack but in each case we are just passing an address. More on arrays as parameters (1) Local array Here is an example of a call where the parameter is a local array void A() { int numbers[10]; int X,Y,Z; } X = sum(numbers,10); The only difference between this and the previous example is the calling environment and how the pointer to the array is figured out. We use the movspa instruction just as we did when passing local scalars by reference. Here is this call of sum in Pep/9 movspa ; save address of top of stack subsp 2,i ; for returned value subsp 4,i ; for parameters adda 6,i ; address of array numbers is 6 greater than old top of ; stack i.e. beyond space for X, Y and Z stwa 0,s ; array parameter ldwa 10,i stwa 2,s ; size parameter call sum Comp 162 Notes Page 3 of 15 March 14, 2018

4 Pictorially: Pointer to numbers (2 bytes) 10 (2 bytes) 00 0A Space for returned value (2 bytes) Z (2 bytes) Saved SP value Y (2 bytes) X (2 bytes) Array numbers (not all shown) (20 bytes) (2) Array parameter from an outer call Consider quicksort, a recursive sorting algorithm. In C, the implementation might be void quicksort (int first, int last, int AR[]) { int P; if (first<last) { P = partition(first,last,ar); quicksort(first,p-1,ar); quicksort(p+1,last,ar); } } The following is an implementation of this function as a Pep/9 subroutine. Highlighted are the three instances where the array address parameter is copied from the outer call to location used by a recursive call or call to partition. Comp 162 Notes Page 4 of 15 March 14, 2018

5 quiksort: subsp 2,i ldwa 4,s cpwa 6,s brge end subsp 8,i ldwa 12,s stwa 0,s ldwa 14,s stwa 2,s ldwa 16,s stwa 4,s call partitio ldwa 6,s stwa 8,s addsp 8,i subsp 6,i ldwa 10,s stwa 0,s ldwa 6,s suba 1,i stwa 2,s ldwa 14,s stwa 4,s call quiksort ldwa 6,s adda 1,i stwa 0,s ldwa 12,s stwa 2,s call quiksort addsp 6,i end: addsp 2,i ret Our own stro If there were no stro instruction, the following subroutine could be used to output a null-terminated string with address passed as parameter (but note how registers A and X may be changed by the subroutine more on this later). mystro: ldwa 0,i ; clearing reg A to hold characters from string ldwx 0,i ; string index loop: ldba 2,sfx ; char from string breq exit ; if null byte we are done stba charout,d ; otherwise output addx 1,i ; move to next br loop exit: ret Comp 162 Notes Page 5 of 15 March 14, 2018

6 Finally, here are two examples of C array-processing functions translated into Pep/9. Example 1: copies N elements from one array to another. C void copy (int ar[1], int ar2[], int N) { int i; for (i=0; i<n; i++) ar2[i] = ar1[i]; } Pep/9 Local: i Ret. Addr.... N... The same value of register X is used to access both arrays so the Pep/9 is relatively straightforward copy: subsp 2,I ; for i ldwx 0,i stwx 0,s ; initialized to 0 top: cpwx 8,s ; i<n? brge done ; no, we are finished aslx ; convert to byte offset ldwa 4,sfx ; ar1[i] stwa 6,sfx ; ar2[i] = ar1[i] asrx ; convert back to index addx 1,i br top done: addsp 2,i ret Comp 162 Notes Page 6 of 15 March 14, 2018

7 Example 2: partial sort of an N-element array. The function partsort makes a pass through the array exchanging out-of-order neighbors. C void partsort(int ar[], int N) { int i,temp; } for (i=0; i<n-1; i++) if (ar[i]>ar[i+1]) { temp = ar[i]; ar[i]=ar[i+1]; ar[i+1]=temp; } Pep/9 Version 1 Local: temp Local: i Ret. Addr N... The relevant part of our example is the if statement which, given the stack above, translates to ldwx 2,s aslx ldwa 6,sfx addx 2,i cpwa 6,sfx brle skip stwa 0,s ldwa 6,sfx subx 2,i stwa 6,sfx ldwa 0,s addx 2,i stwa 6,sfx skip: ; i ; as byte offset ; ar[i] ; compare with ar[i+1] ; skip if no swap ; temp = ar[i] ; ar[i+1] ; ar[i] = ar[i+1] ; temp ; ar[i+1] = temp Comp 162 Notes Page 7 of 15 March 14, 2018

8 Note that three instructions just change the value in Register X. (This is a consequence of Pep/9 having only a single index register.) One way to eliminate these changes (and thus make the program run faster) is to have a second pointer to the array. The pointer is stored in the scratch space above the top of the stack and points not to the first element of the array but to the second thus: Pep/9 Version 2 Local: temp.local: i Ret. Addr N... Setting up the pointer is straightforward: ldwa 6,s adda 2,i stwa -2,s Now the code for the if statement is shorter skip: ldwx 2,s ; i aslx ; as byte offset ldwa 6,sfx ; ar[i] cpwa -2,sfx ; compare with ar[i+1] brle skip ; skip if no swap stwa 0,s ; temp = ar[i] ldwa -2,sfx ; ar[i+1] stwa 6,sfx ; ar[i] = ar[i+1] ldwa 0,s ; temp stwa -2,sfx ; ar[i+1] = temp We have added three instructions at the top of the subroutine to set up the pointer but shorted the then part of the if statement by three instructions. So the space required by this version of the subroutine is the same as the earlier one. However, if the array is large and many swaps are made we would expect our new version to run faster. Comp 162 Notes Page 8 of 15 March 14, 2018

9 Storage and manipulation of multi-dimensional arrays We need multi-dimensional arrays for representing matrices, chessboards, spreadsheets, timetables and so on. There are two main ways in which we can organize their storage. There is somewhat of a trade-off between space and time Option A: single contiguous block Store the array elements systematically in a single block either row-by-row or column-by-column. Suppose our array is declared as follows Values: array[1..3, 2..5] of integer; [ Some languages, e.g., Pascal, let us have non-zero lower bounds.] The contents are Row-by-row it is stored Column-by-column it is stored What is the address of element Values[i,j] (given that both i and j are in bounds)? For our example: Row-by-row mapping: the element is found at Values + 2 * ( (i-1) * 4 + (j-2)) [ In general at : Base-address + element-size * ((row_number row_lower_bound) * row_size + column_number - column_lower_bound) ] Column-by-column mapping: the element is found at Values + 2 * ((j-2) * 3 + (i-1)) For example, Values[3,4] is at M+20 in a row-by-row mapping and at M+16 in a column-bycolumn mapping. Comp 162 Notes Page 9 of 15 March 14, 2018

10 Many systems will store and access multi-dimensional arrays in this way because a MULTIPLY instruction is usually available to compute element addresses. But MULTIPLY is often a timeconsuming operation and some machines (e.g., Pep/9) may not have a multiply instruction so we need to look at another way to store and access multi-dimensional arrays. Option B: using Iliffe vectors We can devise a storage scheme that uses pointers instead of multiplication. Access may be faster than the multiply method although requiring more space (another example of trading time for space). Assume we store the array row by row (ideas are similar if it is stored column by column). Our method works by using an array of pointers to the array. This array of pointers is called an Iliffe vector (after J. K. Iliffe who proposed its use). Each pointer in the Iliffe vector points to the beginning of a row of the actual array. We label the Iliffe vector with the name of the array. In the following depiction of our array Values stored in this way we assume that the actual array starts at location 600 and the Iliffe vector starts at location 300. Note that there is no requirement for the rows to be adjacent in memory or even the same size. Values Comp 162 Notes Page 10 of 15 March 14, 2018

11 Now the Pep/9 code to implement is K = Values[Row,Col] ldwx Row,d ; the row number subx 1,i ; the row lower bound aslx ; make a byte offset ldwx Values,x ; get element from array of pointers and put in Reg. X ; this is pointer to row Row addx Col,d ; add 2 times addx Col,d ; Col subx 4,i ; and subtract 2* column lower bound ; so reg X now has address of element we need ldwa 0,x ; get the element stwa K,d ; and assign to K Here is a trace of Register X values for Row=3, Col=4 Reg. X 3 ldwx Row,d ; the row number 2 subx 1,i ; the row lower bound 4 aslx ; as byte offset 616 ldwx Values,x ; get element from array of pointers and put in Reg. X ; this is pointer to row Row 620 addx Col,d ; add 2 times 624 addx Col,d ; Col 620 subx 4,i ; and subtract 2* column lower bound ; so reg X now has address of element we need 30 ldwx 0,x ; get the element stwx K,d ; and assign to K C uses this method to store arrays. Advantages of the pointer method compared with the multiplication method include: (1) may be faster (2) storage for different rows of the actual array need not be adjacent (3) rows need not be the same length! Can have triangular and irregular arrays. We can extend the technique to higher number of dimensions with additional Iliffe vectors. Comp 162 Notes Page 11 of 15 March 14, 2018

12 Array bound checking Adding bound checking to either the multiplication method or the pointer-based method typically adds three or more instructions per dimension for each array access. The Option B code with checking highlighted is ldwx Row,d ; the row number cpwx MinRow ; the row lower bound brlt Error cpwx MaxRow ; the row upper bound brgt Error subx 1,i ; the row lower bound aslx ; as byte offset ldwx M,x ; get element from array of pointers and put in Reg. X ; this is pointer to row Row ldwa Col,d cpwa MinCol ; column lower bound brlt Error ; if Col < lower bound cpwa MaxCol brgt Error ; if Col > upper bound addx Col,d ; add 2 times addx Col,d ; subx 4,i ; subtract twice lower bound ; so reg X now has address of element we need ldwa 0,x ; get the element stwa K,d ; and assign to K Because it makes the program larger and slower, perhaps bound checking should be a compiler option. Special arrays: special access techniques (a) triangular arrays If a matrix is symmetric (M[i][j] = M[j][i]) we need only store half of it, for example elements M[i[[j] where j i. Storing the (variable-length) rows in adjacent memory locations: M[0][0] M[1][0] M[1][1] M[2][0] M[2][1] and so on the address of M[i][j] is M + (i+1)(i)/2 + j (b) sparse arrays If a large number of elements in an array (e.g., >90%) are zero, there may be ways to represent the contents in a more space-efficient manner. For example, we could store a list of triples (row, column, value) Comp 162 Notes Page 12 of 15 March 14, 2018

13 Identifying the locations of the non-zero values. Now accessing and updating functions are written in terms of this list. Memo functions A memo function is one that remembers (in some data structure) parameter-result pairs. If it is called again with the same parameter, it can recall the previous result to save recomputation. We might imagine such a function having N integer parameters saving the results in an N-dimensional array. The Fibonacci series can be computer, very inefficiently, using a recursive function: Fib(N) = if N=0 or N=1 then 1 else Fib(N-1)+Fib(N-2) Consider calculating Fib(20). Fib(20)=Fib(19)+Fib(18) = Fib(18)+Fib(17)+Fib(17)+Fib(16) The following table shows, the number of calls of Fib required to compute Fib(N) ( for N from 17 to 21) and the number of calls of memfib (a memoised version that saves computed values in an array. N Fib(N) Calls of fib Calls of memfib Reading Warford does not cover multi-dimensional arrays. Next we will look at how a switch statement can be implemented using an array. This starts on page 350 in the book. Comp 162 Notes Page 13 of 15 March 14, 2018

14 Review Questions 1. We have a Pep/9 subroutine CLEARA that takes two parameters: Address of an integer array (A) An integer N. A call to CLEARA sets the first N elements of A to zero. In our program we have X:.block 60 Y:.block 60 Intending to clear X, we call CLEARA with parameters X and 60. What happens to Y? 2. We have an integer matrix M with 30 rows and 30 columns. (a) how many bytes does M occupy? (b) The matrix is symmetric: M[i][j] = M[j][i]. What is the fewest number of elements we need to store to eliminate duplicates? 3. Consider M from question 2. We decide to store M[i][j] where i j. How can we calculate the address of M[i][j]? 4. We have seen that having a null-byte terminator in a character array (string) means we can just pass the string address to string handling subroutines no need for a second length parameter. Can we do the same for integer arrays? 5. A 100-byte ASCII string followed by a null byte is at label S. How can we replace all the non-null characters with spaces using fewer than 100 iterations of a loop? 6. Mode sfx in Pep/9 was called sxf in Pep/8. If mode sxf were added to Pep/9 how should it be interpreted? Comp 162 Notes Page 14 of 15 March 14, 2018

15 Review Answers 1. X has 30 integers (60 bytes) so CLEARA continues past the end of X and clears Y also. 2. (a) 1800 (b) 1 element from row 1, 2 from row 2, 30 from row 30 = If we store elements of M in this order M0,0, M0,1, M1,1, M0,2, M1,2, M2,2, then Mi,j is at M + 2 * [ i + j*(j+1) ] 4. Not in general. There is no integer that would be a universal terminator. 5. Treat the string as 50 words and loop assigning integer 0x2020 to each word. 6. It would let us access an item pointed to from an element of a local array. First the CPU would evaluate,sx to get the array element then follow the pointer to the item. Comp 162 Notes Page 15 of 15 March 14, 2018

Monday, October 24, 2016

Monday, October 24, 2016 Monday, October 24, 2016 Topics for today Arrays and Indexed Addressing Arrays as parameters of functions Multi-dimensional arrays Option A: Space-minimal solution Option B: Iliffe vectors Array bound

More information

Wednesday, October 17, 2012

Wednesday, October 17, 2012 Wednesday, October 17, 2012 Topics for today Arrays and Indexed Addressing Arrays as parameters of functions Multi-dimensional arrays Indexed branching Implementation of switch statement Arrays as parameters

More information

Monday, March 13, 2017

Monday, March 13, 2017 Monday, March 13, 2017 Topics for today Arrays and Indexed Addressing Global arrays Local arrays Buffer exploit attacks Arrays and indexed addressing (section 6.4) So far we have looked at scalars (int,

More information

Monday, March 27, 2017

Monday, March 27, 2017 Monday, March 27, 2017 Topics for today Indexed branching Implementation of switch statement Reusable subroutines Indexed branching It turns out that arrays are useful in translating other language constructs,

More information

Monday, March 6, We have seen how to translate void functions. What about functions that return a value such as

Monday, March 6, We have seen how to translate void functions. What about functions that return a value such as Monday, March 6, 2017 Topics for today C functions and Pep/9 subroutines Translating functions (c) Non-void functions (d) Recursive functions Reverse Engineering: Pep/9 to C C Functions and Pep/9 Subroutines

More information

Wednesday, March 29, Implementation of sets in an efficient manner illustrates some bit-manipulation ideas.

Wednesday, March 29, Implementation of sets in an efficient manner illustrates some bit-manipulation ideas. Wednesday, March 29, 2017 Topics for today Sets: representation and manipulation using bits Dynamic memory allocation Addressing mode summary Sets Implementation of sets in an efficient manner illustrates

More information

Wednesday, September 27, 2017

Wednesday, September 27, 2017 Wednesday, September 27, 2017 Topics for today Chapter 6: Mapping High-level to assembly-level The Pep/9 run-time stack (6.1) Stack-relative addressing (,s) SP manipulation Stack as scratch space Global

More information

Monday, October 17, 2016

Monday, October 17, 2016 Monday, October 17, 2016 Topics for today C functions and Pep/8 subroutines Passing parameters by reference Globals Locals Reverse Engineering II Representation of Booleans C Functions and Pep/8 Subroutines

More information

Wednesday, March 12, 2014

Wednesday, March 12, 2014 Wednesday, March 12, 2014 Topics for today Solutions to HW #3 Arrays and Indexed Addressing Global arrays Local arrays Buffer exploit attacks Solutions to Homework #3 1. deci N,d < (a) N not defined lda

More information

Wednesday, February 28, 2018

Wednesday, February 28, 2018 Wednesday, February 28, 2018 Topics for today C functions and Pep/9 subroutines Introduction Location of subprograms in a program Translating functions (a) Void functions (b) Void functions with parameters

More information

Monday, March 9, 2015

Monday, March 9, 2015 Monday, March 9, 2015 Topics for today C functions and Pep/8 subroutines Passing parameters by reference Globals Locals More reverse engineering: Pep/8 to C Representation of Booleans C Functions and Pep/8

More information

Wednesday, February 15, 2017

Wednesday, February 15, 2017 Wednesday, February 15, 2017 Topics for today Before and after assembly: Macros, Linkers Overview of Chapter 6 Branching Unconditional Status bits and branching If statements While statements The V and

More information

Wednesday, February 7, 2018

Wednesday, February 7, 2018 Wednesday, February 7, 2018 Topics for today The Pep/9 memory Four example programs The loader The assembly language level (Chapter 5) Symbolic Instructions Assembler directives Immediate mode and equate

More information

Wednesday, September 20, 2017

Wednesday, September 20, 2017 Wednesday, September 20, 2017 Topics for today More high-level to Pep/9 translations Compilers and Assemblers How assemblers work Symbol tables ILC Pass 1 algorithm, Error checking Pass 2 Immediate mode

More information

Wednesday, October 4, Optimizing compilers source modification Optimizing compilers code generation Your program - miscellaneous

Wednesday, October 4, Optimizing compilers source modification Optimizing compilers code generation Your program - miscellaneous Wednesday, October 4, 2017 Topics for today Code improvement Optimizing compilers source modification Optimizing compilers code generation Your program - miscellaneous Optimization Michael Jackson Donald

More information

Wednesday, April 16, 2014

Wednesday, April 16, 2014 Wednesday, pril 16, 2014 Topics for today Homework #5 solutions Code generation nalysis lgorithm 4: infix to tree Synthesis lgorithm 5: tree to code Optimization HW #5 solutions 1. lda 0,i ; for sum of

More information

Monday, October 26, 2015

Monday, October 26, 2015 Monday, October 26, 2015 Topics for today Indexed branching Implementation of switch statement Reusable subroutines Indexed branching It turns out that arrays are useful in translating other language constructs,

More information

Monday, November 7, Structures and dynamic memory

Monday, November 7, Structures and dynamic memory Monday, November 7, 2016 Topics for today Structures Structures and dynamic memory Grammars and Languages (Chapter 7) String generation Parsing Regular languages Structures We have seen one composite data

More information

Wednesday, November 15, 2017

Wednesday, November 15, 2017 Wednesday, November 15, 2017 Topics for today Code generation Synthesis Algorithm 5: tree to code Optimizations Code generation Algorithm 5: generating assembly code Visiting all the nodes in a linked

More information

Wednesday, September 21, 2016

Wednesday, September 21, 2016 Wednesday, September 21, 2016 Topics for today More high-level to translations Compilers and Assemblers How assemblers work Symbol tables ILC Pass 1 algorithm, Error checking Pass 2 Immediate mode and

More information

Wednesday, September 13, Chapter 4

Wednesday, September 13, Chapter 4 Wednesday, September 13, 2017 Topics for today Introduction to Computer Systems Static overview Operation Cycle Introduction to Pep/9 Features of the system Operational cycle Program trace Categories of

More information

Monday, February 16, 2015

Monday, February 16, 2015 Monday, February 16, 2015 Topics for today How assemblers work Symbol tables ILC Pass 1 algorithm, Error checking Pass 2 Immediate mode and equate Assembler variants: Disassembler, Cross assembler Macros

More information

Wednesday, February 4, Chapter 4

Wednesday, February 4, Chapter 4 Wednesday, February 4, 2015 Topics for today Introduction to Computer Systems Static overview Operation Cycle Introduction to Pep/8 Features of the system Operational cycle Program trace Categories of

More information

Wednesday, April

Wednesday, April Wednesday, April 9. 2014 Topics for today Addressing mode summary Structures Structures and dynamic memory Grammars and Languages (Chapter 7) String generation Parsing Regular languages Summary of addressing

More information

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i Instruction set Instruction Mnemonic Instruction Addressing Status Specifier Mode Bits 0000 0000 STOP Stop execution U 0000 0001 RET Return from CALL U 0000 0010 RETTR Return from trap U 0000 0011 MOVSPA

More information

Chapter 6. Compiling to the Assembly Level

Chapter 6. Compiling to the Assembly Level Chapter 6 Compiling to the Assembly Level Direct addressing Oprnd = Mem[OprndSpec] Asmb5 letter: d The operand specifier is the address in memory of the operand. Immediate addressing Oprnd = OprndSpec

More information

Wednesday, November 8, 2017

Wednesday, November 8, 2017 Wednesday, November 8, 207 Topics for today Grammars and Languages (hapter 7) Finite State Machines Semantic actions ode generation - Overview Finite State Machines (see 7.2) If a language is regular (Type

More information

Monday, April 9, 2018

Monday, April 9, 2018 Monday, April 9, 208 Topics for today Grammars and Languages (Chapter 7) Finite State Machines Semantic actions Code generation Overview Finite State Machines (see 7.2) If a language is regular (Type 3)

More information

Monday, September 28, 2015

Monday, September 28, 2015 Monda, September 28, 2015 Topics for toda Chapter 6: Mapping High-level to assembl-level The Pep/8 run-time stack (6.1) Stack-relative addressing (,s) SP manipulation Stack as scratch space Global variables

More information

APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL

APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL Operating System APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL 4 INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL Three types of operating systems

More information

Chapter 8. Process Management

Chapter 8. Process Management Chapter 8 Process Management Operating System APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL 4 INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL Three

More information

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4) Orders of Growth of Processes Today s topics Resources used by a program to solve a problem of size n Time Space Define order of growth Visualizing resources utilization using our model of evaluation Relating

More information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous

More information

Monday, August 28, 2017

Monday, August 28, 2017 Monday, August 28, 2017 Topics for today Course in context. Course outline, requirements, grading. Administrivia: Tutoring: Department, PLTL, LRC Knowledge Survey The concept of a multi-level machine Motivations

More information

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos. UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation

More information

Wednesday, February 19, 2014

Wednesday, February 19, 2014 Wednesda, Februar 19, 2014 Topics for toda Solutions to HW #2 Topics for Eam #1 Chapter 6: Mapping High-level to assembl-level The Pep/8 run-time stack Stack-relative addressing (,s) SP manipulation Stack

More information

Chapter 5: Recursion

Chapter 5: Recursion Chapter 5: Recursion Objectives Looking ahead in this chapter, we ll consider Recursive Definitions Function Calls and Recursive Implementation Anatomy of a Recursive Call Tail Recursion Nontail Recursion

More information

The University of North Carolina at Chapel Hill. Comp 411 Computer Organization Spring Problem Set #3 Solution Set

The University of North Carolina at Chapel Hill. Comp 411 Computer Organization Spring Problem Set #3 Solution Set Problem 1. Compiler Appreciation The University of North Carolina at Chapel Hill Comp 411 Computer Organization Spring 2012 Problem Set #3 Solution Set There are many solutions to these problems. My solutions

More information

Chapter 17 Recursion

Chapter 17 Recursion Chapter 17 Recursion What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces of data. Similar to recurrence function in mathematics. Like iteration -- can

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

How to declare an array in C?

How to declare an array in C? Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous values.

More information

More Complicated Recursion CMPSC 122

More Complicated Recursion CMPSC 122 More Complicated Recursion CMPSC 122 Now that we've gotten a taste of recursion, we'll look at several more examples of recursion that are special in their own way. I. Example with More Involved Arithmetic

More information

School of Informatics, University of Edinburgh

School of Informatics, University of Edinburgh CS1Bh Lecture Note 4 Dynamic Programming This lecture examines a problem solving technique known as dynamic programming. It is frequently used when a straightforward recursive solution to a problem has

More information

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution 1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can

More information

Chapter 17 Recursion

Chapter 17 Recursion Chapter 17 Recursion What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces of data. Similar to recurrence relation in mathematics. Sometimes recursion

More information

Wednesday, April 19, 2017

Wednesday, April 19, 2017 Wednesday, April 19, 2017 Topics for today Process management (Chapter 8) Loader Traps Interrupts, Time-sharing Storage management (Chapter 9) Main memory (1) Uniprogramming (2) Fixed-partition multiprogramming

More information

Weeks 6&7: Procedures and Parameter Passing

Weeks 6&7: Procedures and Parameter Passing CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]

More information

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014 Programming & Data Structure Laboratory rrays, pointers and recursion Day 5, ugust 5, 2014 Pointers and Multidimensional rray Function and Recursion Counting function calls in Fibonacci #include

More information

Provided by - Microsoft Placement Paper Technical 2012

Provided by   - Microsoft Placement Paper Technical 2012 Provided by www.yuvajobs.com - Microsoft Placement Paper Technical 2012 1. Analytical 25 questions ( 30 minutes) 2. Reasoning 25 questions (25 minutes) 3. Verbal 20 questions (20 minutes) Analytical (some

More information

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an

More information

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers Maltepe University Computer Engineering Department BİL 133 Algoritma ve Programlama Chapter 8: Arrays and pointers Basics int * ptr1, * ptr2; int a[10]; ptr1 = &a[2]; ptr2 = a; // equivalent to ptr2 =

More information

Sparse Matrices. sparse many elements are zero dense few elements are zero

Sparse Matrices. sparse many elements are zero dense few elements are zero Sparse Matrices sparse many elements are zero dense few elements are zero Special Matrices A square matrix has the same number of rows and columns. Some special forms of square matrices are Diagonal: M(i,j)

More information

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE 1 ARCHITECTURE MODEL The basic instruction set of a computer is comprised of sequences of REGISTER TRANSFERS. Example: Add A, B, C Register B # A

More information

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers CSE 230 Intermediate Programming in C and C++ Arrays and Pointers Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Definition: Arrays A collection of elements

More information

CSC 221: Computer Organization, Spring 2009

CSC 221: Computer Organization, Spring 2009 1 of 7 4/17/2009 10:52 AM Overview Schedule Resources Assignments Home CSC 221: Computer Organization, Spring 2009 Practice Exam 2 Solutions The exam will be open-book, so that you don't have to memorize

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion? Chapter 17 Recursion Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! A recursive function is one that solves its task by calling

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Computer Organization and Structure. Bing-Yu Chen National Taiwan University Computer Organization and Structure Bing-Yu Chen National Taiwan University Instructions: Language of the Computer Operations and Operands of the Computer Hardware Signed and Unsigned Numbers Representing

More information

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture ECE468 Computer Organization & Architecture MIPS Instruction Set Architecture ECE468 Lec4.1 MIPS R2000 / R3000 Registers 32-bit machine --> Programmable storage 2^32 x bytes 31 x 32-bit GPRs (R0 = 0) 32

More information

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables Activation Records The storage (for formals, local variables, function results etc.) needed for execution of a subprogram is organized as an activation record. An Activation Record for Simple Subprograms.

More information

www.thestudycampus.com Recursion Recursion is a process for solving problems by subdividing a larger problem into smaller cases of the problem itself and then solving the smaller, more trivial parts. Recursion

More information

Monday, February 11, 2013

Monday, February 11, 2013 Monday, February 11, 2013 Topics for today The Pep/8 memory Four example programs The loader The assembly language level (Chapter 5) Symbolic Instructions Assembler directives Immediate mode and equate

More information

6.001 Notes: Section 4.1

6.001 Notes: Section 4.1 6.001 Notes: Section 4.1 Slide 4.1.1 In this lecture, we are going to take a careful look at the kinds of procedures we can build. We will first go back to look very carefully at the substitution model,

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

Implementing Subprograms

Implementing Subprograms 1 Implementing Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University CS315 Programming Languages Pinar Duygulu The General Semantics of Calls and Returns 2 The subprogram call and return

More information

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures Outline 1 2 Solution 2: calls 3 Implementation To create recursion, you must create recursion. How to a recursive algorithm

More information

Module 6: Array in C

Module 6: Array in C 1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of

More information

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access Run Time Environment Activation Records Procedure Linkage Name Translation and Variable Access Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University

More information

Solutions for Chapter 2 Exercises

Solutions for Chapter 2 Exercises Solutions for Chapter 2 Exercises 1 Solutions for Chapter 2 Exercises 2.2 By lookup using the table in Figure 2.5 on page 62, 7fff fffa hex = 0111 1111 1111 1111 1111 1111 1111 1010 two = 2,147,483,642

More information

Wednesday, April 22, 2015

Wednesday, April 22, 2015 Wednesday, April 22, 2015 Topics for today Topics for Exam 3 Process management (Chapter 8) Loader Traps Interrupts, Time-sharing Storage management (Chapter 9) Main memory (1) Uniprogramming (2) Fixed-partition

More information

CS 2461: Computer Architecture I

CS 2461: Computer Architecture I Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space

More information

QUIZ. Name all the 4 parts of the fetch-execute cycle.

QUIZ. Name all the 4 parts of the fetch-execute cycle. QUIZ Name all the 4 parts of the fetch-execute cycle. 1 Solution Name all the 4 parts of the fetch-execute cycle. 2 QUIZ Name two fundamental differences between magnetic drives and optical drives: 3 QUIZ

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

Computers Programming Course 10. Iulian Năstac

Computers Programming Course 10. Iulian Năstac Computers Programming Course 10 Iulian Năstac Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in

More information

Supplement for MIPS (Section 4.14 of the textbook)

Supplement for MIPS (Section 4.14 of the textbook) Supplement for MIPS (Section 44 of the textbook) Section 44 does a good job emphasizing that MARIE is a toy architecture that lacks key feature of real-world computer architectures Most noticable, MARIE

More information

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search Chapter 17 Recursion Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! A recursive function is one that solves its task by calling

More information

COMPUTER ORGANIZATION AND DESIGN

COMPUTER ORGANIZATION AND DESIGN COMPUTER ORGANIZATION AND DESIGN 5 th The Hardware/Software Interface Edition Chapter 2 Instructions: Language of the Computer 2.1 Introduction Instruction Set The repertoire of instructions of a computer

More information

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting

More information

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body; CLASSROOM SESSION Loops in C Loops are used to repeat the execution of statement or blocks There are two types of loops 1.Entry Controlled For and While 2. Exit Controlled Do while FOR Loop FOR Loop has

More information

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points) ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points) Objectives: Get familiar with MIPS instructions Assemble, execute and debug MIPS

More information

Computers in Engineering. Linear Algebra Michael A. Hawker

Computers in Engineering. Linear Algebra Michael A. Hawker Computers in Engineering COMP 208 Linear Algebra Michael A. Hawker Representing Vectors A vector is a sequence of numbers (the components of the vector) If there are n numbers, the vector is said to be

More information

Computers in Engineering COMP 208. Representing Vectors. Vector Operations 11/29/2007. Scaling. Adding and Subtracting

Computers in Engineering COMP 208. Representing Vectors. Vector Operations 11/29/2007. Scaling. Adding and Subtracting Computers in Engineering COMP 208 Linear Algebra Michael A. Hawker Representing Vectors A vector is a sequence of numbers (the components of the vector) If there are n numbers, the vector is said to be

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

Instruction Set II. COMP 212 Computer Organization & Architecture. COMP 212 Fall Lecture 7. Instruction Set. Quiz. What is an Instruction Set?

Instruction Set II. COMP 212 Computer Organization & Architecture. COMP 212 Fall Lecture 7. Instruction Set. Quiz. What is an Instruction Set? COMP 212 Computer Organization & Architecture Quiz COMP 212 Fall 2008 Lecture 7 Fill in your student number only, do NOT write down your name Open book, but NO calculator, NO discussions, Relax and have

More information

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined): 2-D Arrays We define 2-D arrays similar to 1-D arrays, except that we must specify the size of the second dimension. The following is how we can declare a 5x5 int array: int grid[5][5]; Essentially, this

More information

We will study the MIPS assembly language as an exemplar of the concept.

We will study the MIPS assembly language as an exemplar of the concept. MIPS Assembly Language 1 We will study the MIPS assembly language as an exemplar of the concept. MIPS assembly instructions each consist of a single token specifying the command to be carried out, and

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

Subject: Computer Science

Subject: Computer Science Subject: Computer Science Topic: Data Types, Variables & Operators 1 Write a program to print HELLO WORLD on screen. 2 Write a program to display output using a single cout statement. 3 Write a program

More information

Dynamic Programming. An Introduction to DP

Dynamic Programming. An Introduction to DP Dynamic Programming An Introduction to DP Dynamic Programming? A programming technique Solve a problem by breaking into smaller subproblems Similar to recursion with memoisation Usefulness: Efficiency

More information

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin CS 61B Summer 2005 (Porter) Midterm 2 July 21, 2005 - SOLUTIONS Do not open until told to begin This exam is CLOSED BOOK, but you may use 1 letter-sized page of notes that you have created. Problem 0:

More information

Subprograms, Subroutines, and Functions

Subprograms, Subroutines, and Functions Subprograms, Subroutines, and Functions Subprograms are also called subroutines, functions, procedures and methods. A function is just a subprogram that returns a value; say Y = SIN(X). In general, the

More information

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

Assembly Language. Operand Size. The internal registers. Computers operate on chunks of data composed of a particular number of bits.

Assembly Language. Operand Size. The internal registers. Computers operate on chunks of data composed of a particular number of bits. 1 2 Chapter 6 Assembly Language Operand Size 8 bits 16 bits Computers operate on chunks of data composed of a particular number of bits. The 68K has a 32-bit architecture and a 16-bit organization. Internal

More information

Low-level software. Components Circuits Gates Transistors

Low-level software. Components Circuits Gates Transistors QUIZ Pipelining A computer pipeline has 4 processors, as shown above. Each processor takes 15 ms to execute, and each instruction must go sequentially through all 4 processors. A program has 10 instructions.

More information

Lecture 6: Assembly Programs

Lecture 6: Assembly Programs Lecture 6: Assembly Programs Today s topics: Procedures Examples Large constants The compilation process A full example 1 Procedures Local variables, AR, $fp, $sp Scratchpad and saves/restores, $fp Arguments

More information