2011 ics middle examination
DESCRIPTION
2011 ICS Middle Examination. PROBLEM-1. Problem-1. Ref: section 2.2, 2.3 Conditions 6-bits machine Two’s complement arithmetic Signed integer Right shift is performed arithmetically Short is encoded using 3 bits. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; - PowerPoint PPT PresentationTRANSCRIPT
1
2011 ICS Middle Examination
PROBLEM-1
2
3
Problem-1
• Ref: section 2.2, 2.3• Conditions
– 6-bits machine– Two’s complement arithmetic– Signed integer– Right shift is performed arithmetically– Short is encoded using 3 bits
4
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9ux
y
usy
(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
9 = 001 001-9 = 110 110 + 1 = 110 111
5
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111ux
y
usy
(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
9 = 001 001-9 = 110 110 + 1 = 110 111
6
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
uxy
usy
(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
x = -3030 = 011 110-30 = 100 001 + 1 = 100 010ux = x = 100 010
7
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010y
usy
(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
x = -3030 = 011 110-30 = 100 001 + 1 = 100 010ux = x = 100 010
8
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
yusy
(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
sy = -4 short4 = 100-4 = 011 + 1 = 100y = sy = 111 100 sign extension
9
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100usy
(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
sy = -4 short4 = 100-4 = 011 + 1 = 100y = sy = 111 100 sign extension
10
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
sy = -4 short4 = 100-4 = 011 + 1 = 100usy = sy = 100
11
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100(x>>2)<<1
x + y
x & !y
UMax
TMax
-TMin
sy = -4 short4 = 100-4 = 011 + 1 = 100usy = sy = 100
12
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1x + y
x & !y
UMax
TMax
-TMin
x = 100 010x>>2 = 11100 010 = 111 000 arithmetically
(x>>2)<<1 = 111 0000 = 110 000
13
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000x + y
x & !y
UMax
TMax
-TMin
x = 100 010x>>2 = 11100 010 = 111 000 arithmetically
(x>>2)<<1 = 111 0000 = 110 000
14
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000
x + yx & !y
UMax
TMax
-TMin
x = 100 010y = 111 100
x + y = 100 010 + 111 100 = 1011 110 overflow
15
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000
x + y 011110x & !y
UMax
TMax
-TMin
x = 100 010y = 111 100
x + y = 100 010 + 111 100 = 1011 110 overflow
16
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000
x + y 011110
x & !yUMax
TMax
-TMin
x = 100 010y = 111 100
x & !y = 100 010 & 000 000 = 000 000
!y = 000 000
17
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000
x + y 011110
x & !y 000000UMax
TMax
-TMin
x = 100 010y = 111 100
x & !y = 100 010 & 000 000 = 000 000
!y = 000 000
18
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000
x + y 011110
x & !y 000000
UMaxTMax-TMin
UMax = 2^6 – 1 = 111 111 TMax = 2^5 – 1 = 011 111-TMin = -2^5 = -(100 000) = 011 111 + 1 = 100 000
19
Problem-1short sy = -4;unsigned short usy = sy;int y = sy;int x = -30;unsigned int ux = x;Expression Binary
Repr.-9 110111
ux 100010
y 111100
usy 100
(x>>2)<<1 110000
x + y 011110
x & !y 000000
UMax 111111TMax 011111-TMin 100000
UMax = 2^6 – 1 = 111 111 TMax = 2^5 – 1 = 011 111-TMin = -2^5 = -(100 000) = 011 111 + 1 = 100 000
PROBLEM-2
20
21
Problem-2
• Ref: lab-1• Conditions
– The coding rules are same as lab1– Legal ops: ! ~ & ^ | + << >>– Constants: <= 8 bits (0x0 ~ 0xFF)
22
Problem-2
/* [absValue] * calculate the abs value of x * ex: absValue(5) = 5 * absValue(-29) = 29 * legal ops: ~ & ^ | - << >> * #ops: 8 */int absValue (int x) {
/* fill your code */ return}
23
Problem-2
/* [absValue] * calculate the abs value of x * ex: absValue(5) = 5 * absValue(-29) = 29 * legal ops: ~ & ^ | - << >> * #ops: 8 */int absValue (int x) {
/* fill your code */ return}
if x >= 0 abs = x
if x < 0 abs = x^0xF.. + 1 = x^(-1)+1 = x^(-1)–(-1)
= x^(0)-(0)
abs = x^mask-maskmask = x>>31
24
Problem-2
/* [absValue] * calculate the abs value of x * ex: absValue(5) = 5 * absValue(-29) = 29 * legal ops: ~ & ^ | - << >> * #ops: 8 */int absValue (int x) {
int mask = x >> 31; abs = x ^ mask – mask; return abs;}
ex: x=5mask = 5>>31 = 0abs = 5^0 – 0 = 5
ex: x=-5= 0xF..FBmask = 0xF..FF abs = 0xF..FB ^ 0xF..FF - (-1) = 0x0..04 + 0x0..01 = 0x5
25
Problem-2
/* [swapBits] * swap n bits started from i * with n bits started from j * ex: swapBits(0x2F,1,5,3) = 0xE3 * legal ops: ~ & ^ | - << >> */int swapBits (int x, int i, int j, int n) {
int mask = (1 << ? ) - 1;int xorTemp = ((x >> ? ) ^ (x >> ? ))& ? ;return x ? ((xorTemp << i) ? (xorTemp << j)
}
26
Problem-2
x = 0x2F, i = 1, j = 5, n = 3 0x2F = 0010 1111swapBits(0x2F) = 1110 0011 = E3
x = 0x..A..B.. swapBits(x) = 0x..B..A..
A = A ^ B ^ B = (A ^ B) ^ B B = B ^ A ^ A = (B ^ A) ^ A
x = 0x..A..B.. ^ 0x..T..T.. (T = A ^ B)swapBits(x) = 0x..B..A..
27
Problem-2
x = 0x..A..B.. T = A ^ B = 0x0..0..A ^ 0x0..A..B & 0x0....0M {M = (1<<n)-1} = ((x >> j) ^ (x >> i)) & M
swapBits(x) = 0x..A..B.. ^ 0x..T..T.. {0x..T.. | 0x....T..} = x ^ ((T << j) | (T << i))
28
Problem-2
/* [swapBits] * swap n bits started from i * with n bits started from j * ex: swapBits(0x2F,1,5,3) = 0xE3 * legal ops: ~ & ^ | - << >> */int swapBits (int x, int i, int j, int n) {
int mask = (1 << n) - 1;int xorTemp = ((x >> i) ^ (x >> j))& mask;return x ^ ((xorTemp << i) | (xorTemp << j)
}
PROBLEM-3
29
30
Problem-3
• Ref: section 3.3, 3.4, 3.5• Conditions
– 32-bit little endian machine– 4 byte size and hex– Each operation take effect on the memory and
register
31
Problem-3
Operation Dest Valuesubl (%ebx),%eax
incl 4(%eax)
decl %ecx
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43210x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x8765 4421%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
32
Problem-3
Operation Dest Valuesubl (%ebx),%eax
incl 4(%eax)
decl %ecx
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43210x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x8765 4421%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
33
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax)
decl %ecx
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43210x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x8765 4421%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
34
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax)
decl %ecx
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43210x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
35
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43210x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
36
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
37
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0002%edx 0x0000 0008
38
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4)
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
39
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0x0000 00010x10C 0x0000 00020x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
40
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx)
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0x0000 00010x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
41
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx) 0x108 0xFFFFFFFE
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0x0000 00010x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
42
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx) 0x108 0xFFFFFFFE
andl (%eax,%ecx,8),%eax
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0xFFFF FFFE0x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
43
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx) 0x108 0xFFFFFFFE
andl (%eax,%ecx,8),%eax %eax 0x00000100
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0xFFFF FFFE0x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
44
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx) 0x108 0xFFFFFFFE
andl (%eax,%ecx,8),%eax %eax 0x00000100
leal 9(%eax,%ecx,2),%edx
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0xFFFF FFFE0x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
45
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx) 0x108 0xFFFFFFFE
andl (%eax,%ecx,8),%eax %eax 0x00000100
leal 9(%eax,%ecx,2),%edx %edx 0x10B
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0xFFFF FFFE0x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 0008
46
Problem-3
Operation Dest Valuesubl (%ebx),%eax %eax 0x00000100
incl 4(%eax) 0x104 0x87654322
decl %ecx %ecx 0x00000001
imull $4,0x100(%edx,%ecx,4) 0x10C 0x00000008
notl (%eax, %edx) 0x108 0xFFFFFFFE
andl (%eax,%ecx,8),%eax %eax 0x00000100
leal 9(%eax,%ecx,2),%edx %edx 0x10B
Address value0x100 0xFFFF FFFF0x104 0x8765 43220x108 0xFFFF FFFE0x10C 0x0000 00080x110 0x2234 76880x114 0x1234 5678
register value%eax 0x0000 0100%ebx 0x0000 0104%ecx 0x0000 0001%edx 0x0000 010B
PROBLEM-4
47
48
Problem-4
• Ref: section 3.3~3.6, 3.7• Conditions
– 32-bit little endian machine
49
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048324 <mystery>L1 8048324: 55L2 : 89 e5L3 : 83 ec 08L4 ? : 83 7d 08 00 00
50
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048324 <mystery>L1 8048324: 55L2 8048325: 89 e5L3 8048327: 83 ec 08L4 804832a: 83 7d 08 00 00
51
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
sub $0x8, %esp...mov %0x8(%ebp), %eaxsub $0x1, %eaxmov %eax, ? .call 8048324 <mystery>mov 0x8(%ebp), %edx...
52
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
sub $0x8, %esp...mov %0x8(%ebp), %eaxsub $0x1, %eaxmov %eax, (%esp).call 8048324 <mystery>mov 0x8(%ebp), %edxadd ? , %edxmov %edx, 0xfffffffc(%ebp)jmp 804834e <mystery+0x2a>...
53
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
sub $0x8, %esp...mov %0x8(%ebp), %eaxsub $0x1, %eaxmov %eax, (%esp)call 8048324 <mystery>mov 0x8(%ebp), %edxadd %eax, %edxmov %edx, 0xfffffffc(%ebp)jmp 804834e <mystery+0x2a>...
54
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
sub $0x8, %esp...mov %0x8(%ebp), %eaxsub $0x1, %eaxmov %eax, (%esp)call 8048324 <mystery>mov 0x8(%ebp), %edxadd %eax, %edxmov %edx, 0xfffffffc(%ebp)jmp 804834e <mystery+0x2a>... : mov $0x8(%ebp), %eax
. : mov %eax 0xfffffffc(%ebp) 804834e: mov ? , %eax : leave : ret
55
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
sub $0x8, %esp...mov %0x8(%ebp), %eaxsub $0x1, %eaxmov %eax, (%esp)call 8048324 <mystery>mov 0x8(%ebp), %edxadd %eax, %edxmov %edx, 0xfffffffc(%ebp)jmp 804834e <mystery+0x2a>... : mov $0x8(%ebp), %eax
. : mov %eax 0xfffffffc(%ebp) 804834e: mov 0xfffffffc(%ebp),
%eax : leave : ret
56
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L25 sub %$0x4, %espL26 mov ? , (%esp)L27 call 8048324 <mystery>...
57
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>...
58
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp%esp
After L3
59
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp%esp -0x4
After L3
60
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp %esp-0x4%esp -0x4
After L3
61
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp *%esp-0x4%esp -0x8
After L3
62
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp *%esp-0x4%esp -0xC
After L3
63
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp *%esp-0x4%esp -0x10
After L3
64
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp *%esp-0x4%esp -0x14
After L3
65
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp *%esp-0x14%esp -0x14
After L3
66
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp *%esp-0x14%esp -0x1C
After L3
67
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048353 <main>...L21 pushl $0xfffffffc(%ecx)L22 push %ebpL23 mov %esp, %ebpL24 push %ecxL25 sub %$0x4, %espL26 mov $0xA, (%esp)L27 call 8048324 <mystery>L1 push %ebpL2 mov %esp, %ebpL3 sub $0x8, %espL4...
%ebp 0xbf8ce638%esp 0xbf8ce5cc
Before L22
%ebp 0xbf8ce5b8%esp 0xbf8ce5b0
After L3
08048324 <mystery>...L8 mov %eax, (%esp)L9 call 8048348 <mystery>...L11 add %eax, %edxL12 mov %edx, 0xfffffffc(%ebp)L13 jmp L16...L16 mov 0xfffffffc(%ebp), %eaxL17 leave...
68
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048324 <mystery>...L8 mov %eax, (%esp)L9 call 8048348 <mystery>...L11 add %eax, %edxL12 mov %edx, 0xfffffffc(%ebp)L13 jmp L16...L16 mov 0xfffffffc(%ebp), %eaxL17 leave...
69
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048324 <mystery>...L8 mov %eax, (%esp)L9 call 8048348 <mystery>...L11 add %eax, %edxL12 mov %edx, 0xfffffffc(%ebp)L13 jmp L16...L16 mov 0xfffffffc(%ebp), %eaxL17 leave...
70
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
08048324 <mystery>...L8 mov %eax, (%esp)L9 call 8048348 <mystery>...L11 add %eax, %edxL12 mov %edx, 0xfffffffc(%ebp)L13 jmp L16...L16 mov 0xfffffffc(%ebp), %eaxL17 leave...
71
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
...L14 mov 0x8(%ebp)L15 mov %eax, 0xfffffffc(%ebp)L16 mov 0xfffffffc(%ebp), %eax
08048353 <main>...L27 call 8048324L28 add %0x4, %espL29 pop %ecxL30 pop %ebpL31 lea 0xfffffffc(%ecx), %espL32 ret
72
Problem-4int mystery(int i) { if(i != 0) return i + mystery(i-1) return i;}int main(void) { return mystery(10);}
Where is the return value for main function ?
PROBLEM-5
73
74
Problem-5
• Ref: section 3.3~3.6, 3.7• Conditions
– 32-bit little endian machine
75
Problem-5int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}int main(void) { int data[2] = {0x12345678, 0x9ABCDEF} int flag = 0x457823AB; byte_abs(data, 3);}
...80483a1: c7 45 f0 78 56 34 12 movl $0x12345678, -0x10(%ebp)80483a8: c7 45 ? movl $0x9ABCDEF, -0xc(%ebp)80483bf: c7 45 ? movl $0x457823AB, -0x8(%ebp)...
76
Problem-5int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}int main(void) { int data[2] = {0x12345678, 0x9ABCDEF} int flag = 0x457823AB; byte_abs(data, 3);}
...80483a1: c7 45 f0 78 56 34 12 movl $0x12345678, -0x10(%ebp)80483a8: c7 45 ? movl $0x9ABCDEF, -0xc(%ebp)80483bf: c7 45 ? movl $0x457823AB, -0x8(%ebp)...
77
Problem-5int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}int main(void) { int data[2] = {0x12345678, 0x9ABCDEF} int flag = 0x457823AB; byte_abs(data, 3);}
...80483a1: c7 45 f0 78 56 34 12 movl $0x12345678, -0x10(%ebp)80483a8: c7 45 f4 ef cd ab 09 movl $0x9ABCDEF, -0xc(%ebp)80483bf: c7 45 f8 ab 23 78 45 movl $0x457823AB, -0x8(%ebp)...
78
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp %esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
79
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx %esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
80
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx
%esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
81
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx
0x12345678
%esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
82
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx
0x09ABCDEF0x12345678
%esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
83
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx
0x457823AB0x09ABCDEF0x12345678
%esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
84
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx
0x457823AB0x09ABCDEF0x12345678
0x3%esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
85
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp Old %ebp%ecx
0x457823AB0x09ABCDEF0x12345678
0x3-
0x10(%ebp)%esp
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
86
Problem-5...804839a: pushl %ebp804839b: movl %esp, %ebp804839d: pushl %ecx804839e: subl $0x14, %esp80483a1: movl **, -0x10(%ebp)80483a8: movl **, -0xc(%ebp)80483af: movl **, -0x8(%ebp) 80483b6: movl $0x3, 0x4(%esp)80483be: leal -0x10(%ebp),%eax80483cl: movl %eax, (%esp)80483c4: call <byte_abs>...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
%ebp 0xbfb88bbc--
0x457823AB0x09ABCDEF0x12345678
0x3-- %esp
87
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB0x09ABCDEF0x12345678 ptr
b 0x3--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
88
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB0x09ABCDEF0x12345678 ptr
b 0x3--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
temp = 0x000000078 > 0
89
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB0x09ABCDEF ptr0x12345678
b 0x2--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
90
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB0x09ABCDEF ptr0x12345678
b 0x2--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
temp = 0xFFFFFFEF < 0
91
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB0x09ABCD11 ptr0x12345678
b 0x2--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
-temp = 0x11
92
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB ptr0x09ABCD110x12345678
b 0x1--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
93
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823AB ptr0x09ABCD110x12345678
b 0x1--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
temp = 0xFFFFFFAB < 0
94
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x45782355 ptr0x09ABCD110x12345678
b 0x1--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
-temp = 0x55
95
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc-- ptr
0x457823550x09ABCD110x12345678
b 0x0--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
96
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823550x09ABCD110x12345678
0x0--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}
97
Problem-5...80483c4: call <byte_abs>80483c9: ...
%ebp 0xbfb88bbc%esp --
Before mov %esp, %ebp
0xbfb88bbc--
0x457823550x09ABCD110x12345678
0x3--
int byte_abs(int *a, int b) { char *ptr = (char *)a; while(b>0) { int temp = (int)(*ptr); if(temp < 0) ptr[0] = (char)(-temp) ptr += 4 b --; }}