Monday, October 24, 2016

Size: px
Start display at page:

Download "Monday, October 24, 2016"

Transcription

1 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 checking 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 14 October 24, 2016

2 Here is the call sum(t,60) in Pep/8 subsp 2,i ; for returned value subsp 4,i ; for parameters; lda T,i ; address of array sta 0,s ; is the first parameter lda 60,i ; count of elements sta 2,s ; is the second parameter call 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 ldx 0,s ; the local variable j in index register lda 2,s ; temp in Reg. A aslx ; index converted to byte offset adda 6,sxf ; add V[j] sta 2,s ; update temp Comp 162 Notes Page 2 of 14 October 24, 2016

3 The new mode (,sxf) is stack-indexed deferred 1. 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,sxf 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/8 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 sta 0,s ; array parameter lda 10,i sta 2,s ; size parameter call sum 1 To be consistent, this mode should really be sfx and will be renamed in this way in Pep/9. Comp 162 Notes Page 3 of 14 October 24, 2016

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/8 subroutine. Highlighted are the two instructions where the array address parameter is copied from the outer call to location used by a recursive call. Comp 162 Notes Page 4 of 14 October 24, 2016

5 quiksort: subsp 2,i lda 4,s cpa 6,s brge end subsp 8,i lda 12,s sta 0,s lda 14,s sta 2,s lda 16,s sta 4,s call partitio lda 6,s sta 8,s addsp 8,i subsp 6,i lda 10,s sta 0,s lda 6,s suba 1,i sta 2,s lda 14,s sta 4,s call quiksort lda 6,s adda 1,i sta 0,s lda 12,s sta 2,s call quiksort addsp 6,i end: ret2 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. mystro: lda 0,i ; clearing reg A to hold characters from string ldx 0,i ; string index loop: ldbytea 2,sxf ; char from string breq exit ; if null byte we are done charo 2,sxf ; otherwise output addx 1,i ; move to next br loop exit: ret0 Comp 162 Notes Page 5 of 14 October 24, 2016

6 Finally, here are two examples of C array-processing functions translated into Pep/8. 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/8 Local: i Ret. Addr.... N... The same value of register X is used to access both arrays so the Pep/8 is relatively straightforward copy: subsp 2,i ; for i ldx 0,i stx 0,s ; initialized to 0 top: cpx 8,s ; i<n? brge done ; no, we are finished aslx ; convert to byte offset lda 4,sxf ; ar1[i] sta 6,sxf ; ar2[i] = ar1[i] asrx ; convert back to index addx 1,i br top done: ret2 Comp 162 Notes Page 6 of 14 October 24, 2016

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/8 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 ldx 2,s aslx lda 6,sxf addx 2,i cpa 6,sxf brle skip sta 0,s lda 6,sxf subx 2,i sta 6,sxf lda 0,s addx 2,i sta 6,sxf 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 14 October 24, 2016

8 Note that three instructions just change the value in Register X. (This is a consequence of Pep/8 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/8 Version 2 Local: temp.local: i Ret. Addr N... Setting up the pointer is straightforward: lda 6,s adda 2,i sta -2,s Now the code for the if statement is shorter skip: ldx 2,s ; i aslx ; as byte offset lda 6,sxf ; ar[i] cpa -2,sxf ; compare with ar[i+1] brle skip ; skip if no swap sta 0,s ; temp = ar[i] lda -2,sxf ; ar[i+1] sta 6,sxf ; ar[i] = ar[i+1] lda 0,s ; temp sta -2,sxf ; 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 14 October 24, 2016

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 14 October 24, 2016

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/8) 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 14 October 24, 2016

11 Now the Pep/8 code to implement is K = Values[Row,Col] ldx Row,d ; the row number subx 1,i ; the row lower bound aslx ; make a byte offset ldx 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 lda 0,x ; get the element sta K,d ; and assign to K Here is a trace of Register X values for Row=3, Col=4 Reg. X 3 ldx Row,d ; the row number 2 subx 1,i ; the row lower bound 4 aslx ; as byte offset 616 ldx 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 ldx 0,x ; get the element stx 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 14 October 24, 2016

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 added in bold is ldx Row,d ; the row number cpx MinRow ; the row lower bound brlt Error cpx MaxRow ; the row upper bound brgt Error subx 1,i ; the row lower bound aslx ; as byte offset ldx M,x ; get element from array of pointers and put in Reg. X ; this is pointer to row Row lda Col,d cpa MinCol ; column lower bound brlt Error ; if Col < lower bound cpa 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 lda 0,x ; get the element sta 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 14 October 24, 2016

13 Identifying the locations of the non-zero values. Now accessing and updating functions are written in terms of this list. 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 297 in the book. Review Questions 1. We have a Pep/8 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? 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? Comp 162 Notes Page 13 of 14 October 24, 2016

14 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. Comp 162 Notes Page 14 of 14 October 24, 2016

