2011 ics middle examination

97
1 2011 ICS Middle Examination

Upload: adora

Post on 10-Feb-2016

28 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: 2011 ICS Middle Examination

1

2011 ICS Middle Examination

Page 2: 2011 ICS Middle Examination

PROBLEM-1

2

Page 3: 2011 ICS Middle Examination

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

Page 4: 2011 ICS Middle Examination

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

Page 5: 2011 ICS Middle Examination

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

Page 6: 2011 ICS Middle Examination

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

Page 7: 2011 ICS Middle Examination

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

Page 8: 2011 ICS Middle Examination

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

Page 9: 2011 ICS Middle Examination

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

Page 10: 2011 ICS Middle Examination

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

Page 11: 2011 ICS Middle Examination

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

Page 12: 2011 ICS Middle Examination

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

Page 13: 2011 ICS Middle Examination

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

Page 14: 2011 ICS Middle Examination

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

Page 15: 2011 ICS Middle Examination

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

Page 16: 2011 ICS Middle Examination

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

Page 17: 2011 ICS Middle Examination

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

Page 18: 2011 ICS Middle Examination

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

Page 19: 2011 ICS Middle Examination

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

Page 20: 2011 ICS Middle Examination

PROBLEM-2

20

Page 21: 2011 ICS Middle Examination

21

Problem-2

• Ref: lab-1• Conditions

– The coding rules are same as lab1– Legal ops: ! ~ & ^ | + << >>– Constants: <= 8 bits (0x0 ~ 0xFF)

Page 22: 2011 ICS Middle Examination

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}

Page 23: 2011 ICS Middle Examination

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

Page 24: 2011 ICS Middle Examination

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

Page 25: 2011 ICS Middle Examination

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)

}

Page 26: 2011 ICS Middle Examination

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

Page 27: 2011 ICS Middle Examination

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))

Page 28: 2011 ICS Middle Examination

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)

}

Page 29: 2011 ICS Middle Examination

PROBLEM-3

29

Page 30: 2011 ICS Middle Examination

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

Page 31: 2011 ICS Middle Examination

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

Page 32: 2011 ICS Middle Examination

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

Page 33: 2011 ICS Middle Examination

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

Page 34: 2011 ICS Middle Examination

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

Page 35: 2011 ICS Middle Examination

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

Page 36: 2011 ICS Middle Examination

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

Page 37: 2011 ICS Middle Examination

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

Page 38: 2011 ICS Middle Examination

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

Page 39: 2011 ICS Middle Examination

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

Page 40: 2011 ICS Middle Examination

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

Page 41: 2011 ICS Middle Examination

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

Page 42: 2011 ICS Middle Examination

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

Page 43: 2011 ICS Middle Examination

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

Page 44: 2011 ICS Middle Examination

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

Page 45: 2011 ICS Middle Examination

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

Page 46: 2011 ICS Middle Examination

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

Page 47: 2011 ICS Middle Examination

PROBLEM-4

47

Page 48: 2011 ICS Middle Examination

48

Problem-4

• Ref: section 3.3~3.6, 3.7• Conditions

– 32-bit little endian machine

Page 49: 2011 ICS Middle Examination

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

Page 50: 2011 ICS Middle Examination

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

Page 51: 2011 ICS Middle Examination

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

Page 52: 2011 ICS Middle Examination

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

Page 53: 2011 ICS Middle Examination

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

Page 54: 2011 ICS Middle Examination

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

Page 55: 2011 ICS Middle Examination

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

Page 56: 2011 ICS Middle Examination

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

Page 57: 2011 ICS Middle Examination

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

Page 58: 2011 ICS Middle Examination

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

Page 59: 2011 ICS Middle Examination

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

Page 60: 2011 ICS Middle Examination

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

Page 61: 2011 ICS Middle Examination

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

Page 62: 2011 ICS Middle Examination

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

Page 63: 2011 ICS Middle Examination

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

Page 64: 2011 ICS Middle Examination

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

Page 65: 2011 ICS Middle Examination

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

Page 66: 2011 ICS Middle Examination

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

Page 67: 2011 ICS Middle Examination

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

Page 68: 2011 ICS Middle Examination

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

Page 69: 2011 ICS Middle Examination

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

Page 70: 2011 ICS Middle Examination

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

Page 71: 2011 ICS Middle Examination

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

Page 72: 2011 ICS Middle Examination

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 ?

Page 73: 2011 ICS Middle Examination

PROBLEM-5

73

Page 74: 2011 ICS Middle Examination

74

Problem-5

• Ref: section 3.3~3.6, 3.7• Conditions

– 32-bit little endian machine

Page 75: 2011 ICS Middle Examination

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)...

Page 76: 2011 ICS Middle Examination

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)...

Page 77: 2011 ICS Middle Examination

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)...

Page 78: 2011 ICS Middle Examination

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

Page 79: 2011 ICS Middle Examination

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

Page 80: 2011 ICS Middle Examination

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

Page 81: 2011 ICS Middle Examination

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

Page 82: 2011 ICS Middle Examination

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

Page 83: 2011 ICS Middle Examination

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

Page 84: 2011 ICS Middle Examination

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

Page 85: 2011 ICS Middle Examination

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

Page 86: 2011 ICS Middle Examination

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

Page 87: 2011 ICS Middle Examination

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 --; }}

Page 88: 2011 ICS Middle Examination

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

Page 89: 2011 ICS Middle Examination

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 --; }}

Page 90: 2011 ICS Middle Examination

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

Page 91: 2011 ICS Middle Examination

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

Page 92: 2011 ICS Middle Examination

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 --; }}

Page 93: 2011 ICS Middle Examination

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

Page 94: 2011 ICS Middle Examination

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

Page 95: 2011 ICS Middle Examination

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 --; }}

Page 96: 2011 ICS Middle Examination

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 --; }}

Page 97: 2011 ICS Middle Examination

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 --; }}