Ge-ng at things on the chip you can t easily reach from C

Size: px
Start display at page:

Download "Ge-ng at things on the chip you can t easily reach from C"

Transcription

1 chapter 3 part 2 1

2 Ge-ng at things on the chip you can t easily reach from C 2

3 CPUID CPUID example from Blum, Professional Assembly Language. Programmer to Programmer. Very good maybe lidle bit dated Available in PB and PDF 3

4 CPUID instruchon Introduced in the early 90s late 486s, PenHums Why? find out what chip type, know feature set Controversy? Processor Serial Number ophon. Disabled. 4

5 1 /* file cpuid.s */ 2.section.data 3 output: CPUID.S 4.ascii "The processor Vendor ID is xxxxxxxxxxxx \n" 5.section.text 6.globl _start 7 _start: 8 movl $0, %eax 9 cpuid 10 movl $output, %edi 11 movl %ebx, 28(%edi) 12 movl %edx, 32(%edi) 13 movl %ecx, 36(%edi) 14 movl $4, %eax 15 movl $1, %ebx 16 movl $output, %ecx 17 movl $42, %edx 18 int $0x80 19 movl $1, %eax 20 movl $0, %ebx 21 int $0x80 5

6 calling a funchon push args onto the stack in reverse order call fname return value will be in %eax 6

7 1 /* file cpuid2.s */ 2.section.data 3 output: CPUID2.S 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer, %edi 13 movl %ebx, (%edi) 14 movl %edx, 4(%edi) 15 movl %ecx, 8(%edi) 16 pushl $buffer 17 pushl $output 18 call printf 19 addl $8, %esp 20 pushl $0 21 call exit 7

8 assembling, linking, running our normal way: as o fname.o fname.s ld o fname fname.o when we re using C library funchons: as o fname o fname.s ld - - dynamic- linker /lib/ld- linux.so.2 - lc o fname fname.o 8

9 Where do I learn about CPUID? or any other operahon? Intel IA- 32, Intel64 manuals are available online for free from Intel hdp:// Volume 2. InstrucHon set reference details on every instruchon, including CPUID 9

10 Aside: running assembly under GDB Recall that when we compiled C programs to run under GDB, we used g switch For assembly programs, we use - gstabs 10

11 se-ng a breakpoint simple way: *label+offset e.g., *_start *_start+1 GDB Bug: misses breakpoint at *_start Workaround: insert nop 11

12 other useful GDB commands p $regname info locals info frame info registers 12

13 examining the stack with GDB info registers info frame p $esp p *(int*)$esp Just what it looks like take what s in %esp treat it as an int* print what it points to p *(int*)($esp+4) if you forget the ( ), you ll add 4 to *(int*)$esp x/nuf address N number u units f format example x/20bx $esp 13

14 disassembling in GDB disas disassembles current funchon disas sum disassemble the sum funchon disas addr disassemble funchon at addr disas ad 1 ad 2 disas between addr ad 1, ad 2 14

15 How to do in assembly things you commonly do in C 15

16 some things that you know in C assignment simple arithmehc operahons funchons condihonals loops 16

17 some things that you know in C assignment simple arithmehc operahons funchons condihonals loops 17

18 assignment int x, y;... x=y; 18

19 assignment C GAS int x, y;... movl %ebx, %eax x=y; 19

20 assignment C GAS int x, y;... x=y; movl %ebx, %eax remember: in GAS this is like %eax=%ebx, not vice versa l specifies size mov{b,w,l,q} 20

21 more on sizes C declaration intel data type GAS su x, e.g., for movx instruction char byte b 1 short word w 2 int double word l 4 unsigned double word l 4 long int double word l 4 unsigned long double word l 4 char * double word l 4 float single precision s 4 double double precision l 8 long double extended precision t 10/12 x86-32 size (bytes) 21

22 with simple constants in C x=35; GAS movl $35, %eax in assembly, $35 is called an immediate value 22

23 assignment with different widths In C, if we have: int x = 0x ; char y = 0xFF;... x=y; what s the value of x aker the assignment? 23

24 What happens? What do we expect to find in %eax? movl movb $0x7FFFFFFF, %eax $0x6, %al Tempted to think 0x

25 What happens? What do we expect to find in %eax? movl movb $0x7FFFFFFF, %eax $0x6, %al Tempted to think 0x , but we get 2,147,483,398 or 0x7FFFFF06 25

26 example. what do we get? 1.section.rodata 2 print_str: 3.string "x=0x%x\n" 4.text 5.globl main 6.type 7 main: 8 pushl %ebp 9 movl %esp, %ebp 10 movl $0x , %eax 11 movl $print_str, %ecx 12 pushl %eax 13 pushl %ecx 14 call printf 15 addl $8, %esp 16 movl $0, %eax 17 leave 18 ret 1.section.rodata 2 print_str: 3.string "x=0x%x\n" 4.text 5.globl main 6.type 7 main: 8 pushl %ebp 9 movl %esp, %ebp 10 movl $0x , %eax 11 movb $0xFF, %al 12 movl $print_str, %ecx 13 pushl %eax 14 pushl %ecx 15 call printf 16 addl $8, %esp 17 movl $0, %eax 18 leave 19 ret 26