Wednesday, March 14, 2018

Wednesday, March 14, 2018 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

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

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

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

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

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

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

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

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

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

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

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

A rubric for programming assignments

A rubric for programming assignments Fall 2012 Comp 162 Peter Smith A rubric for programming assignments Generally, half the points for a program assignment are for the Correctness of the program with respect to the specification. The other

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

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

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

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

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

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

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

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 Solution

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

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

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

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

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

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

Chapter. Assembly Language

Chapter. Assembly Language Chapter 5 Assembly Language Mappings The mapping from Asmb5 to ISA3 is one-toone The mapping from HOL6 to Asmb5 is oneto-many Symbols Defined by an identifier followed by a colon at the start of a statement

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

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

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

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

Monday, April 15, We will lead up to the Analysis and Synthesis algorithms involved by first looking at three simpler ones.

Monday, April 15, We will lead up to the Analysis and Synthesis algorithms involved by first looking at three simpler ones. Monday, pril 15, 2013 Topics for today Code generation nalysis lgorithm 1: evaluation of postfix lgorithm 2: infix to postfix lgorithm 3: evaluation of infix lgorithm 4: infix to tree Synthesis lgorithm

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

Extra-credit QUIZ Pipelining -due next time-

Extra-credit QUIZ Pipelining -due next time- 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

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

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

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

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

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

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

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

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

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

[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

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

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

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

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

COSC 243. Assembly Language Techniques. Lecture 9. COSC 243 (Computer Architecture)

COSC 243. Assembly Language Techniques. Lecture 9. COSC 243 (Computer Architecture) COSC 243 Assembly Language Techniques 1 Overview This Lecture Source Handouts Next Lectures Memory and Storage Systems 2 Parameter Passing In a high level language we don t worry about the number of parameters

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

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

Arrays, Pointers and Memory Management

Arrays, Pointers and Memory Management Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by

More information

Monday, April 14, 2014

Monday, April 14, 2014 Monday, April 14, 2014 Topics for today Grammars and Languages (Chapter 7) Finite State Machines Semantic actions Code generation - Overview Analysis Algorithm 1: evaluation of postfix Algorithm 2: infix

More information

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop QUIZ: loops Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop QUIZ: loops Write a program that prints the integers from -7 to 15 using: for

More information

Today's Topics. CISC 458 Winter J.R. Cordy

Today's Topics. CISC 458 Winter J.R. Cordy Today's Topics Last Time Semantics - the meaning of program structures Stack model of expression evaluation, the Expression Stack (ES) Stack model of automatic storage, the Run Stack (RS) Today Managing

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

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

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

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

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

Lab 3. Pointers Programming Lab (Using C) XU Silei

Lab 3. Pointers Programming Lab (Using C) XU Silei Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions

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

Personal SE. Computer Memory Addresses C Pointers

Personal SE. Computer Memory Addresses C Pointers Personal SE Computer Memory Addresses C Pointers Computer Memory Organization Memory is a bucket of bytes. Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Computer Memory

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #44 Multidimensional Array and pointers In this video, we will look at the relation between Multi-dimensional

More information

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same

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

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

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

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

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

n = 1 What problems are interesting when n is just 1?

n = 1 What problems are interesting when n is just 1? What if n=1??? n = 1 What problems are interesting when n is just 1? Sorting? No Median finding? No Addition? How long does it take to add one pair of numbers? Multiplication? How long does it take to

More information

Lesson 7. Reading and Writing a.k.a. Input and Output

Lesson 7. Reading and Writing a.k.a. Input and Output Lesson 7 Reading and Writing a.k.a. Input and Output Escape sequences for printf strings Source: http://en.wikipedia.org/wiki/escape_sequences_in_c Escape sequences for printf strings Why do we need escape

More information

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Week 02 Module 06 Lecture - 14 Merge Sort: Analysis So, we have seen how to use a divide and conquer strategy, we

More information

Lecture 7: Examples, MARS, Arithmetic

Lecture 7: Examples, MARS, Arithmetic Lecture 7: Examples, MARS, Arithmetic Today s topics: More examples MARS intro Numerical representations 1 Dealing with Characters Instructions are also provided to deal with byte-sized and half-word quantities:

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

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at

More information

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

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

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

CS1 Lecture 22 Mar. 6, 2019

CS1 Lecture 22 Mar. 6, 2019 CS1 Lecture 22 Mar. 6, 2019 HW 5 due Friday Questions? In discussion exams next week Last time Ch 12. Zip, lambda, etc Default/keyword function parameters Ch 19 conditional expresssions, list comprehensions

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

Computer Science is...

Computer Science is... Computer Science is... Machine Learning Machine learning is the study of computer algorithms that improve automatically through experience. Example: develop adaptive strategies for the control of epileptic

More information

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will: Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array

More information

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it Last Class Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it public class February4{ public static void main(string[] args) { String[]

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

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

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