Final Review 09
-
Upload
qwerty17327 -
Category
Documents
-
view
218 -
download
0
Transcript of Final Review 09
-
7/23/2019 Final Review 09
1/21
Question 1
int decode2(int x, int y, int z)
1: movl 16(%ebp), %eax2: movl 12(%ebp), %edx3: subl %eax, %edx4: movl %edx, %eax5: imull 8(%ebp), %edx6: sall $31, %eax7: sarl $31, %eax8: xorl %edx, %eax
Q. Write the corresponding C code
Answer:
int decode2(int x, int y, int z) {int t1 ! " #
int t2 x t1int t3 (t1 && 31) '' 31int t4 t3 t2return t4
}
-
7/23/2019 Final Review 09
2/21
Question 21: int absdi2(int x, int !) *2: int result3: i (x & !)4: result ! + x5: else6: result x + !7: return result8:
Corresponding assemby code
1: movl 8(%ebp), %edx2: movl 12(%ebp), %e-x3: movl %edx, %eax
4: subl %e-x, %eax5: -mpl %e-x, %eax6: ./e 037: movl %e-x, %eax8: subl %edx, %eax: 03
a. What s!btractions are per"ormed when x # y$ When x%& y$
b. 'n what way does this code deiate "rom the standard impementation o" an i"ese$
c. *sing C syntax (inc!ding goto+s), show the genera "orm o" this transation
d. What restrictions mustbe imposed on the !se o this transation to g!arantee that it has
the behaior speci"ied by the C code$
Answers:
a. when x # y it "irst comp!tes x y and then y x, when x %& y it comp!tes y x.
b. -he code "or the thenstatement is exec!ted !nconditionay.
c.
thenstatement
t & testexpression
i" (t)
goto done
esestatement
-
7/23/2019 Final Review 09
3/21
done:
Question 3:
witch statements are partic!ary tric/y to reerse engineer "rom the ob0ect code. 'n the
"oowing proced!re, the body o" the switch statement has been remoed:
1: int sit-prob(int x) *2: int result x3: sit- (x) *4: ill in -ode ere 5: 6: return result7:
1ig!re .3 shows the disassembed ob0ect code "or the proced!re. We are ony
interested in the part o" the code shown on ines 4 thro!gh 56. We can see on ine 4 that
parameter x (at o""set 3 reatie to 7ebp) is oaded into register 7eax, corresponding toprogram ariabe res!t. -he 8ea 9x9(7esi), 7esi instr!ction on ine 55 is a nop
instr!ction inserted to ma/e the instr!ction on ine 52 start on an address that is a
m!tipe o" 56.
-he 0!mp tabe resides in a di""erent area o" memory. *sing the deb!gger ;6w0x8048468: 0x080483d5 0x080483eb 0x080483d5 0x0x80483e0
0x8048478: 0x080483e5 0x080483e8
(gdb)
Assemby Code:1: 989483-9 &sit-prob':2: 89483-9: pus %ebp3: 89483-1: mov %esp,%ebp4: 89483-3: mov 9x8(%ebp),%eax5: 89483-6: lea 9x-e(%eax),%edx6: 89483-: -mp $9x5, %edx7: 89483--: .a 89483eb &sit-prob9x2b'8: 89483-e: .mp 9x8948468(,%edx,4)
: 89483d5: sl $9x2, %eax19: 89483d8: .mp 89483ee &sit-prob9x2e'11: 89483da: lea 9x9(%esi),%esi12: 89483e9: sar $9x2, %eax13: 89483e3: .mp 89483ee &sit-prob9x2e'14: 89483e5: lea (%eax, %eax, 2), %eax15: 89483e8: imul %eax, %eax16: 89483eb: add $9xa, %eax17: 89483ee: mov %ebp, %esp18: 894839: pop %ebp1: 894831: ret
-
7/23/2019 Final Review 09
4/21
29: 894832: mov %esi, %esi
;nser:int sit-prob(int x) * int result x
sit- (x) *-ase 59:-ase 52: result && 2brea
-
7/23/2019 Final Review 09
5/21
Question 4:t!pede stru-t *
int letastru-t a=>?@Aint ri/t
bstru-t
void test(int i, bstru-t bp) * astru-t ap Bbp"'a=iA int n bp"'let bp"'ri/t ap"'x=ap"'idxA n
*n"ort!natey, the 8.h "ie de"ining the compietime constant C?- and the str!ct!re
a@str!ct are in "ies "or which yo! do not hae access priieges. 1ort!natey yo! hae
access to the a 8.o ersion o" the code, which yo! are abe to disassembe with theob0d!mp program, yieding the disassemby shown beow.
9 99999999&test'
5 9: pus %ebp
2 1: mov %esp, %ebp
3: pus %ebx
4 4: mov 9x8(%ebp), %eax
7: mov 9x-(%ebp), %e-x
6 a: lea (%eax, %eax, 4), %eax
B d: lea 9x4(%e-x, %eax, 4), %eax3 11: mov (%eax), %edx
13: sl $9x2, %edx
59 16: mov 9xb8(%e-x), %ebx
55 1-: add (%e-x), %ebx
52 1e: mov %ebx, 9x4(%edx, %eax, 1)
5 22: pop %ebx
54 23: mov %ebp, %esp
5 25: pop %ebp
56 26: ret
-
7/23/2019 Final Review 09
6/21
Answers:
a. C?- is
b.t!pede stru-t * int idx
int x=4A astru-t
-
7/23/2019 Final Review 09
7/21
Question 5:union ele *
stru-t *int pint !
e1stru-t *
int xunion ele next
e2
-his decaration i!strates that str!ct!res can be embedded within !nions.
-he "oowing proced!re (with some expressions omitted) operates on a in/ed ist
consisting o" these !nions as ist eements:
void pro- (union ele up)*
up C (up C ) " up C
a. What wo!d be the o""sets (in bytes) o" the "oowing "ieds:
e5.p:
e5.y:
e2.x:
e2.next:
b. Dow many tota bytes wo!d the str!ct!re reE!ire$
c. -he compie generates the "oowing assemby code "or proc:
1 movl 8(%ebp), %eax2 movl 4(%eax), %edx3 movl (%edx), %e-x
4 movl %ebp, %esp5 movl 8(%eax), %eax6 movl 8(%e-x), %e-x7 subl %eax, %e-x8 movl %e-x, 4(%edx)
*se this code to "i in the ban/s "or the C so!rce. -here is ony one answer that does
not per"orm any casting and does not ioate any type constraints.
-
7/23/2019 Final Review 09
8/21
Answers:
-his probem ery ceary shows that !nions are simpy a way to associate m!tipe
names (and types) with a singe storage ocation.
a.
-
7/23/2019 Final Review 09
9/21
Question 6:
-his E!estion is testing yo!r !nderstanding o" the stac/ "rame str!ct!re.
a. -he "oowing memory image embeds a binary tree with root node at 0x804961c.
Hease draw the ogica organization o" the tree in the same "ormat as the shown
exampe. Hease indicate the address and /ey a!e (in hexidecima) o" a the tree nodesand the pointers "rom parent nodes to chid nodes. -he decaration o" the tree node
str!ct!re is as "oows.
struct tree_node {
int key;
struct tree_node *left;
struct tree_Node *right;
};
/* address of the root */
tree_node *root;
Memory:
0x80495f8: 0x0000000c
0x80495fc: 0x00000000
0x8049600: 0x00000000
0x8049604: 0x0000001f
0x8049608: 0x080495f8
0x804960c: 0x08049610
0x8049610: 0x00000022
0x8049614: 0x00000000
0x8049618: 0x000000000x804961c: 0x00000037
0x8049620: 0x08049604
0x8049624: 0x08049628
0x8049628: 0x0000003c
0x804962c: 0x00000000
0x8049630: 0x08049634
0x8049634: 0x0000004e
0x8049638: 0x00000000
0x804963c: 0x00000000
1: struct tree_node * search(struct tree_node * node, int value)
2: {3: if (node->key == value)
4: return node;
5: else if (node->key > value) {
6: if (node->left == NULL)
7: return NULL;
8: else
9: return search(node->left, value);
10: } else {
11: if (node->right == NULL)
12: return NULL;
-
7/23/2019 Final Review 09
10/21
13: else
14: return search(node->right, value);
15: }
16: }
b. !ppose we ca search(root, 9x4e). 1i in the ban/s the a!e o" these memory
ocation so that it shows the stac/ when the exec!tion is at ine 4. (Iore space than
needed is proided. ) Jo! can ass!me that the stac/ stores ony arg!ments, ret!rnaddress, and the ebp register a!e. -he a!e o" ebp is 9xb""""339 when the program
cas the "!nction. Write rtn addr "or ret!rn addresses.
Address Ka!e9xb899 9x4e9xb7- 9x89461-9xb78 rtnaddr9xb74
9xb799xb7e-9xb7e89xb7e49xb7e99xb7d-9xb7d89xb7d49xb7d99xb7--
9xb7-89xb7-4
Answers:
a. (0x804961c/0x37)
/ \
(0x8049604/0x1f) (0x8049628/0x3c)
/ \ \
(0x80495f8/0xc) (0x8049610/0x22) (0x8049634/0x4e)
b.
0xbffff800 0x4e
0xbffff7fc 0x804961c
0xbffff7f8 rtn_addr
0xbffff7f4 0xbffff880
0xbffff7f0 0x4e
-
7/23/2019 Final Review 09
11/21
0xbffff7ec 0x8049628
0xbffff7e8 rtn_addr
0xbffff7e4 0xbffff7f4
0xbffff7e0 0x4e
0xbffff7dc 0x8049634
0xbffff7d8 rtn_addr
0xbffff7d4 0xbffff7e4
Question 7:
a.
Which o" the "oowing x36 instr!ctions can be !sed to add two registers and store the
res!t witho!t oerwriting either o" the origina a!es$
(a) mo
(b) add
(c) ea
(d) ?one o" aboe
b.
-he register rax is c!rrenty storing a ?*FF pointer. Which o" the "oowing x36
instr!ctions wi ca!se a segmentation "a!t beca!se o" an inaid memory access$
(a) mo (7rax), 7rcx
(b) ea (7rax), 7rcx
(c) ?one o" the aboe
c. A programmer wishes to compare the contents o" a string caed my@str to the string8;L-. he writes the "oowing C code:
i (m!str DEF@D) 000
Which o" the "oowing appy$
(a) my str is a pointer to the "irst character o" a string in memory
(b) my str is the AC'' a!e o" the "irst character o" a string in memory
(c) my str is a register containing a o" the characters in the string
(d) 8;L- wi compie to a pointer to a string in memory
(e) 8;L- wi compie to the AC'' a!e "or the etter ;
(") 8;L- wi compie to a register containing the string 8;L- represented as an
integer
(g) -he comparison wi aways wor/ as expected
(h) -he comparison wi not necessariy wor/ as expected
(i) -he comparison itse" wi ca!se the program to crash
-
7/23/2019 Final Review 09
12/21
d. -he "!nction foo() is decared in a C program as "oows: void foo(int
int_param, char *str_param);
A programmer cas foo() "rom within the "!nction bar() as "oows:
foo(my_int, my_string);
Which o" the "oowing is>are tr!e:
(a) '" foo() changes the a!e o" int param, the change wi propagate bac/ to the
caing "!nctionbar(), in other words, the a!e o" my int wi aso change.
(b) '" foo() changes the second character o" str param, the change wi propagate
bac/ to the caing "!nction bar(), in other words, the second character o" my
string wi aso change.
(c) '" foo() changes the address o" str param to point to a di""erent string, the
change wi propagate bac/ to the caing "!nction bar(), in other words, my string
wi now point to a di""erent string.
(d) ?one o" the aboe.
e.A programmer has decared an array in a C program as "oows:
int my_array[100];
Which o" the "oowing gie(s) the address o" the eighth eement in the array (bearing in
mind that the "irst eement in the array is at index zero):
-
7/23/2019 Final Review 09
13/21
(a) my array[7]
(b) &my array[7]
(c) my array + 7
(d) my array + 28
(e) ?one o" the aboe
g. A programmer has stored an 3bit a!e in memory. -he pointer:
char GptrM
points to the ocation where it is stored. De or she now wants to retriee the a!e and
store it into the ariabe:
int a!eM
Which o" the "oowing (i" any) wi achiee this propery$
(a) a!e & ptrM
(b) a!e & GptrM
(c) a!e & (int)ptrM
(d) a!e & (int G)ptrM
(e) a!e & G(int G)ptrM
(") ?one o" the aboe
h. ;ien code "or an impicit ist memory aocator, yo! caim yo! can improe
per"ormance by creating a in/ed ist o" "ree boc/s. Why does this hep increase
per"ormance$
(a) -raersing a in/ed ist is signi"icanty "aster than moing "rom boc/ to boc/ in the
impicit ist.
(b) -he impicit ist had to inc!de eery boc/ in memory, b!t the in/ed ist co!d 0!st
inc!de the "ree
-
7/23/2019 Final Review 09
14/21
boc/s.
(c) -he compier /nows how to optimize the code "or a in/ed ist by !nroing oops,
b!t wasnNt abe to do
this "or the impicit ist.
(d) Daing a in/ed ist made coaescing signi"icanty "aster.
(e) ?one o" the aboe.
Answer(s):
Answers:
-
7/23/2019 Final Review 09
15/21
Question 8: Synhroni!ation
A m!tithreaded program has two goba data str!ct!res that wi be shared among the
threads. -he data str!ct!res are not necessariy accessed at the same time. Which o" the
"oowing is>are tr!e (i" any)$
(a) '" the program has ony one semaphore, and threads ca P on that singe semaphore
be"ore !sing either
o" the data str!ct!res, the code wi not wor/ correcty.
(b) Daing one semaphore wi wor/, b!t haing two, one per shared data str!ct!re, may
aow "or increased
per"ormance.
(c) '" the machine has ony one processor, ony one o" the threads can r!n at a time, so
semaphores are notnecessary in that case.
(d) ?one o" the aboe.
"orret answer(s):
Answers:
-
7/23/2019 Final Review 09
16/21
Question #: Synhroni!ation
A barbershop consists o" n waiting chairs and the one barber chair. '" there are no
c!stomers, the barber waits. '" a c!stomer enters, and a the waiting chairs are occ!pied,
then the c!stomer eaes the shop. '" the barber is b!sy, b!t waiting chairs are aaiabe,
then the c!stomer sits in one o" the "ree waiting chairs. We proide yo! the s/eeton
code witho!t any synchronization.extern int ?
int -ustomers 9
void -ustomer() *
i (-ustomers ' ?) *
return ?G
-ustomers 1
/etHair>ut()
-ustomers " 1
return ?G
void barber() *ile (1) *
-utHair()
O!r so!tion !ses threebinary sempahores:mutex: to ontro$ aess to the g$oba$ %ariab$e ustomers
-ustomer: to signa$ a ustomer is in the sho&
barber: to signa$ the barber is busy
a. What are the initia a!es "or the three semaphores$
b. Compete the aboe witho!t changing A?J existing code b!t ony by "iing in as
many copies o" the "oowing ines as yo! need.
I(Bmutex)J(Bmutex)I(B-ustomer)J(B-ustomer)I(Bbarber)J(Bbarber)
-
7/23/2019 Final Review 09
17/21
Answers:
Dere is GoneG so!tion, "or which the initia a!es are m!tex & 5 (ariabe c!stomers may be
accessed), c!stomer & 9 (no c!stomers) and barber & 9 (barber is not b!sy).oidG c!stomer() {
H(Pm!tex)M
i" (c!stomers % ?) {K(Pm!tex)M
ret!rn ?*FFM
c!stomers R& 5M
K(Pm!tex)M
K(Pc!stomer)M
H(Pbarber)M
getDairC!t()M
H(Pm!tex)M
c!stomers & 5MK(Pm!tex)M
ret!rn ?*FFM
oidG barber() {
whie(5) {H(Pc!stomer)M
K(Pbarber)M
c!tDair()M
-
7/23/2019 Final Review 09
18/21
Question 1': Synhroni!ation 2
Consider the "oowing three threads and semaphores
Knitiali#in/ semapores
s1 1
s2 9
s3 9
Knitiali#e x
x 9
void tread1() *
x x 1
void tread2() *
x x 2
void tread3() *
x x 2
Add H() and K() cas (and change nothing ese) to the three threads s!ch that at the end
o" any seE!ence o" conc!rrent exec!tion o" the three threads, x wi aways be & 6.
-
7/23/2019 Final Review 09
19/21
Answer
Knitiali#in/ semapores
s1 1
s2 9s3 9
Knitiali#e x
x 9
void tread1() * I(s1)
x x 1
J(s1)
J(s2)
void tread2() * I(s1)
x x 2
J(s1)J(s3)
void tread3() * I(s2) I(s3)
x x 2
J(s3)
J(s2)
-
7/23/2019 Final Review 09
20/21
Question 11: Synhroni!ation 3
Consider the "oowing three threads and semaphores
Knitiali#in/ semapores
s1
s2
s3
s4
Knitiali#e x
x 1
void tread1() * ile (x L 369) *
x x 2
exit(9)
void tread2() * ile (x L 369) *
x x 3
exit(9)
void tread3() * ile (x L 369) *
x x 5
exit(9)
Hroide initia a!es "or the "o!r semaphores and add H(), K() semaphore operations
(!sing the "o!r semaphores) in the code "or thread 5, 2 and s!ch that the process is
g!aranteed to terminate.
-
7/23/2019 Final Review 09
21/21
Answer Knitiali#in/ semapores
s1 3
s2 2
s3 1s4 1
Knitiali#e x
x 1
void tread1() * ile (x L 369) * I(s1) I(s4) x x 2
J(s4) exit(9)
void tread2() * ile (x L 369) * I(s2) I(s4) x x 3
J(s4) exit(9)
void tread3() * ile (x L 369) * I(s3) I(s4) x x 5
J(s4) exit(9)