27 example. what do we get? 1.section.rodata 2 print_str: 3.string "x=0x%x\n" 4.text 5.globl main 6.type 7 main: 8 pushl %ebp 9 movl %esp, %ebp 10 movl $0x , %eax 11 movl $print_str, %ecx 12 pushl %eax 13 pushl %ecx 14 call printf 15 addl $8, %esp 16 movl $0, %eax 17 leave 18 ret prints 0x section.rodata 2 print_str: 3.string "x=0x%x\n" 4.text 5.globl main 6.type 7 main: 8 pushl %ebp 9 movl %esp, %ebp 10 movl $0x , %eax 11 movb $0xFF, %al 12 movl $print_str, %ecx 13 pushl %eax 14 pushl %ecx 15 call printf 16 addl $8, %esp 17 movl $0, %eax 18 leave 19 ret prints 0x12345FF 27

28 movb, movzbl, movsbl movb moves a byte to deshnahon What if we want to move 1 byte to 32 bit reg? movzbl expand to 32 bits fill in the LHS with 24 0s movsbl expand to 32 bits fill in LHS with 24 copies of the ms bit of src operand Think back to C: movzbl does zero extension movsbl does sign extension 28

29 another try 1.section.rodata 2 print_str: 3.string "x=0x%x\n" 4.text 5.globl main 6.type 7 main: 8 pushl %ebp 9 movl %esp, %ebp 10 movl $0x , %eax 11 movb $0xFF, %dl 12 movzbl %dl, %eax 13 movl $print_str, %ecx 14 pushl %eax 15 pushl %ecx 16 call printf 17 addl $8, %esp 18 movl $0, %eax 19 leave 20 ret 21.size main,.-main 29

30 another try prints: x=0xff 1.section.rodata 2 print_str: 3.string "x=0x%x\n" 4.text 5.globl main 6.type 7 main: 8 pushl %ebp 9 movl %esp, %ebp 10 movl $0x , %eax 11 movb $0xFF, %dl 12 movzbl %dl, %eax 13 movl $print_str, %ecx 14 pushl %eax 15 pushl %ecx 16 call printf 17 addl $8, %esp 18 movl $0, %eax 19 leave 20 ret 21.size main,.-main 30

31 C pointers suppose that register %eax contains 1000 movl $500, %eax in register %eax replaces 1000 with 500 movl $500, (%eax) puts the value 500 into memory address 1000 think of ( ) around registers to be something like the C dereference operator * 31

32 assignments: moving data we can move things from: immediate to anything memory to a register register to memory 32

33 moving data. examples. src dst assembly example C equiv imm reg movl $0x52, %eax x=0x52; imm mem movl $0x52, (%eax) *p=0x52; reg reg movl %ebx, %eax a=b; reg mem movl %eax, (%ebx) *p=x; mem reg movl (%eax), %ebx x=*p; mem mem can t be done *p=*q; 33

34 C pointer arithmehc given: int *ip; char *cp = (char*)ip; if ip has the address 1000, what is: ip+1 cp+1 34

35 given: int *ip; C pointer arithmehc char *cp = (char*)ip; if ip has the address 1000: var addr ip 1000 cp 1000 cp ip cp+x 1000+x ip+x 1000+x*sizeof(int) 35

36 assembly pointer arithmehc if %eax contains 1000 movl $555, %eax replaces 1000 with the number 555. movl $555, (%eax) puts the number 555 into address 1000 movl $555, 4(%eax) puts the number 555 into address 1004 in this case, 4 is our displacement or offset 36

37 example: from Bryant and O Hallaron void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up Body Finish 37

38 Understanding Swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Offset yp xp Rtn adr Stack (in memory) 0 Old %ebp %ebp Register Value %ecx yp %edx xp %eax t1 %ebx t0-4 Old %ebx movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx 38

39 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 0x104 yp xp Offset 12 %ebp 0 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

40 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 0x120 0x104 yp xp Offset 12 %ebp 0 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

41 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 0x124 0x120 0x104 yp xp Offset %ebp movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

42 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 456 0x124 0x120 0x104 yp xp Offset %ebp movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

43 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 456 0x124 0x x104 yp xp Offset %ebp movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

44 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap x124 0x x104 yp xp Offset %ebp movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

45 %eax %edx %ecx %ebx %esi %edi %esp %ebp Understanding Swap 456 0x124 0x x104 yp xp Offset %ebp movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp 0x120 0x124 Rtn adr movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx x y 123 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100

