Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
680XX Program Examples Outline –Binary to BCD –Matrix Addition –Ones Count –String Compare –Sector Map –Raster Graphics –Subroutine Calls Goal –Understand.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
MIPS Function Continued
The University of Adelaide, School of Computer Science
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 TopicC: Pointers and Arrays José Nelson Amaral.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Faculty of Computer Science © 2006 CMPUT 229 Why Computer Architecture? An Introduction to CMPUT 229.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.
CMPUT Efficiency  Often several ways to write the same program  Want to choose the most efficient implementation  Space efficiency  using small.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C Instructor: Dr Aleksandar Milenkovic Lecture Notes.
C Tokens Identifiers Keywords Constants Operators Special symbols.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Arrays and Strings in Assembly
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Imperative Programming C. Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands:
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6.
Intro to Pointers in C CSSE 332 Operating Systems
Pointers and Dynamic Arrays
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Network Programming CSC- 341
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
The University of Adelaide, School of Computer Science
Lecture 2 SCOPE – Local and Global variables
C++ Pointers and Strings
Chapter 9: Pointers and String
Chapter 9: Pointers and String
C++ Pointers and Strings
Presentation transcript:

Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data

© 2006 Department of Computing Science CMPUT 229 The car.c program #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node *next; } CarNode; void ReadCar(CarNode *car); void PrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); } void ReadCar(CarNode *car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } void PrintCar(CarNode car) { printf("vehicleID: %d\n",car.vehicleID); printf("make: %s\n",car.make); printf("model: %s\n",car.model); printf("year: %d\n",car.year); printf("mileage: %d\n",car.mileage); printf("cost: %f\n",car.cost); } Patt and Patel, pp. 419

© 2006 Department of Computing Science CMPUT 229 The car.c program #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; vehicleID make[20]model[20] yearmileage cost next

© 2006 Department of Computing Science CMPUT 229 The car.c program main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node *next; } CarNode; void ReadCar(CarNode *car); void PrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); }

© 2006 Department of Computing Science CMPUT 229 The car.c program SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts

© 2006 Department of Computing Science CMPUT 229 The car.c program SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts A6

© 2006 Department of Computing Science CMPUT 229 The car.c program SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts A6

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6 main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts D0 24 D1 24 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts D0 64 D1 24 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts D0 64 D1 24 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6

© 2006 Department of Computing Science CMPUT 229 memcpy NAME memcpy - copy memory area SYNOPSIS #include void *memcpy(void *dest, const void *src, size_t n); DESCRIPTION The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas should not overlap. Use memmove(3) if the memory areas do overlap. RETURN VALUE The memcpy() function returns a pointer to dest.

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts D0 64 D1 24 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6 cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID next

© 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID SP main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar addq.l #4,SP lea (-64,SP),SP move.l SP,D0 move.l D0,D1 lea (-68,A6),A0 moveq #64,D0 move.l D0,-(SP) move.l A0,-(SP) move.l D1,-(SP) jbsr memcpy lea (12,SP),SP jbsr PrintCar lea (64,SP),SP unlk A6 rts D0 64 D1 24 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] A6 cost2 cost1 mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID next

© 2006 Department of Computing Science CMPUT 229 Why so much copying? The program car.c passes the data structure CarNode to the PrintCar function by value. A copy of each byte of CarNode must be made in the stack for each call of the function PrintCar. We could, instead have passed the address of the copy of CarNode that we already had in the stack.

© 2006 Department of Computing Science CMPUT 229 The car2.c Program #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; void ReadCar(CarNode  car); void PrintCar(CarNode  car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } void ReadCar(CarNode  car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } void PrintCar(CarNode  car) { printf("vehicleID: %d\n",car->vehicleID); printf("make: %s\n",car->make); printf("model: %s\n",car->model); printf("year: %d\n",car->year); printf("mileage: %d\n",car->mileage); printf("cost: %f\n",car->cost); }

© 2006 Department of Computing Science CMPUT 229 Assembly for car2.c main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar jbsr PrintCar addq.l #4,SP unlk A6 rts #include #define STRINGLENGTH 20 typedef struct c_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; double cost; struct c_node  next; } CarNode; void ReadCar(CarNode  car); void PrintCar(CarNode  car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); }

© 2006 Department of Computing Science CMPUT 229 Assembly for car2.c main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar jbsr PrintCar addq.l #4,SP unlk A6 rts SP A6

© 2006 Department of Computing Science CMPUT 229 Assembly for car2.c main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar jbsr PrintCar addq.l #4,SP unlk A6 rts SP A6 A0 88

