DOC PREVIEW
CMU CS 15213 - Recitation

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

15213 Recitation Section C Shimin Chen Sept 23 2002 Outline Last week s exercise Function and stack Array Struct and linked list Last Week s Final Example int func5 int x 0x80483c0 0x80483c1 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 0x80483d9 0x80483da 15213 Recitation C push mov mov xor xor cmp jge mov add inc cmp jl mov pop ret ebp esp ebp 0x8 ebp ecx eax eax edx edx ecx edx 0x80483d7 esi esi edx eax edx ecx edx 0x80483d0 ebp esp ebp 2 Body Shimin Chen Write Comments int func5 int x 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 15213 Recitation C mov xor xor cmp jge mov add inc cmp jl 0x8 ebp ecx eax eax edx edx ecx edx 0x80483d7 esi esi edx eax edx ecx edx 0x80483d0 3 ecx x eax 0 edx 0 if edx x goto L1 nop L2 eax edx edx if edx x goto L2 L1 Shimin Chen Name the variables eax result edx i 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 15213 Recitation C mov xor xor cmp jge mov add inc cmp jl 0x8 ebp ecx eax eax edx edx ecx edx 0x80483d7 esi esi edx eax edx ecx edx 0x80483d0 4 ecx x result 0 i 0 if i x goto L1 L2 result i i if i x goto L2 L1 Shimin Chen Loop result 0 i 0 if i x goto L1 L2 result i i if i x goto L2 L1 15213 Recitation C result 0 i 0 if i x goto L1 do result i i while i x L1 result 0 i 0 While i x result i i result 0 for i 0 i x i result i 5 Shimin Chen C Code int func5 int x int result 0 int i for i 0 i x i result i return result 15213 Recitation C 6 Shimin Chen Stack Basics push Decreasing Addresses decrement esp then places value pop get value then increment esp Stack Pointer esp 15213 Recitation C 7 Stack Grows Down Stack Top Shimin Chen Function Stack Frames A caller function calls a callee function Caller Frame Arguments Frame Pointer ebp Return Addr Old ebp Saved Registers Local Variables Stack Pointer esp 15213 Recitation C 8 Argument Build Shimin Chen Making a Call Caller push arguments in what order call put return address onto stack jump to the start of callee function Callee save caller s ebp eb p set up stack frame save callee saved registers if want to use ebx esi edi put return value in eax restore ebp and esp ret to jump to the Return Addr esp 15213 Recitation C 9 Arguments Return Addr Old ebp Saved Registers Local Variables Argument Build Shimin Chen Example 1 Please draw the stack at the marked points Write C code for the assembly code gdb x s 0x8048478 0x8048478 IO stdin used 4 d n int example 1 int x int y 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 10 2 Stack Shimin Chen ASM of main 0x80483f4 0x80483f5 0x80483f7 0x80483fa 0x80483fd 0x80483ff 0x8048401 0x8048406 0x8048409 0x804840a 0x804840f 0x8048414 0x8048416 0x8048418 0x8048419 15213 Recitation C push mov sub add push push call add push push call xor mov pop ret ebp esp ebp 0x8 esp 0xfffffff8 esp 0x2 0x1 0x80483e4 example 1 0xfffffff8 esp eax 0x8048478 0x8048308 printf eax eax ebp esp ebp 11 1 Stack 3 Stack Shimin Chen Stack at Point 1 main 0x80483f4 push 0x80483f5 mov 0x80483f7 sub 0x80483fa add 0x80483fd push 0x80483ff push 0x8048401 call example 1 0x8048406 15213 Recitation C ebp esp ebp 0x8 esp 0xfffffff8 esp 0x2 0x1 0x80483e4 ebp 2 esp 12 old ebp 1 Shimin Chen Stack at Point 2 old ebp example 2 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 2 1 0x8048406 ebp esp 15213 Recitation C 13 main ebp Shimin Chen Stack at Point 3 main 0x80483ff 0x8048401 0x8048406 example 2 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C call 0x80483e4 example 1 push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 14 ebp old ebp 2 esp 1 0x8048406 main ebp Shimin Chen Write Comments int example 1 int x int y 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 15 eax y eax x Shimin Chen main 0x80483f4 0x80483f5 0x80483f7 0x80483fa 0x80483fd 0x80483ff 0x8048401 0x8048406 0x8048409 0x804840a 0x804840f 0x8048414 0x8048416 0x8048418 0x8048419 15213 Recitation C push mov sub add push push call add push push call xor mov pop ret ebp esp ebp 0x8 esp 0xfffffff8 esp 0x2 0x1 0x80483e4 example 1 0xfffffff8 esp eax 0x8048478 0x8048308 printf eax eax ebp esp ebp 16 example 1 1 2 printf d n result example 1 return 0 Shimin Chen C Code int example 1 int x int y return x y int main int result result example 1 1 2 printf d n result return 0 15213 Recitation C 17 Shimin Chen Example 2 Recursion Please write C code for the assembly code Draw the stack changes of calling example 2 3 int example 2 int x 0x8048420 0x8048421 0x8048423 0x8048426 0x8048427 0x8048428 0x804842b 0x804842e 0x8048430 0x8048435 15213 Recitation C push mov sub push push mov cmp jg mov jmp ebp esp ebp 0x10 esp esi ebx 0x8 ebp ebx 0x2 ebx 0x8048437 0x1 eax 0x8048453 18 Shimin Chen Example 2 Cont d 0x8048437 0x804843a 0x804843d 0x804843e 0x8048443 0x8048445 0x8048448 0x804844b 0x804844c 0x8048451 0x8048453 0x8048456 0x8048457 0x8048458 0x804845a 0x804845b 15213 Recitation C add lea push call mov add lea push call add lea pop pop mov pop ret 0xfffffff4 esp 0xfffffffe ebx eax eax 0x8048420 example 2 eax esi 0xfffffff4 esp 0xffffffff ebx eax eax 0x8048420 example 2 esi eax 0xffffffe8 ebp esp ebx esi ebp esp ebp 19 Shimin Chen Stack Frame example 2 0x8048420 0x8048421 0x8048423 0x8048426 0x8048427 push mov sub push push ebp esp ebp 0x10 esp esi ebx lea pop pop mov pop ret 0xffffffe8 ebp esp ebx esi ebp esp ebp x rtrn addr ebp old ebp 0x8048453 0x8048456 0x8048457 0x8048458 0x804845a 0x804845b 15213 Recitation C 20 old esi esp old ebx Shimin Chen Write Comments For Body 0x8048428 0x804842b 0x804842e 0x8048430 0x8048435 0x8048437 0x804843a 0x804843d 0x804843e 0x8048443 0x8048445 0x8048448 0x804844b 0x804844c 0x8048451 0x8048453 15213 Recitation C mov cmp jg mov jmp add lea push call mov add lea push call add 0x8 ebp ebx 0x2 ebx 0x8048437 0x1 eax 0x8048453 0xfffffff4 esp 0xfffffffe ebx eax eax 0x8048420 example 2 eax esi 0xfffffff4 esp 0xffffffff ebx eax eax 0x8048420 example 2 esi eax 21 ebx x if x 2 goto L1 eax 1 goto L2 L1 push x 2 example 2 esi eax push x 1 example 2 eax esi L2


View Full Document

CMU CS 15213 - Recitation

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

lecture

lecture

37 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

47 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download Recitation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Recitation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Recitation and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?