View
219
Download
1
Tags:
Embed Size (px)
Citation preview
Faculty of Computer Science
CMPUT 229 © 2006
Pointers and Arrays(part 2)
Differentiating Pointers from Data
© 2006
Department of Computing Science
CMPUT 229
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; 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
048
12162024283236404448525660
© 2006
Department of Computing Science
CMPUT 229
The car.c programmain:
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 <stdio.h>#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
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
112116
120
124
128
132
136
140
144148
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
© 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
92SP
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old A6>
© 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
92SP
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old A6>
© 2006
Department of Computing Science
CMPUT 229
The car.c programmake[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
92SP
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
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
© 2006
Department of Computing Science
CMPUT 229
The car.c programmake[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
92SP
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
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6
© 2006
Department of Computing Science
CMPUT 229
The car.c programmake[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
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6
© 2006
Department of Computing Science
CMPUT 229
The car.c programmake[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
D024
D124
A088
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6
© 2006
Department of Computing Science
CMPUT 229
The car.c programmake[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
D064
D124
A088
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6
© 2006
Department of Computing Science
CMPUT 229
The car.c program
648824
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
D064
D124
A088
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6
© 2006
Department of Computing Science
CMPUT 229
memcpyNAME
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.
© 2006
Department of Computing Science
CMPUT 229
The car.c program
648824
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
D064
D124
A088
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
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
© 2006
Department of Computing Science
CMPUT 229
The car.c program
648824
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
D064
D124
A088
<old A6>next
cost
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
112116
120
124
128
132
136
140
144148
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
© 2006
Department of Computing Science
CMPUT 229
Why so much copying?
The program car.c passes the data structure CarNodeto the PrintCar function by value.
A copy of each byte of CarNode must be made in thestack for each call of the function PrintCar.
We could, instead have passed the address of the copyof CarNode that we already had in the stack.
© 2006
Department of Computing Science
CMPUT 229
The car2.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; 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 <stdio.h>#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
00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92SP
180
184
186
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old 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
00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92SP
180
184
186
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old A6>
A088
© 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
92SP
180
184
186
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old A6>
A088
© 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
92SP
180
184
186
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old A6>
A088
next
cost
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
88
00
04
08
12
16
20
24
28
32
36
40
44
48
52
56
60
64
68
72
76
80
84
88
92SP
180
184
186
96
100
104
108
112116
120
124
128
132
136
140
144148
152
156
160
164
168
172
176
A6<old A6>
A088
next
cost
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+32successors’ 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 followingC 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+32successors’ 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*4D0 (A1);A2 A6+64+5*4(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+60D0 (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+32successors’ 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*4D0 0(A1);A2 A6+64+5*4(A2) D0;
move.l (A6,60), D0 move.l D0, (A6,84)
A1 A6+60D0 (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+32successors’ 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*4D0 0(A1);A2 A6+64+5*4(A2) D0;
move.l (A6,60), (A6,84)
A1 A6+60D0 (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 instructionsthat 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*4A2 (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 D0move (SP,44), A2cmp.b (A2)+, #0beq doneaddq.l #1, D0bra 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 #2names 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 D0move (SP,44), A2cmp.b (A2)+, #0beq doneaddq.l #1, D0bra 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
© 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_filesuccessors
names<ret adr>16
20
24
28
32
36
SP
8
© 2006
Department of Computing Science
CMPUT 229
The meaning of (*successor)[node]
int *vector;Int position;Int value;
vector[position] = value;
(*successor)[node] = temp;
successor
vector
int *vector;Int position;Int value;
*(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_filesuccessors
names<ret adr>16
20
24
28
32
36
SP
node
8
temp D0node 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_filesuccessors
names<ret adr>16
20
24
28
32
36
SP
node
8
temp D0node 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 ?
namesA d a m \0
J a m e \0s
C e c i \0l y
M u r r \0a 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
A d a m \0
J a m e \0s
C e c i \0l y
M u r r \0a y
Input_filesuccessors
names<ret adr>16
20
24
28
32
36
SP
8
temp D0node 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 4nodelsl.l #2, D1 # D1 4nodemove.l (A0,D1), A1 # A1 (names)[node] move.l A1, -(SP) # Push A1 into stack
A0
A d a m \0
J a m e \0s
C e c i \0l y
M u r r \0a y
Input_filesuccessors
names<ret adr>16
20
24
28
32
36
SP
8
temp D0node D1
© 2006
Department of Computing Science
CMPUT 229
Quiz #7
Write assembly for the following program.Assume that:
apple A6+12 ptr A6+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;Addq.l #4, (A6,12) apple apple + 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 }
ind
Stackapple
ptr
A6 0
4
8
12A0
A1
© 2006
Department of Computing Science
CMPUT 229
Quiz #7
Write assembly for the following program.Assume that:
apple A6+12 ptr A6+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;Addq.l #4, (A6,12) apple apple + 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 }
A6+8
Stackapple
ptr
A6 0
4
8
12A6+8A0
A1
© 2006
Department of Computing Science
CMPUT 229
Quiz #7
Write assembly for the following program.Assume that:
apple A6+12 ptr A6+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;Addq.l #4, (A6,12) apple apple + 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 }
A6+8
StackappleA6+12
A6 0
4
8
12A6+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+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;Addq.l #4, (A6,12) apple apple + 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 }
A6+8
Stack123
A6+12
A6 0
4
8
12A6+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+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;Addq.l #4, (A6,12) apple apple + 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 }
A6+12
Stack123
A6+12
A6 0
4
8
12A6+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+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;Addq.l #4, (A6,12) apple apple + 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 }
A6+12
Stack127
A6+12
A6 0
4
8
12A6+12
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+8ind A6+4
# 7 ind = &ptr;lea (A6,8), A0 A0 &ptr move.l A0, (A6,4) ind &ptr# 8 *ind = &apple;lea (A6,12), A0 A0 &applemove.l (A6,4), A1 A1 indmove.l A0, (A1) *ind &apple# 9 **ind = 123;move.l (A6,4), A0 A0 indmove.l (A0), A1 A1 *indmove.l #123, (A1) **ind 123# 10 ind++;addq.l #4, (A6,4) ind++# 11 (*ptr)++;move.l (A6,8), A0 A0 ptraddq.l #4, (A0) *ptr *ptr + 4# 12 apple++;qddq.l #4, (A6,12) apple apple + 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 }
A6+12
Stack131
A6+12
A6 0
4
8
12A6+12
A6+12
A0
A1