46 more memory access modes assembly exp x(%eax) (%eax, %ebx) x(%eax, %ebx) (,%eax,s) x(,%eax,s) (%eax,%ebx,s) x(%eax,%ebx,s) resulting address %eax+x %eax+%ebx x+%eax+%ebx (%eax*s) x+(%eax*s) %eax+(%ebx*s) x+%eax+(%ebx*s) 46

47 Why? Arrays, etc. given int A[5]; what is &A[2]? 47

48 Why? Arrays, etc. given int A[5]; what is &A[2]? the address of A[0] + 2*sizeof(int) What is (%eax,%edx,4) if: %eax contains address of A[0] %edx contains 2 48

49 book complete list of operand forms type form operand value name immediate $imm imm immediate register E a R[E a ] register memory imm M[imm] absolute memory (E a ) M[R[E a ]] indirect memory imm(e b ) M[imm+R[E b ]] base+displacement memory (E b,e i ) M[R[E b ]+R[E i ]] indexed memory imm(e b,e i ) M[imm+R[E b ]+R[E i ]] indexed memory (,E i,s) M[R[E i ]*s] scaled indexed memory imm(,e i,s) M[imm+R[E i ]*s] scaled indexed memory (E b,e i,s) M[R[E b ]+R[E i ]*s] scaled indexed memory imm(e b,e i,s) M[imm+R[E b ]+R[E i ]*s] scaled indexed E a some register a R[E a ] what s in register a M[x] value at memory address x 49

50 why something like (E b,e i,s)? to loop through an array: set E b is &A[0] E i is an index s is the size of each element we ll do this a lidle bit later 50

51 prachce problem operand value %eax? 0x104? $0x108? (%eax)? 4(%eax)? 9(%eax,%edx)? 260(%ecx,%edx)? 0xFC(,%ecx,4)? (%eax,%edx,4)? address 0x100 0x104 0x108 0x10C register %eax %ecx %edx value 0xFF 0xAB 0x13 0x11 value 0x100 0x1 0x3 51

52 prachce problem soluhon operand value comment %eax 0x100 register 0x104 0xAB absolute address $0x108 0x108 immediate (%eax) 0xFF addr 0x100 4(%eax) 0xAB addr 0x104 9(%eax,%edx) 0x11 addr 0x10C 260(%ecx,%edx) 0x13 addr 0x108 0xFC(,%ecx,4) 0xFF addr 0x100 (%eax,%edx,4) 0x11 addr 0x10C address 0x100 0x104 0x108 0x10C register %eax %ecx %edx value 0xFF 0xAB 0x13 0x11 value 0x100 0x1 0x3 52

53 leal load effechve address more or less assembly equiv of C s & op example: %eax 0x1000 %ecx 0xA leal (%eax,%ecx,4), %edx what s in %edx? 53

54 leal load effechve address more or less assembly equiv of C s & op example: leal (%eax,%ecx,4), %edx what s in %edx? %eax %ecx 0x1000 0xA %eax+%ecx*4 = 0x *4 = 0x = 0x1000+0x28 = 0x

55 some leal prachce problems if %eax contains x, and %ecx contains y, what is in %edx aker the following ops? expression leal 6(%eax), %edx leal (%eax,%ecx), %edx leal (%eax,%ecx, 4), %edx leal 7(%eax,%eax,8), %edx leal 0xA(,%ecx,4), %edx leal 9(%eax,%ecx,2), %edx result 55

56 some leal prachce problems if %eax contains x, and %ecx contains y, what is in %edx aker the following ops? expression result leal 6(%eax), %edx 6+x leal (%eax,%ecx), %edx x+y leal (%eax,%ecx, 4), %edx x+4y leal 7(%eax,%eax,8), %edx 7+x+8x=7+9x leal 0xA(,%ecx,4), %edx 0xA+4y=10+4y leal 9(%eax,%ecx,2), %edx 9+x+2y 56

57 some things that you know in C assignment simple arithme7c opera7ons funchons condihonals loops 57

58 Some arithmehc ops instruction e ect description incl D D D + 1 increment decl D D D 1 decrement negl D D D negate notl D D D complement addl S, D D D + S add subl S, D D D S subtract imull S, D D D S multiply xorl S, D D D S exclusive or orl S, D D D S or andl S, D D D&S and sall k, D D D << k left shift shll k, D D D << k left shift (same as sall) sarl k, D D D >> k shift right arithmetic shrl k, D D D >> k shift right logical 58

59 simple arithmehc ops examples we ll save these for another day 59

60 some things that you know in C assignment simple arithmehc operahons func7ons condihonals loops 60