© 2006 Department of Computing Science CMPUT 229 Assembly for car2.c main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar jbsr PrintCar addq.l #4,SP unlk A6 rts SP A6 A0 88

© 2006 Department of Computing Science CMPUT 229 Assembly for car2.c main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar jbsr PrintCar addq.l #4,SP unlk A6 rts SP A6 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID

© 2006 Department of Computing Science CMPUT 229 Assembly for car2.c main: link.w A6,#-68 lea (-68,A6),A0 move.l A0,-(SP) jbsr ReadCar jbsr PrintCar addq.l #4,SP unlk A6 rts SP A6 A0 88 next cost mileage year model[16-19] model[12-15] model[8-11] model[4-7] model[0-3] make[16-19] make[12-15] make[8-11] make[4-7] make[0-3] vehicleID

© 2006 Department of Computing Science CMPUT 229 Quiz #1 predecessors and successors are two vectors of 32-bit unsigned integers. Assume that: predecessors’ first element is stored at A6+32 successors’ first element is stored at A6+64 Where A6 points to the base of the stack frame of the current procedure. Write a sequence of assembly instructions that executes the following C statement: successors[5] = predecessors[7];

© 2006 Department of Computing Science CMPUT 229 Quiz #1 - Solution predecessors and successors are two vectors of 32-bit unsigned integers. predecessors’ first element is stored at A6+32 successors’ first element is stored at A6+64 successors[5] = predecessors[7]; D0  predecessors[7]; successors[5]  D0; A1  Addr(predecessor[7]); D0  (A1); A2  Addr(successors[5]); (A2)  D0; A1  A6+32+7*4 D0  (A1); A2  A6+64+5*4 (A2)  D0; move.lA6, A1 addq.l #60,A1 move.l(A1), D0 move.l A6, A2 addq.l#84, A2 move.l D0, (A2) A1  A6+60 D0  (A1); A2  A6+84 (A2)  D0; Can you do better than this?

© 2006 Department of Computing Science CMPUT 229 Quiz #1 - Solution predecessors and successors are two vectors of 32-bit unsigned integers. predecessors’ first element is stored at A6+32 successors’ first element is stored at A6+64 successors[5] = predecessors[7]; D0  predecessors[7]; successors[5]  D0; A1  Addr(predecessor[7]); D0  (A1); A2  Addr(successors[5]); (A2)  D0; A1  A6+32+7*4 D0  0(A1); A2  A6+64+5*4 (A2)  D0; move.l(A6,60), D0 move.l D0, (A6,84) A1  A6+60 D0  (A1); A2  A6+84 (A2)  D0;

© 2006 Department of Computing Science CMPUT 229 Quiz #1 - Solution predecessors and successors are two vectors of 32-bit unsigned integers. predecessors’ first element is stored at A6+32 successors’ first element is stored at A6+64 successors[5] = predecessors[7]; D0  predecessors[7]; successors[5]  D0; A1  Addr(predecessor[7]); D0  (A1); A2  Addr(successors[5]); (A2)  D0; A1  A6+32+7*4 D0  0(A1); A2  A6+64+5*4 (A2)  D0; move.l(A6,60), (A6,84) A1  A6+60 D0  (A1); A2  A6+84 (A2)  D0;

© 2006 Department of Computing Science CMPUT 229 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of assembly instructions that counts the number of characters in the string whose pointer is at names[7].

© 2006 Department of Computing Science CMPUT 229 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of assembly instructions that counts the number of characters in the string whose pointer is at names[7]. count = 0; c = names[7]; while (*c != 0) { count = count+1; c = c+1; } C D0  0; A2  names[7]; while ( *(A2) != 0) { D0  D0+1; A2  A2+1; } C D0  0; A1  Address(names[7]); A2  (A1); while ((A2) != 0) { D0  D0+1; A2  A2+1; } RTL D0  0; A1  SP+16+7*4 A2  (A1); while ((A2)+ != 0) { D0  D0+1; } RTL

© 2006 Department of Computing Science CMPUT 229 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7]. D0  0; A1  SP+44; A2  (A1); while ((A2)+ != 0) { D0 = D0+1; } clr D0 move(SP,44), A2 cmp.b (A2)+, #0 beq done addq.l #1, D0 bra loop; done: loop: RTL D0  0; A2  (SP+44); while ((A2)+ != 0) { D0 = D0+1; } RTL Assembly

