Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data."— Presentation transcript:

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

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

3 © 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 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60

4 © 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); }

5 © 2006 Department of Computing Science CMPUT 229 The car.c program 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 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

6 © 2006 Department of Computing Science CMPUT 229 The car.c program 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

7 © 2006 Department of Computing Science CMPUT 229 The car.c program 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

8 © 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 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

9 © 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

10 © 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

11 © 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

12 © 2006 Department of Computing Science CMPUT 229 The car.c program make[0-3] vehicleID 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

13 © 2006 Department of Computing Science CMPUT 229 The car.c program 64 88 24 make[0-3] vehicleID 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

14 © 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.

15 © 2006 Department of Computing Science CMPUT 229 The car.c program 64 88 24 make[0-3] vehicleID 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 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

16 © 2006 Department of Computing Science CMPUT 229 The car.c program 64 88 24 make[0-3] vehicleID 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 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] 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 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

17 © 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.

18 © 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); }

19 © 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); }

20 © 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 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 SP 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6

21 © 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 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 SP 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6 A0 88

22 © 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 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 SP 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 A6 A0 88

23 © 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 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 SP 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 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

24 © 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 88 00 04 08 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 SP 180 184 186 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 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

25 © 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];

26 © 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?

27 © 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;

28 © 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;

29 © 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].

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

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

32 © 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] 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 SP c 2 2 9\0 A2 D0  0; A2  (SP+44); while ((A2)+ != 0) { D0 = D0+1; } RTL

33 © 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 16 20 24 28 32 36 SP 8

34 © 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; = 

35 © 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 16 20 24 28 32 36 SP  node 8 temp  D0 node  D1 A1

36 © 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 16 20 24 28 32 36 SP  node 8 temp  D0 node  D1 A1 D0

37 © 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]); }

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

39 © 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 16 20 24 28 32 36 SP 8 temp  D0 node  D1

40 © 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 16 20 24 28 32 36 SP 8 temp  D0 node  D1

41 © 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 + 4 1 #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 A6 0 4 8 12 A0 A1

42 © 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 + 4 1 #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 A6 0 4 8 12 A6+8 A0 A1

43 © 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 + 4 1 #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 A6 0 4 8 12 A6+12 A6+8 A0 A1

44 © 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 + 4 1 #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 A6 0 4 8 12 A6+8 A6+12 A0 A1

45 © 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 + 4 1 #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 A6 0 4 8 12 A6+8 A6+12 A0 A1

46 © 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 + 4 1 #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 A6 0 4 8 12 A6+12 A0 A1

47 © 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 + 4 1 #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 A6 0 4 8 12 A6+12 A0 A1


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

Similar presentations


Ads by Google