61 1 #include <stdlib.h> 2 3 #define BUF_LEN int main(void) { 6 int x; 7 char *buff; Where allocated? 11 if ((buff = (char*)malloc(buf_len))==null) { 12 fprintf(stderr, "error allocating space. quitting\n"); 13 return 1; 14 } return 0; 18 }

62 1 class Junk { 2 int x; 3 String s; 4 } and here? 5 6 public class WhereAllocated { 7 public static void main(string args[]) { 8 final int LEN = 1024; 9 int x; 10 Junk j = new Junk(); 11 int A[] = new int[len]; for (int i=0; i<a.length; i++) { /* do something with A[] */ } 20 } 21 }

63 Linux Process Memory

64 stacks can grow up or down 64

65 funchon calls stack very important for funchon calls high level view of what s happening: 65

66 funchon calls 66

67 funchon calls 67

68 funchon calls 68

69 funchon calls 69

70 funchon calls 70

71 funchon calls 71

72 funchon calls 72

73 funchon calls 73

74 where s the top, bodom of my frame? Answer: %esp and %ebp registers Used in parhcular way in every funchon call 74

75 funchon calls 75

76 funchon calls 76

77 funchon calls 77

78 funchon calls 78

79 how do we know where to return? Remember %eip Loop forever: fetch decode execute how do we get back? 79

80 How do we get back? In Dr. Bob s SIM in caller: save return address in %R8 in callee: In IA32: copy saved %R8 into %R9 (instruchon ptr) save the return address on the stack locahon: %ebp+4 80

81 calling a funchon in the caller: save caller s state pass parameters to the funchon remember return address turn control over to called funchon in the funchon: allocate space for locals do the work of the funchon return value turn control back over to caller 81

82 closer look at args on stack parameter location old %ebp (%ebp) return addr 4(%ebp) param 1 8(%ebp) param 2 12(%ebp) param n 4n+4(%ebp) 82

83 if we add local variables parameter location local var 2-8(%ebp) local var 1-4(%ebp) old %ebp (%ebp) return addr 4(%ebp) param 1 8(%ebp) param 2 12(%ebp) param n 4n+4(%ebp) 83

84 %ebp, %esp inside a funchon: beginning of each funchon: pushl %ebp movl %esp, %ebp at the end of each funchon, restore: movl %ebp, %esp popl %ebp so common that we have the instruchons: ENTER LEAVE 84

85 funchon return value %eax used as a return register by convenhon 85

86 a funchon template type fname: pushl %ebp movl %esp, %ebp /* add space for local vars if */ /* necessary by subtracting */ /* from %esp */... /* if you ve added space for */ /* local vars, free the space */ /* by adding to %esp */ movl popl ret %ebp, %esp %ebp 86

87 another look at swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up Body Finish 87

88 another example: sum two ints 1.text 2.globl sumtwomine 3.type 4 sumtwomine: 5 pushl %ebp 6 movl %esp, %ebp 7 movl 12(%ebp), %eax 8 movl 8(%ebp), %edx 9 addl %edx, %eax 10 popl %ebp 11 ret 88

89 another example: sum two ints 1.text 2.globl sumtwomine 3.type 4 sumtwomine: 5 pushl %ebp 6 movl %esp, %ebp 7 movl 12(%ebp), %eax 8 movl 8(%ebp), %edx 9 addl %edx, %eax 10 popl %ebp 11 ret Where do we find the arguments? Return value? 89

90 same thing, but gcc output 1.text 2.globl sumtwo 3.type 4 sumtwo: 5 pushl %ebp 6 movl %esp, %ebp 7 movl 12(%ebp), %eax 8 movl 8(%ebp), %edx 9 leal (%edx,%eax), %eax 10 popl %ebp 11 ret 90

91 What about my registers? Isn t the funchon going to trash them? 91

92 Have a roomate? 92

93 Have a roomate? hdp://commons.wikimedia.org/wiki/file:dirty_dishes.jpg 93

94 funchon reg saving convenhon caller save %eax %ecx %edx callie save %ebx %esi %edi 94

95 funchon reg saving convenhon caller save %eax %ecx %edx callie save %ebx %esi %edi Caller save: if I have something important in one of these, I need to save it before calling a funchon. 95

96 funchon reg saving convenhon caller save %eax %ecx %edx callie save %ebx %esi %edi Caller save: if I have something important in one of these, I need to save it before calling a funchon. Callie save: if I need to write to one of these, save it first. (Otherwise, I might be trashing the caller s stuff.) 96

97 save where? 97

98 save where? on the stack. 98

99 yet another look at swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } save %ebx restore %ebx swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up Body Finish 99