© 2006 Department of Computing Science CMPUT 229 Quiz #2 names is an array of pointers to strings. Each position of names contains a pointer to a null terminated string. Assume that the first element of names is stored in the memory position SP+16. Write a sequence of MIPS assembly instructions that counts the number of characters in the string whose pointer is at names[7]. clr D0 move(SP,44), A2 cmp.b (A2)+, #0 beq done addq.l #1, D0 bra loop; loop: Names[7] SP c 2 2 9\0 A2 D0  0; A2  (SP+44); while ((A2)+ != 0) { D0 = D0+1; } RTL

© 2006 Department of Computing Science CMPUT 229 Quiz #3 Consider the following C function: Write assembly code for the first statement of this function. Assume: temp  D0 node  D1 parameters are passed in the stack int ReadGraph(FILE  input_file, unsigned int  successors, char ***names) { unsigned int temp = 0; unsigned int node = 0; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); } Input_file successors names SP 8

© 2006 Department of Computing Science CMPUT 229 The meaning of (*successor)[node] int *vector; Intposition; Intvalue; vector[position] = value; (*successor)[node] = temp; successor vector int *vector; Intposition; Intvalue; *(vector+position) = value; = *((*successor)+node) = temp; = 

© 2006 Department of Computing Science CMPUT 229 Quiz #3 - Solution int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp = 0; unsigned int node; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); } (*successors)[node]  temp; (SP+8)[D1]  D0; A1  (SP+8) (A1+D1*4)  D0; move.l(SP,8), A1 Input_file successors names SP  node 8 temp  D0 node  D1 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #3 - Solution int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp = 0; unsigned int node; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); } (*successors)[node]  temp; (SP+8)[D1]  D0; A1  (SP+8) (A1+D1*4)  D0; move.l(SP,8), A1 move.l D1, D2 lsl.l #2, D2 move.l(A1,D2), D0 Input_file successors names SP  node 8 temp  D0 node  D1 A1 D0

© 2006 Department of Computing Science CMPUT 229 Quiz #4 Consider the following C function: Assume that ReadGraph receives its parameters in the stack. Write assembly code to push the parameter (*names)[node] into the stack before the call to the function call to printf. int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) { unsigned int temp; unsigned int node; (*successors)[node] = temp; printf(“name = %s\n”,(*names)[node]); }

© 2006 Department of Computing Science CMPUT 229 How did we ended up with  names ? names Adam\0Jame sCeci ly Murr a y

© 2006 Department of Computing Science CMPUT 229 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } SP+4 move.l(SP,4), A0# A0   names A0 Adam\0Jame sCeci ly Murr a y Input_file successors names SP 8 temp  D0 node  D1

© 2006 Department of Computing Science CMPUT 229 int ReadGraph(FILE  input_file, unisgned int  successors, char ***names) {  printf(“  ”,(*names)[node]); } SP+4 A1 move.l(SP,4), A0# A0   names Move.l D1, D2 # D2  4  node lsl.l #2, D1 # D1  4  node move.l(A0,D1), A1 # A1  (  names)[node] move.l A1, -(SP) # Push A1 into stack A0 Adam\0Jame sCeci ly Murr a y Input_file successors names SP 8 temp  D0 node  D1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; Addq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } ind Stack apple ptr A A0 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; Addq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } A6+8 Stack apple ptr A A6+8 A0 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; Addq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } A6+8 Stack apple A6+12 A A6+12 A6+8 A0 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; Addq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } A6+8 Stack 123 A6+12 A A6+8 A6+12 A0 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; Addq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } A6+12 Stack 123 A6+12 A A6+8 A6+12 A0 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; Addq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } A6+12 Stack 127 A6+12 A A6+12 A0 A1

© 2006 Department of Computing Science CMPUT 229 Quiz #7 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 # 7 ind = &ptr; lea (A6,8), A0A0  &ptr move.lA0, (A6,4)ind  &ptr # 8 *ind = &apple; lea (A6,12), A0A0  &apple move.l (A6,4), A1 A1  ind move.lA0, (A1) *ind  &apple # 9 **ind = 123; move.l (A6,4), A0 A0  ind move.l(A0), A1 A1  *ind move.l #123, (A1) **ind  123 # 10 ind++; addq.l#4, (A6,4) ind++ # 11 (*ptr)++; move.l(A6,8), A0A0  ptr addq.l#4, (A0)*ptr  *ptr + 4 # 12 apple++; qddq.l#4, (A6,12)apple  apple #include 2 main() 3 { 4 int apple; 5 int *ptr; 6 int **ind; 7 ind = &ptr; 8 *ind = &apple; 9 **ind = 123; 10 ind++; 11 (*ptr)++; 12 apple++; 13 printf(“%x %x %d\n”, ind, ptr, apple); 14 } A6+12 Stack 131 A6+12 A A6+12 A0 A1