1 / 47

Pointers and Arrays (part 2)

Differentiating Pointers from Data. Pointers and Arrays (part 2). The car.c program. #include <stdio.h> #define STRINGLENGTH 20 typedef struct c_node { int vehicleID ; char make [STRINGLENGTH]; char model [STRINGLENGTH]; int year ; int mileage ; double cost ;

adriel
Download Presentation

Pointers and Arrays (part 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Differentiating Pointers from Data Pointers and Arrays(part 2) CMPUT 229

  2. CMPUT 229 The car.c program #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node *next; } CarNode; voidReadCar(CarNode *car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); } voidReadCar(CarNode *car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } voidPrintCar(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. CMPUT 229 model[20] next make[20] year mileage vehicleID cost The car.c program 60 56 52 48 #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; 44 40 36 32 28 24 20 16 12 8 4 0

  4. CMPUT 229 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 The car.c program #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node *next; } CarNode; voidReadCar(CarNode *car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(mycar); }

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

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

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

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

  9. CMPUT 229 SP 186 184 180 176 172 168 164 160 156 152 <old A6> 148 next 144 cost 140 cost 136 mileage 132 year 128 model[16-19] 124 model[12-15] 120 model[8-11] 116 model[4-7] 112 model[0-3] 108 make[16-19] 104 make[12-15] A6 100 make[8-11] 96 make[4-7] 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 The car.c program 92 make[0-3] 88 vehicleID 84 88 80 76 72 68 64 60 56 52 48 44 40 36 32 28 24 20 16 12 08 04 00

  10. CMPUT 229 SP 186 184 180 176 172 168 164 160 156 152 <old A6> 148 next 144 cost 140 cost 136 mileage 132 year 128 model[16-19] 124 model[12-15] 120 model[8-11] 116 model[4-7] 112 model[0-3] 108 make[16-19] 104 make[12-15] A6 100 make[8-11] 96 make[4-7] 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 The car.c program 92 make[0-3] 88 vehicleID 84 88 80 76 72 68 64 60 56 52 48 44 40 36 32 28 24 20 16 12 08 04 00

  11. CMPUT 229 SP 186 184 180 176 172 D0 168 24 164 160 156 152 <old A6> 148 next 144 cost 140 cost D1 136 mileage 132 year 24 128 model[16-19] 124 model[12-15] 120 model[8-11] 116 model[4-7] 112 model[0-3] 108 make[16-19] 104 make[12-15] A6 100 make[8-11] 96 make[4-7] 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 The car.c program 92 make[0-3] 88 vehicleID 84 88 80 76 72 68 64 60 56 52 A0 48 44 88 40 36 32 28 24 20 16 12 08 04 00

  12. CMPUT 229 SP 186 184 180 176 172 D0 168 64 164 160 156 152 <old A6> 148 next 144 cost 140 cost D1 136 mileage 132 year 24 128 model[16-19] 124 model[12-15] 120 model[8-11] 116 model[4-7] 112 model[0-3] 108 make[16-19] 104 make[12-15] A6 100 make[8-11] 96 make[4-7] 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 The car.c program 92 make[0-3] 88 vehicleID 84 88 80 76 72 68 64 60 56 52 A0 48 44 88 40 36 32 28 24 20 16 12 08 04 00

  13. CMPUT 229 SP 186 184 180 176 172 D0 168 64 164 160 156 152 <old A6> 148 next 144 cost 140 cost D1 136 mileage 132 year 24 128 model[16-19] 124 model[12-15] 120 model[8-11] 116 model[4-7] 112 model[0-3] 108 make[16-19] 104 make[12-15] A6 100 make[8-11] 96 make[4-7] 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 The car.c program 92 make[0-3] 88 vehicleID 84 88 80 76 72 68 64 60 56 52 A0 48 44 88 40 36 32 28 24 20 64 16 88 12 24 08 04 00

  14. CMPUT 229 memcpy NAME memcpy - copy memory area SYNOPSIS #include <string.h> 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. CMPUT 229 SP 186 184 180 176 172 D0 168 64 164 160 156 152 <old A6> 148 next 144 cost 140 cost D1 136 mileage 132 year 24 128 model[16-19] 124 model[12-15] 120 model[8-11] 116 model[4-7] 112 model[0-3] 108 make[16-19] 104 make[12-15] A6 100 make[8-11] 96 make[4-7] 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 The car.c program 92 make[0-3] 88 vehicleID 84 next 80 cost2 76 cost1 72 mileage 68 year 64 model[16-19] 60 model[12-15] 56 model[8-11] 52 model[4-7] A0 48 model[0-3] 44 make[16-19] 88 40 make[12-15] 36 make[8-11] 32 make[4-7] 28 make[0-3] 24 vehicleID 20 64 16 88 12 24 08 04 00

  16. CMPUT 229 SP D0 64 D1 24 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 The car.c program 186 92 make[0-3] 184 88 vehicleID 180 84 next 80 176 cost2 172 76 cost1 168 72 mileage 164 68 year 160 64 model[16-19] 60 156 model[12-15] 56 152 <old A6> model[8-11] 52 148 next model[4-7] A0 144 48 cost model[0-3] 140 44 cost make[16-19] 88 40 136 mileage make[12-15] 36 132 year make[8-11] 32 128 model[16-19] make[4-7] 124 28 model[12-15] make[0-3] 120 24 model[8-11] vehicleID 20 116 model[4-7] 64 16 112 model[0-3] 88 12 108 make[16-19] 24 08 104 make[12-15] 100 04 make[8-11] 00 96 make[4-7]

  17. 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. CMPUT 229 The car2.c Program #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; voidReadCar(CarNode car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } voidReadCar(CarNode car) { car->vehicleID = 2; strcpy(car->make,"DODGE"); strcpy(car->model,"STRATUS"); car->year = 1996; car->mileage = 70000; car->cost = 4,525.74; } voidPrintCar(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. CMPUT 229 Assembly for car2.c #include<stdio.h> #defineSTRINGLENGTH 20 typedef structc_node{ int vehicleID; char make[STRINGLENGTH]; char model[STRINGLENGTH]; int year; int mileage; doublecost; struct c_node next; } CarNode; voidReadCar(CarNode car); voidPrintCar(CarNode car); main() { CarNode mycar; ReadCar(&mycar); PrintCar(&mycar); } main: • link.w A6,#-68 • lea (-68,A6),A0 • move.l A0,-(SP) • jbsr ReadCar • jbsr PrintCar • addq.l #4,SP • unlk A6 • rts

  20. CMPUT 229 SP 186 184 180 176 172 168 164 160 156 152 <old A6> 148 144 140 136 132 128 124 120 116 112 108 104 A6 100 96 Assembly for car2.c 92 88 84 main: • link.w A6,#-68 • lea (-68,A6),A0 • move.l A0,-(SP) • jbsr ReadCar • jbsr PrintCar • addq.l #4,SP • unlk A6 • rts 80 76 72 68 64 60 56 52 48 44 40 36 32 28 24 20 16 12 08 04 00

  21. CMPUT 229 SP 186 184 180 176 172 168 164 160 156 152 <old A6> 148 144 140 136 132 128 124 120 116 112 108 104 A6 100 96 Assembly for car2.c 92 88 84 main: • link.w A6,#-68 • lea (-68,A6),A0 • move.l A0,-(SP) • jbsr ReadCar • jbsr PrintCar • addq.l #4,SP • unlk A6 • rts 80 76 72 68 64 60 56 52 A0 48 44 88 40 36 32 28 24 20 16 12 08 04 00

  22. CMPUT 229 SP 186 184 180 176 172 168 164 160 156 152 <old A6> 148 144 140 136 132 128 124 120 116 112 108 104 A6 100 96 Assembly for car2.c 92 88 84 88 main: • link.w A6,#-68 • lea (-68,A6),A0 • move.l A0,-(SP) • jbsr ReadCar • jbsr PrintCar • addq.l #4,SP • unlk A6 • rts 80 76 72 68 64 60 56 52 A0 48 44 88 40 36 32 28 24 20 16 12 08 04 00

  23. CMPUT 229 SP A6 Assembly for car2.c 186 92 make[0-3] 184 88 vehicleID 180 84 88 main: • link.w A6,#-68 • lea (-68,A6),A0 • move.l A0,-(SP) • jbsr ReadCar • jbsr PrintCar • addq.l #4,SP • unlk A6 • rts 80 176 172 76 168 72 164 68 160 64 60 156 56 152 <old A6> 52 148 next A0 144 48 cost 140 44 cost 88 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] 08 104 make[12-15] 100 04 make[8-11] 00 96 make[4-7]

  24. CMPUT 229 SP A6 Assembly for car2.c 186 92 make[0-3] 184 88 vehicleID 180 84 88 main: • link.w A6,#-68 • lea (-68,A6),A0 • move.l A0,-(SP) • jbsr ReadCar • jbsr PrintCar • addq.l #4,SP • unlk A6 • rts 80 176 172 76 168 72 164 68 160 64 60 156 56 152 <old A6> 52 148 next A0 144 48 cost 140 44 cost 88 40 136 mileage 36 132 year 32 128 model[16-19] 124 28 model[12-15] 120 24 model[8-11] 20 116 model[4-7] 16 112 model[0-3] 12 108 make[16-19] 08 104 make[12-15] 100 04 make[8-11] 00 96 make[4-7]

  25. 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. CMPUT 229 Quiz #1 - Solution Can you do better than this? 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]; A1  A6+60 D0  (A1); A2  A6+84 (A2)  D0; D0  predecessors[7]; successors[5]  D0; A1  Addr(predecessor[7]); D0  (A1); A2  Addr(successors[5]); (A2)  D0; move.l A6, A1 addq.l #60,A1 move.l (A1), D0 move.l A6, A2 addq.l #84, A2 move.l D0, (A2) A1  A6+32+7*4 D0  (A1); A2  A6+64+5*4 (A2)  D0;

  27. 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]; A1  A6+60 D0  (A1); A2  A6+84 (A2)  D0; D0  predecessors[7]; successors[5]  D0; A1  Addr(predecessor[7]); D0  (A1); A2  Addr(successors[5]); (A2)  D0; move.l (A6,60), D0 move.l D0, (A6,84) A1  A6+32+7*4 D0  0(A1); A2  A6+64+5*4 (A2)  D0;

  28. 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]; A1  A6+60 D0  (A1); A2  A6+84 (A2)  D0; D0  predecessors[7]; successors[5]  D0; A1  Addr(predecessor[7]); D0  (A1); A2  Addr(successors[5]); (A2)  D0; move.l (A6,60), (A6,84) A1  A6+32+7*4 D0  0(A1); A2  A6+64+5*4 (A2)  D0;

  29. 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. CMPUT 229 D0  0; A1  Address(names[7]); A2  (A1); while ((A2) != 0) { D0  D0+1; A2  A2+1; } D0  0; A1  SP+16+7*4 A2  (A1); while ((A2)+ != 0) { D0  D0+1; } D0  0; A2 names[7]; while ( *(A2) != 0) { D0  D0+1; A2  A2+1; } RTL C RTL 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

  31. CMPUT 229 D0  0; A2  (SP+44); while ((A2)+ != 0) { D0 = D0+1; } RTL 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; done: D0  0; A1  SP+44; A2  (A1); while ((A2)+ != 0) { D0 = D0+1; } loop: RTL Assembly

  32. CMPUT 229 SP A2 D0  0; A2  (SP+44); while ((A2)+ != 0) { D0 = D0+1; } RTL 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]. Quiz #2 76 72 68 Names[7] 64 60 56 c 2 2 9 \0 52 48 44 40 clr D0 move (SP,44), A2 cmp.b (A2)+, #0 beq done addq.l #1, D0 bra loop; 36 32 28 24 loop: 20 16 12

  33. CMPUT 229 SP 36 Quiz #3 32 28 Input_file 24 successors 20 names 16 <ret adr> Consider the following C function: 8 intReadGraph(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]); } Write assembly code for the first statement of this function. Assume: temp  D0 node  D1 parameters are passed in the stack

  34. CMPUT 229 int *vector; Int position; Int value; *(vector+position) = value; = vector  = *((*successor)+node) = temp; successor  The meaning of (*successor)[node] int *vector; Int position; Int value; vector[position] = value; (*successor)[node] = temp;

  35. CMPUT 229 SP A1 36 Quiz #3 - Solution 32 28 Input_file node 24  successors temp  D0 node  D1 20 names 16 <ret adr> 8 (*successors)[node]  temp; (SP+8)[D1]  D0; move.l (SP,8), A1 A1  (SP+8) (A1+D1*4)  D0; intReadGraph(FILEinput_file, unisgned intsuccessors, char ***names) { unsigned int temp = 0; unsigned int node; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); }

  36. CMPUT 229 SP D0 A1 36 Quiz #3 - Solution 32 28 Input_file node 24  successors temp  D0 node  D1 20 names 16 <ret adr> 8 (*successors)[node]  temp; (SP+8)[D1]  D0; move.l (SP,8), A1 move.l D1, D2 lsl.l #2, D2 move.l (A1,D2), D0 A1  (SP+8) (A1+D1*4)  D0; intReadGraph(FILEinput_file, unisgned intsuccessors, char ***names) { unsigned int temp = 0; unsigned int node; (*successors)[node] = temp; printf(“name = %d\n”,*names[node]); }

  37. CMPUT 229 Quiz #4 Consider the following C function: intReadGraph(FILEinput_file, unisgned intsuccessors, char ***names) { unsigned int temp; unsigned int node; (*successors)[node] = temp; printf(“name = %s\n”,(*names)[node]); } 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.

  38. CMPUT 229 A d a m \0 J a m e s \0 y M u r r a \0 C e c i l y \0 How did we ended up with names ? names

  39. CMPUT 229 intReadGraph(FILEinput_file, unisgned intsuccessors, char ***names) {  printf(“”,(*names)[node]); } 36 32 SP 28 Input_file 24 successors 20 names 16 <ret adr> 8 A d a m \0 J a m e s \0 A0 y M u r r a \0 C e c i l y \0 move.l (SP,4), A0 # A0  names SP+4 temp  D0 node  D1

  40. CMPUT 229 intReadGraph(FILEinput_file, unisgned intsuccessors, char ***names) {  printf(“”,(*names)[node]); } 36 32 SP 28 Input_file 24 successors 20 names 16 <ret adr> 8 A d a m \0 J a m e s \0 A0 y M u r r a \0 A1 C e c i l y \0 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.lA1, -(SP) # Push A1 into stack SP+4 temp  D0 node  D1

  41. CMPUT 229 Stack 12 apple ptr 8 ind 4 A6 0 A0 Quiz #7 A1 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; Addq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

  42. CMPUT 229 Stack 12 apple ptr 8 A6+8 4 A6 0 A0 A6+8 Quiz #7 A1 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; Addq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

  43. CMPUT 229 Stack 12 apple A6+12 8 A6+8 4 A6 0 A0 A6+12 Quiz #7 A1 A6+8 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; Addq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

  44. CMPUT 229 Stack 12 123 A6+12 8 A6+8 4 A6 0 A0 A6+8 Quiz #7 A1 A6+12 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; Addq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

  45. CMPUT 229 Stack 12 123 A6+12 8 A6+12 4 A6 0 A0 A6+8 Quiz #7 A1 A6+12 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; Addq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

  46. CMPUT 229 Stack 12 127 A6+12 8 A6+12 4 A6 0 A0 A6+12 Quiz #7 A1 A6+12 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; Addq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

  47. CMPUT 229 Stack 12 131 A6+12 8 A6+12 4 A6 0 A0 A6+12 Quiz #7 A1 A6+12 # 7 ind = &ptr; lea (A6,8), A0 A0  &ptr move.l A0, (A6,4) ind  &ptr # 8 *ind = &apple; lea (A6,12), A0 A0  &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), A0 A0  ptr addq.l #4, (A0) *ptr  *ptr + 4 # 12 apple++; qddq.l #4, (A6,12) apple  apple + 4 Write assembly for the following program. Assume that: apple  A6+12 ptr  A6+8 ind  A6+4 1 #include <stdio.h> 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 }

More Related