100 recursive funchon: factorial 1.section.text 2.globl _start from the PGU book 3.globl factorial #this is unneeded unless we want to share 4 #this function among other programs 5 _start: 6 pushl $4 #The factorial takes one argument - the 7 #number we want a factorial of. So, it 8 #gets pushed 9 call factorial #run the factorial function 10 addl $4, %esp #Scrubs the parameter that was pushed on 11 #the stack 12 movl %eax, %ebx #factorial returns the answer in %eax, but 13 #we want it in %ebx to send it as our exit 14 #status 15 movl $1, %eax #call the kernel s exit function 16 int $0x80 100

101 factorial funchon from the PGU book 1.type 2 factorial: 3 pushl %ebp #standard function stuff - we have to 4 #restore %ebp to its prior state before 5 #returning, so we have to push it 6 movl %esp, %ebp #This is because we don t want to modify 7 #the stack pointer, so we use %ebp. 8 9 movl 8(%ebp), %eax #This moves the first argument to %eax 10 #4(%ebp) holds the return address, and 11 #8(%ebp) holds the first parameter 12 cmpl $1, %eax #If the number is 1, that is our base 13 #case, and we simply return (1 is 14 #already in %eax as the return value) 15 je end_factorial 16 decl %eax #otherwise, decrease the value 17 pushl %eax #push it for our call to factorial 18 call factorial #call factorial 19 movl 8(%ebp), %ebx #%eax has the return value, so we 20 #reload our parameter into %ebx 21 imull %ebx, %eax #multiply that by the result of the 22 #last call to factorial (in %eax) 23 #the answer is stored in %eax, which 24 #is good since that s where return 25 #values go. 26 end_factorial: 27 movl %ebp, %esp #standard function return stuff - we 28 popl %ebp #have to restore %ebp and %esp to where 29 #they were before the function started 30 ret #return from the function (this pops the #return value, too)

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

1 /* file cpuid2.s */ 4.asciz The processor Vendor ID is %s \n 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start. 1 /* file cpuid2.s */ 2.section.data 3 output: 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer,

More information

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm

More information

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University Assembly I: Basic Operations Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Moving Data (1) Moving data: movl source, dest Move 4-byte ( long )

More information

Assembly I: Basic Operations. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung Assembly I: Basic Operations Jo, Heeseung Moving Data (1) Moving data: movl source, dest Move 4-byte ("long") word Lots of these in typical code Operand types Immediate: constant integer data - Like C

More information

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung ASSEMBLY I: BASIC OPERATIONS Jo, Heeseung MOVING DATA (1) Moving data: movl source, dest Move 4-byte ("long") word Lots of these in typical code Operand types Immediate: constant integer data - Like C

More information

Machine Programming 1: Introduction

Machine Programming 1: Introduction Machine Programming 1: Introduction CS61, Lecture 3 Prof. Stephen Chong September 8, 2011 Announcements (1/2) Assignment 1 due Tuesday Please fill in survey by 5pm today! Assignment 2 will be released

More information

Machine-Level Programming Introduction

Machine-Level Programming Introduction Machine-Level Programming Introduction Today Assembly programmer s exec model Accessing information Arithmetic operations Next time More of the same Fabián E. Bustamante, Spring 2007 IA32 Processors Totally

More information

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction E I P CPU isters Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Topics Assembly Programmer

More information

Machine-Level Programming I: Introduction Jan. 30, 2001

Machine-Level Programming I: Introduction Jan. 30, 2001 15-213 Machine-Level Programming I: Introduction Jan. 30, 2001 Topics Assembly Programmer s Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008 CISC 360 Machine-Level Programming I: Introduction Sept. 18, 2008 Topics Assembly Programmerʼs Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

Machine-Level Programming Introduction

Machine-Level Programming Introduction Machine-Level Programming Introduction Today! Assembly programmer s exec model! Accessing information! Arithmetic operations Next time! More of the same Fabián E. Bustamante, 2007 X86 Evolution: Programmer

More information

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002 Topics Assembly Programmer s Execution Model Accessing Information Registers Memory Arithmetic operations

More information

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

More information

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Machine-level Programming (3)

Machine-level Programming (3) Machine-level Programming (3) Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 2 Procedure Control

More information

CS213. Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward

More information

X86 Assembly -Data II:1

X86 Assembly -Data II:1 X86 Assembly -Data II:1 Administrivia HW1 due tonight Paper to locker, file to blackboard Lab1 Check scoreboard Quiz0 Remind me if you don t see in the lecture slide online Reading: chapter 3 now! II:2

More information

Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element Bottom Increasing

More information

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View. Machine-Level Programming I: Introduction Sept. 18, 2008 Topics CISC 360 Assembly Programmerʼs Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

The Hardware/Software Interface CSE351 Spring 2015

The Hardware/Software Interface CSE351 Spring 2015 The Hardware/Software Interface CSE351 Spring 2015 Lecture 7 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang

More information

CS241 Computer Organization Spring Addresses & Pointers

CS241 Computer Organization Spring Addresses & Pointers CS241 Computer Organization Spring 2015 Addresses & Pointers 2-24 2015 Outline! Addresses & Pointers! leal - load effective address! Condition Codes & Jumps! conditional statements: if-then-else! conditional

More information

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1 Homework In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, 361-367 Practice Exam 1 1 In-line Assembly Code The gcc compiler allows you to put assembly instructions in-line

More information

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT CS33 Intro to Computer Systems XI 1 Copyright 2017 Thomas

More information

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt

More information

Process Layout and Function Calls

Process Layout and Function Calls Process Layout and Function Calls CS 6 Spring 07 / 8 Process Layout in Memory Stack grows towards decreasing addresses. is initialized at run-time. Heap grow towards increasing addresses. is initialized

More information

CS241 Computer Organization Spring 2015 IA

CS241 Computer Organization Spring 2015 IA CS241 Computer Organization Spring 2015 IA-32 2-10 2015 Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add,

More information

Machine- level Programming

Machine- level Programming Machine- level Programming Topics Assembly Programmer s Execu:on Model Accessing Informa:on Registers Memory Arithme:c opera:ons 1! Evolu:onary Design Star:ng in 1978 with 8086 IA32 Processors Added more

More information

Sungkyunkwan University

Sungkyunkwan University - 2 - Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches While loops - 3 - Most General Form D(Rb,Ri,S) Mem[ Reg[ R b ] + S Reg[ R

More information

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012 19:211 Computer Architecture Lecture 10 Fall 20 Topics:Chapter 3 Assembly Language 3.2 Register Transfer 3. ALU 3.5 Assembly level Programming We are now familiar with high level programming languages

More information

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View Machine-Level Programming I: Introduction Sept. 10, 2002 class05.ppt 15-213 The course that gives CMU its Zip! Topics Assembly Programmer s Execution Model Accessing Information Registers Memory Arithmetic

More information

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete Machine-Level Programming I: Introduction Sept. 10, 2002 class05.ppt 15-213 The course that gives CMU its Zip! Topics! Assembly Programmer s Execution Model! Accessing Information " Registers " Memory!

More information

Sungkyunkwan University

Sungkyunkwan University Switch statements IA 32 Procedures Stack Structure Calling Conventions Illustrations of Recursion & Pointers long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case

More information

X86 Assembly -Procedure II:1

X86 Assembly -Procedure II:1 X86 Assembly -Procedure II:1 IA32 Object Code Setup Label.L61 becomes address 0x8048630 Label.L62 becomes address 0x80488dc Assembly Code switch_eg:... ja.l61 # if > goto default jmp *.L62(,%edx,4) # goto

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address IA32 Procedures 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoDom Increasing Addresses Register contains lowest stack address address of top element Stack

More information

Process Layout, Function Calls, and the Heap

Process Layout, Function Calls, and the Heap Process Layout, Function Calls, and the Heap CS 6 Spring 20 Prof. Vern Paxson TAs: Devdatta Akhawe, Mobin Javed, Matthias Vallentin January 9, 20 / 5 2 / 5 Outline Process Layout Function Calls The Heap

More information

Machine-Level Programming II: Control and Arithmetic

Machine-Level Programming II: Control and Arithmetic Machine-Level Programming II: Control and Arithmetic CSCI 2400: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides 1 Today Complete addressing mode, address

More information

Instruction Set Architecture

Instruction Set Architecture CS:APP Chapter 4 Computer Architecture Instruction Set Architecture Randal E. Bryant Carnegie Mellon University http://csapp.cs.cmu.edu CS:APP Instruction Set Architecture Assembly Language View! Processor

More information

Instruction Set Architecture

Instruction Set Architecture CS:APP Chapter 4 Computer Architecture Instruction Set Architecture Randal E. Bryant Carnegie Mellon University http://csapp.cs.cmu.edu CS:APP Instruction Set Architecture Assembly Language View Processor

More information

Machine Programming 3: Procedures

Machine Programming 3: Procedures Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure

More information

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? exam on Wednesday today s material not on the exam 1 Assembly Assembly is programming

More information

Giving credit where credit is due

Giving credit where credit is due CSCE 230J Computer Organization Machine-Level Programming I: Introduction Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created

More information

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter x6 basics ISA context and x6 history Translation: Compile C à machine code Disassemble machine code x6 Basics: isters Data movement instructions Memory addressing modes Arithmetic instructions 1 Software

More information

Instructor: Alvin R. Lebeck

Instructor: Alvin R. Lebeck X86 Assembly Programming with GNU assembler Lecture 7 Instructor: Alvin R. Lebeck Some Slides based on those from Randy Bryant and Dave O Hallaron Admin Reading: Chapter 3 Note about pointers: You must

More information

Handout #2: Machine-Level Programs

Handout #2: Machine-Level Programs 15-213 Handout #2: Machine-Level Programs on Linux/IA32 Randal E. Bryant David R. O Hallaron October 15, 1999 1 Introduction This document describes how machine-level programs are encoded for Intel IA32

More information

An Experience Like No Other. Stack Discipline Aug. 30, 2006

An Experience Like No Other. Stack Discipline Aug. 30, 2006 15-410 An Experience Like No Other Discipline Aug. 30, 2006 Bruce Maggs Dave Eckhardt Slides originally stolen from 15-213 15-410, F 06 Synchronization Registration If you're here but not registered, please

More information

Stack Discipline Jan. 19, 2018

Stack Discipline Jan. 19, 2018 15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18 Synchronization Registration The wait list will probably be done

More information

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element Machine Representa/on of Programs: Procedures Instructors: Sanjeev Se(a 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoGom Increasing Addresses Register

More information

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View Computer Architecture I Instruction Set Architecture Assembly Language View Processor state Registers, memory, Instructions addl, movl, andl, How instructions are encoded as bytes Layer of Abstraction

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures Machine- Level Programming III: Switch Statements and IA32 Procedures CS 485: Systems Programming Fall 2015 Instructor: James Griffioen Adapted from slides by R. Bryant and D. O Hallaron (hjp://csapp.cs.cmu.edu/public/instructors.html)

More information

Instruction Set Architecture

Instruction Set Architecture CISC 360 Instruction Set Architecture Michela Taufer October 9, 2008 Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron, Prentice Hall, 2003 Chapter

More information

CISC 360 Instruction Set Architecture

CISC 360 Instruction Set Architecture CISC 360 Instruction Set Architecture Michela Taufer October 9, 2008 Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron, Prentice Hall, 2003 Chapter

More information

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 n Mello Machine-Level Programming II: Arithmetic & Control 15-213/18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Instructors: Gregory Kesden The course that gives CMU its Zip! Last Time:

More information

Data Representa/ons: IA32 + x86-64

Data Representa/ons: IA32 + x86-64 X86-64 Instruc/on Set Architecture Instructor: Sanjeev Se(a 1 Data Representa/ons: IA32 + x86-64 Sizes of C Objects (in Bytes) C Data Type Typical 32- bit Intel IA32 x86-64 unsigned 4 4 4 int 4 4 4 long

More information

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang Project 3 Questions CMSC 313 Lecture 12 How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP

More information

Credits to Randy Bryant & Dave O Hallaron

Credits to Randy Bryant & Dave O Hallaron Mellon Machine Level Programming II: Arithmetic & Control Lecture 4, March 10, 2011 Alexandre David Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon 1 Today Complete addressing mode, address

More information

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CMSC 313 Lecture 12 [draft] How C functions pass parameters CMSC 313 Lecture 12 [draft] How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP removes an

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions CS220 Logic Design Outline Calling Conventions Multi-module Programs 1 Calling and Returning We have already seen how the call instruction is used to execute a subprogram. call pushes the address of the

More information

Machine Level Programming II: Arithmetic &Control

Machine Level Programming II: Arithmetic &Control Machine Level Programming II: Arithmetic &Control Arithmetic operations Control: Condition codes Conditional branches Loops Switch Kai Shen 1 2 Some Arithmetic Operations Two Operand Instructions: Format

More information

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm CAS CS 210 - Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm This problem set is to be completed individually. Explain how you got to your answers

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

CS , Fall 2001 Exam 1

CS , Fall 2001 Exam 1 Andrew login ID: Full Name: CS 15-213, Fall 2001 Exam 1 October 9, 2001 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 8: Introduction to code generation Viktor Leijon Slides largely by Johan Nordlander with material generously provided by Mark P. Jones. 1 What is a Compiler? Compilers

More information

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes Machine-Level Programming II: Arithmetic & Control CS-281: Introduction to Computer Systems Instructor: Thomas C. Bressoud 1 Complete Memory Addressing Modes Most General Form D(Rb,Ri,S)Mem[Reg[Rb]+S*Reg[Ri]+

More information

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110 Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question

More information

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures Machine- Level Programming III: Switch Statements and IA32 Procedures 15-213: Introduc;on to Computer Systems 6 th Lecture, Sep. 9, 2010 Instructors: Randy Bryant and Dave O Hallaron Today Switch statements

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today: Winter 2006-2007 Compiler Construction T11 Activation records + Introduction to x86 assembly Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University Today ic IC Language Lexical Analysis

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CONST POINTERS CONST POINTERS 4 ways to declare pointers in combination with const:!! int *ptr! const int *ptr!

More information

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19 Access Young W. Lim 2017-06-10 Sat Young W. Lim Access 2017-06-10 Sat 1 / 19 Outline 1 Introduction References IA32 Operand Forms Data Movement Instructions Data Movement Examples Young W. Lim Access 2017-06-10

More information

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009 CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009 Name Score out of 70 UMBC Username Notes: a. Please write clearly. Unreadable answers receive no credit. b. For TRUE/FALSE questions, write the

More information

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

More information

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only Instructions: The following questions use the AT&T (GNU) syntax for x86-32 assembly code, as in the course notes. Submit your answers to these questions to the Curator as OQ05 by the posted due date and

More information

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday addl: bitwise for signed (& unsigned) 4 bits: 1000 = -8, 0111 = 7-8 + -8 = -16 = 0 1000 + 1000

More information

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation Compiler Construction SMD163 Lecture 8: Introduction to code generation Viktor Leijon & Peter Jonsson with slides by Johan Nordlander Contains material generously provided by Mark P. Jones What is a Compiler?

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron

More information

University of Washington

University of Washington Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp

More information

CS61 Section Solutions 3

CS61 Section Solutions 3 CS61 Section Solutions 3 (Week of 10/1-10/5) 1. Assembly Operand Specifiers 2. Condition Codes 3. Jumps 4. Control Flow Loops 5. Procedure Calls 1. Assembly Operand Specifiers Q1 Operand Value %eax 0x104

More information

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18 Access Young W. Lim 2017-01-27 Fri Young W. Lim Access 2017-01-27 Fri 1 / 18 Outline 1 Introduction References IA32 Operand Forms Data Movement Instructions Young W. Lim Access 2017-01-27 Fri 2 / 18 Based

More information

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016 CS 31: Intro to Systems Functions and the Stack Martin Gagne Swarthmore College February 23, 2016 Reminders Late policy: you do not have to send me an email to inform me of a late submission before the

More information

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G CS 33: Week 3 Discussion x86 Assembly (v1.0) Section 1G Announcements - HW2 due Sunday - MT1 this Thursday! - Lab2 out Info Name: Eric Kim (Section 1G, 2-4 PM, BH 5419) Office Hours (Boelter 2432) - Wed

More information

hnp://

hnp:// The bots face off in a tournament against one another and about an equal number of humans, with each player trying to score points by elimina&ng its opponents. Each player also has a "judging gun" in addi&on

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Warren Hunt, Jr. and Bill Young Department of Computer Sciences University of Texas at Austin Last updated: October 1, 2014 at 08:36 CS429 Slideset 7: 1 Topics

More information

Second Part of the Course

Second Part of the Course CSC 2400: Computer Systems Towards the Hardware 1 Second Part of the Course Toward the hardware High-level language (C) assembly language machine language (IA-32) 2 High-Level Language g Make programming

More information

Systems I. Machine-Level Programming I: Introduction

Systems I. Machine-Level Programming I: Introduction Systems I Machine-Level Programming I: Introduction Topics Assembly Programmerʼs Execution Model Accessing Information Registers IA32 Processors Totally Dominate General Purpose CPU Market Evolutionary

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012) CSE351 Autumn 2012 Midterm Exam (5 Nov 2012) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate will prove to

More information

Instruction Set Architecture

Instruction Set Architecture CS:APP Chapter 4 Computer Architecture Instruction Set Architecture Randal E. Bryant adapted by Jason Fritts http://csapp.cs.cmu.edu CS:APP2e Hardware Architecture - using Y86 ISA For learning aspects

More information

CPS104 Recitation: Assembly Programming

CPS104 Recitation: Assembly Programming CPS104 Recitation: Assembly Programming Alexandru Duțu 1 Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in

More information

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature CSE2421 FINAL EXAM SPRING 2013 Name KEY Instructions: This is a closed-book, closed-notes, closed-neighbor exam. Only a writing utensil is needed for this exam. No calculators allowed. If you need to go

More information

The Hardware/Software Interface CSE351 Spring 2013

The Hardware/Software Interface CSE351 Spring 2013 The Hardware/Software Interface CSE351 Spring 2013 x86 Programming II 2 Today s Topics: control flow Condition codes Conditional and unconditional branches Loops 3 Conditionals and Control Flow A conditional

More information

Intel assembly language using gcc

Intel assembly language using gcc QOTD Intel assembly language using gcc Assembly language programming is difficult. Make no mistake about that. It is not for wimps and weaklings. - Tanenbaum s 6th, page 519 These notes are a supplement

More information

CIT Week13 Lecture

CIT Week13 Lecture CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

More information

CPEG421/621 Tutorial

CPEG421/621 Tutorial CPEG421/621 Tutorial Compiler data representation system call interface calling convention Assembler object file format object code model Linker program initialization exception handling relocation model

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron

More information

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe Machine Level Programming I: Basics 15 213/18 213: 213: Introduction to Computer Systems 5 th Lecture, Jan. 31, 2012 Instructors: Todd C. Mowry & Anthony Rowe 1 Today: Machine Programming gi: Basics History

More information