Systems Programming and Computer Architecture ( )
|
|
- Morgan Weaver
- 6 years ago
- Views:
Transcription
1 Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture ( ) Timothy Roscoe Herbstsemester 2016 AS 2016 Compiling C Control Flow 1
2 8: Compiling C control flow Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 2
3 8.1: if-then-else statements Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 3
4 Conditional branch example #include <stdio.h> int putmax(int x, int y) { int result; if (x > y) { result = printf("%d\n", x); else { result = printf("%d\n", y); return result; putmax: subq $8, %rsp cmpl %esi, %edi jle.l2 movl %edi, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call printf_chk jmp.l3.l2: movl %esi, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call printf_chk.l3: addq $8, %rsp ret Setup Test Body 1 Body 2 Finish AS 2016 Compiling C Control Flow 4
5 Conditional branch example #include <stdio.h> int putmax(int x, int y) { int result; if (x <= y) { goto Else; result = printf("%d\n", x); goto Done; Else: result = printf("%d\n", y); Done: return result; putmax: subq $8, %rsp cmpl %esi, %edi jle.l2 movl %edi, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call printf_chk jmp.l3.l2: movl %esi, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call printf_chk.l3: addq $8, %rsp ret Setup Test Body 1 Body 2 Finish AS 2016 Compiling C Control Flow 5
6 General conditional expression translation C code val = Test? Then-Expr : Else-Expr; val = x>y? p(x) : p(y); Goto version nt =!Test; if (nt) goto Else; val = Then-Expr;... goto Done; Else: val = Else-Expr; Done: return or nt =!Test; if (nt) goto Else; val = Then-Expr;... Done: return Else: val = Else-Expr; goto Done; Test is expression returning integer = 0 interpreted as false 0 interpreted as true Create separate code regions for then & else expressions Execute appropriate one AS 2016 Compiling C Control Flow 6
7 Conditional move int absdiff( int x, int y) { int result; if (x > y) { result = x-y; else { result = y-x; return result; absdiff: # x in %edi, y in %esi movl %edi, %eax subl %esi, %eax movl %esi, %edx subl %edi, %edx cmpl %esi, %edi cmovle %edx, %eax ret Conditional move instruction cmovc src, dest Move value from src to dest if condition C holds More efficient than conditional branching (simple control flow) But overhead: both branches are evaluated AS 2016 Compiling C Control Flow 7
8 C code General form with conditional move val = Test? Then-Expr : Else-Expr; Conditional move version val1 = Then-Expr; val2 = Else-Expr; val1 = val2 if!test; Both values get computed Overwrite then-value with else-value if condition doesn t hold Don t use when: Then or else expressions have side effects Then and else expressions are too expensive AS 2016 Compiling C Control Flow 8
9 8.2: do-while loops Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 9
10 Do-While loop example C code int fact_do(int x) { int result = 1; do { result *= x; x = x-1; while (x > 1); return result; Goto version int fact_goto(int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; Use backward branch to continue looping Only take branch when while condition holds AS 2016 Compiling C Control Flow 10
11 Do-while loop Goto version int fact_goto(int x) { int result = 1; compilation Assembly fact_do: Registers: %edi %eax movl $1, %eax # eax = 1 x result loop: result *= x; x = x-1; if (x > 1) goto loop;.l3: imull %edi, %eax # eax = eax*edi subl $1, %edi # edi -= 1 cmpl $1, %edi # compare 1 & edi jg.l3 # jump if greater return result; rep ret # return AS 2016 Compiling C Control Flow 11
12 General do-while translation C code do Body while (Test); Goto version loop: Body if (Test) goto loop Body: { Statement 1 ; Statement 2 ; Statement n ; Test returns integer = 0 interpreted as false 0 interpreted as true AS 2016 Compiling C Control Flow 12
13 8.3: while loops Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 13
14 GCC while translation C code int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; ; return result; Uses same inner loop as do-while version Guards loop entry with extra test Goto version int fact_while_goto1(int x) { int result = 1; if (!(x > 1)) goto done; loop: result *= x; x = x-1; if (x > 1) goto loop; done: return result; AS 2016 Compiling C Control Flow 14
15 General while translation While version while (Test) Body Do-while version if (!Test) goto done; do Body while(test); done: Goto version if (!Test) goto done; loop: Body if (Test) goto loop; done: AS 2016 Compiling C Control Flow 15
16 New-style while translation C code int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; ; return result; Recent technique for GCC With O2 First iteration jumps over body computation within loop Goto version int fact_while_goto2(int x) { int result = 1; goto middle; loop: result *= x; x = x-1; middle: if (x > 1) goto loop; return result; AS 2016 Compiling C Control Flow 16
17 Jump-to-middle while translation C code while (Test) Body Avoids duplicating test code Unconditional goto incurs no performance penalty these days for loops compiled in similar fashion Goto version goto middle; loop: Body middle: if (Test) goto loop; Goto (previous) version if (!Test) goto done; loop: Body if (Test) goto loop; done: AS 2016 Compiling C Control Flow 17
18 Jump-to-middle example int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x--; ; return result; # x in %edx, result in %eax jmp.l34 # goto Middle.L35: # Loop: imull %edx, %eax # result *= x decl %edx # x--.l34: # Middle: cmpl $1, %edx # x:1 jg.l35 # if >, goto Loop 18
19 Implementing loops Originally translated into do-while Optimizer makes use of jump to middle Why the difference? Compiler originally developed for machine where all operations costly Updated for machine where unconditional branches incur (almost) no overhead AS 2016 Compiling C Control Flow 19
20 Summary Do-While loop While-Do loop While version while (Test) Body C Code do Body while (Test); Do-While version if (!Test) goto done; do Body while(test); done: Goto version loop: Body if (Test) goto loop Goto version if (!Test) goto done; loop: Body if (Test) goto loop; done: AS 2016 or Compiling C Control Flow goto middle; loop: Body middle: if (Test) goto loop;
21 8.4: for loops Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 21
22 For loop example: square-and-multiply /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned p) { int result; for (result = 1; p!= 0; p = p>>1) { if (p & 0x1) { result *= x; x = x*x; return result; Algorithm Exploit bit representation: p = p 0 + 2p p n 1 Gives: x p = z 0 z 1 2 z z n AS 2016 z i = 1 when p i = 0 z i = x when p i = 1 Complexity O(log p) n 1 times Compiling C Control Flow Example 3 10 = 3 2 * 3 8 = 3 2 * ((3 2 ) 2 ) 2
23 ipwr computation /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned p) { int result; for (result = 1; p!= 0; p = p>>1) { if (p & 0x1) { result *= x; x = x*x; return result; before iteration result x=3 p= = = = =
24 For loop example int result; for (result = 1; p!= 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; General Form for (Init; Test; Update) Body Test p!= 0 Init result = 1 Update p = p >> 1 Body { if (p & 0x1) { result *= x; x = x*x; AS 2016 Compiling C Control Flow 24
25 For While Do-While For version for (Init; Test; Update ) Body While version Init; while (Test ) { Body Update ; Goto version Init; if (!Test) goto done; loop: Body Update ; if (Test) goto loop; done: Do-While version Init; if (!Test) goto done; do { Body Update ; while (Test) done:
26 For-loop: compilation #1 For version for (Init; Test; Update ) Body for (result = 1; p!= 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; Goto version Init; if (!Test) goto done; loop: Body Update ; if (Test) goto loop; done: result = 1; if (p == 0) goto done; loop: if (p & 0x1) result *= x; x = x*x; p = p >> 1; if (p!= 0) goto loop; done: 26
27 For version for (Init; Test; Update ) Body For While (jump-to-middle) While version Init; while (Test ) { Body Update ; Goto version Init; goto middle; loop: Body Update ; middle: if (Test) goto loop; done: AS 2016 Compiling C Control Flow 27
28 For-loop: compilation #2 For version for (Init; Test; Update ) Body for (result = 1; p!= 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; Goto version Init; goto middle; loop: Body Update ; middle: if (Test) goto loop; done: result = 1; goto middle; loop: if (p & 0x1) result *= x; x = x*x; p = p >> 1; middle: if (p!= 0) goto loop; done: 28
29 8.5: compact switch statements Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 29
30 Switch statement example Lots going on here Multiple case labels Here: 5, 6 Fall through cases Here: 2 Missing cases Here: 4 long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; return w; AS
31 Jump table structure Switch Form switch(x) { case val_0: Block 0 case val_1: Block 1 case val_n-1: Block n 1 jtab: Jump Table Targ0 Targ1 Targ2 Targn-1 Targ0: Targ1: Targ2: Jump Targets Code Block 0 Code Block 1 Code Block 2 Approximate Translation target = JTab[x]; goto *target; AS 2016 Compiling C Control Flow Targn-1: Code Block n 1
32 Switch statement Indirect jump long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... return w; Setup: switch_eg: movq %rdx, %rcx cmpq $6, %rdi # x : 6? ja.l8 # if > goto default jmp *.L4(,%rdi,8) # goto Jtab[x] Jump table:.section.rodata.align 8.align 4.L4:.quad.L8 # x=0.quad.l3 # x=1.quad.l5 # x=2.quad.l9 # x=3.quad.l8 # x=4.quad.l7 # x=5.quad.l7 # x=6 AS 2016 Compiling C Control Flow 32
33 Assembly setup explanation Table Structure Each target requires 8 bytes Base address at.l4 Jumping Direct: jmp.l8 Jump target is denoted by label.l8 Jump table.section.rodata.align 8.align 4.L4:.quad.L8 # x=0.quad.l3 # x=1.quad.l5 # x=2.quad.l9 # x=3.quad.l8 # x=4.quad.l7 # x=5.quad.l7 # x=6 Indirect: jmp *.L4(,%rdi,8) Must scale by factor of 8 (labels are 64-bit = 8 Bytes on x86_64) Fetch target from effective Address.L61 + rdi*8 Only for 0 x 6 AS 2016 Compiling C Control Flow 33
34 Jump table Jump table.section.rodata.align 8.align 4.L4:.quad.L8 # x=0.quad.l3 # x=1.quad.l5 # x=2.quad.l9 # x=3.quad.l8 # x=4.quad.l7 # x=5.quad.l7 # x=6 switch(x) { case 1: //.L56 w = y*z; break; case 2: //.L57 w = y/z; /* Fall Through */ case 3: //.L58 w += z; break; case 5: case 6: //.L60 w -= z; break; default: //.L61 w = 2; AS 2016 Compiling C Control Flow 34
35 Code blocks (partial) switch(x) { case 1: //.L3 w = y*z; break; case 2: //.L5 w = y/z; /* Fall Through */ case 3: //.L9 w += z; break;....l3: // x = 1 movq %rsi, %rax imulq %rdx, %rax ret.l5: // x = 2 movq %rsi, %rax cqto # div prep idivq %rcx # divide jmp.l6 # fall thru.l9: // x = 3 movl $1, %eax # w = 1.L6: // shared addq %rcx, %rax # Add to w ret AS 2016 Compiling C Control Flow 35
36 Code blocks (the rest) switch(x) {... case 5: case 6: //.L7 w -= z; break; default: //.L8 w = 2;.L7: movl $1, %eax # w = 1 subq %rdx, %rax # w =- z ret.l8: movl $2, %eax # w = 2 ret AS 2016 Compiling C Control Flow 36
37 x86-64 object code Setup Label.L8 becomes address 0x Label.L4 becomes address 0x b8 Assembly Code switch_eg:... ja.l8 # if > goto default jmp *.L4(,%rdi,8) # goto JTab[x] (in this case) Disassembled Object Code ed <switch_eg>: f4: 77 2b ja <switch_eg+0x34> 4004f6: ff 24 fd b jmpq *0x4005b8(,%rdi,8) AS 2016 Compiling C Control Flow 37
38 x86_64 object code Jump Table Doesn t show up in disassembled code Can inspect using gdb: Examine 7 hexadecimal format giant words (8-bytes each) Use command help x to get format documentation (gdb) x/7xg 0x4005b8 0x4005b8: 0x x fd 0x4005c8: 0x x f 0x4005d8: 0x x x4005e8: 0x (gdb) AS 2016 Compiling C Control Flow 38
39 Matching disassembled targets 0x x fd 0x x f 0x x x Dump of assembler code for function switch_eg: 0x ed <+0>: mov %rdx,%rcx 0x f0 <+3>: cmp $0x6,%rdi 0x f4 <+7>: ja 0x <switch_eg+52> 0x f6 <+9>: jmpq *0x4005b8(,%rdi,8) 0x fd <+16>: mov %rsi,%rax 0x <+19>: imul %rdx,%rax 0x <+23>: retq 0x <+24>: mov %rsi,%rax 0x <+27>: cqto 0x a <+29>: idiv %rcx 0x d <+32>: jmp 0x <switch_eg+39> 0x f <+34>: mov $0x1,%eax 0x <+39>: add %rcx,%rax 0x <+42>: retq 0x <+43>: mov $0x1,%eax 0x d <+48>: sub %rdx,%rax 0x <+51>: retq 0x <+52>: mov $0x2,%eax 0x <+57>: retq End of assembler dump. (gdb) AS 2016 Compiling C Control Flow 39
40 8.6: sparse switch statements Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 40
41 Sparse switch example /* Return x/111 if x is multiple && <= otherwise */ int div111(int x) { switch(x) { case 0: return 0; case 111: return 1; case 222: return 2; case 333: return 3; case 444: return 4; case 555: return 5; case 666: return 6; case 777: return 7; case 888: return 8; case 999: return 9; default: return -1; Not practical to use jump table Would require 1000 entries Obvious translation into if-then-else would have max. of 9 tests 41
42 Sparse switch code div111: cmpl $444, %edi je.l3 jle.l28 cmpl $777, %edi je.l10 jg.l11 cmpl $555, %edi movl $5, %eax je.l5 cmpl $666, %edi movb $6, %al jne.l2.l5: rep ret AS 2016.L28:.L2: Compares x to possible case values Jumps different places depending on outcomes cmpl $111, %edi movl $1, %eax je.l5 jle.l29 cmpl $222, %edi movl $2, %eax je.l5 cmpl $333, %edi movb $3, %al je.l5 movl ret $-1, %eax
43 Sparse switch code structure < 444 > = < > > = < = = = = > = = = = Organizes cases as binary tree Logarithmic performance AS 2016 Compiling C Control Flow 43
44 Summary C Control if-then-else do-while while, for switch Assembler Control Conditional jump Conditional move Indirect jump Compiler Must generate assembly code to implement more complex control Standard Techniques Loops converted to do-while form jump-to-middle Large switch statements use jump tables Sparse switch statements may use decision trees Conditions in CISC CISC machines generally have condition code registers AS 2016 Compiling C Control Flow 44
45 8.7: Procedure call and return Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 45
46 x86_64 stack Region of memory managed with stack discipline Grows toward lower addresses Register %rsp contains lowest stack address = address of top element Stack Pointer: %rsp Stack Bottom Increasing Addresses Stack Grows Down Stack Top AS 2016 Compiling C Control Flow 46
47 x86_64 stack: push pushl Src Fetch operand at Src Decrement %rsp by 4 Write operand at address given by %rsp Stack Bottom Increasing Addresses Stack Grows Down Stack Pointer: %rsp -4 Stack Top AS 2016 Compiling C Control Flow 47
48 x86_64 stack: pop popl Dest Read operand at address %rsp Increment %rsp by 4 Write operand to Dest Stack Bottom Increasing Addresses Stack Pointer: %rsp +4 Stack Grows Down Stack Top AS 2016 Compiling C Control Flow 48
49 Procedure control flow Use stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Return address: Address of instruction beyond call Example from disassembly e: e: e8 3d e8 063d call 8048b90 <main> : : pushl %eax Return address = 0x Procedure return: ret Pop address from stack Jump to address AS 2016 Compiling C Control Flow 49
50 Procedure call example e: e8 3d call 8048b90 <main> : 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x x x100 %rsp 0x108 %rsp 0x108 %rip 0x804854e %rip 0x804854e AS 2016 Compiling C Control Flow 50
51 Procedure call example e: e8 3d call 8048b90 <main> : 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x x x100 0x %rsp 0x108 %rsp 0x108 0x100 %rip 0x804854e %rip 0x804854e AS 2016 Compiling C Control Flow 51
52 Procedure call example e: e8 3d call 8048b90 <main> : 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x x x100 0x %rsp 0x108 %rsp 0x108 0x100 %rip 0x804854e %rip 0x804854e 0x8048b90 AS 2016 Compiling C Control Flow 52
53 Procedure return example : c3 ret ret 0x110 0x110 0x10c 0x10c 0x x x100 0x x %rsp 0x100 %rsp 0x100 %rip 0x %rip 0x AS 2016 Compiling C Control Flow 53
54 Procedure return example : c3 ret ret 0x110 0x110 0x10c 0x10c 0x x x100 0x x %rsp 0x100 %rsp 0x100 0x108 %rip 0x %rip 0x x AS 2016 Compiling C Control Flow 54
55 full x86_64/linux stack frame Current stack frame ( top to bottom) Argument build: Parameters for function about to call Local variables If can t keep in registers Saved register context Old frame pointer Frame pointer %rbp Caller Frame Arguments Return Addr Old %rbp Caller stack frame Return address Pushed by call instruction Arguments for this call Stack pointer %rsp Saved Registers + Local Variables Argument Build AS 2016 Compiling C Control Flow 55
56 8.8: x86_64 calling conventions Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe AS 2016 Compiling C Control Flow 56
57 Recursive factorial int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; Registers %rax/%eax used without first saving %rbx/%ebx used, but saved at beginning & restore at end rfact: pushq %rbx movl %edi, %ebx movl $1, %eax cmpl $1, %edi jle.l2 leal -1(%rdi), %edi call rfact imull %ebx, %eax.l2: popq %rbx ret AS 2016 Compiling C Control Flow 57
58 Pointer code Recursive procedure void s_helper (int x, int *accum) { if (x <= 1) return; else { int z = *accum * x; *accum = z; s_helper (x-1,accum); Top-level call int sfact(int x) { int val = 1; s_helper(x, &val); return val; Pass pointer to update location AS 2016 Compiling C Control Flow 58
59 Passing the pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; Variable val must be stored on stack Because: Need to create pointer to it refer to pointer as 12(%rsp) sfact: subq $24, %rsp movl $1, 12(%rsp) leaq 12(%rsp), %rsi movl $0, %eax call s_helper movl 12(%rsp), %eax addq $24, %rsp ret Rtn adr val = 1 Unused Rtn adr old %rsp %rsp AS 2016 Compiling C Control Flow 59
60 x86-64 integer registers %rax %eax %r8 %r8d %rbx %ebx %r9 %r9d %rcx %ecx %r10 %r10d %rdx %edx %r11 %r11d %rsi %esi %r12 %r12d %rdi %edi %r13 %r13d %rsp %rsp %r14 %r14d %rbp %ebp %r15 %r15d AS 2016 Compiling C Control Flow 60
61 Register saving conventions When procedure yoo calls who: yoo is the caller who is the callee Can Register be used for temporary storage? yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%rsp), %edx addl $91125, %edx ret Contents of register %edx overwritten by who AS 2016 Compiling C Control Flow 61
62 Register saving conventions When procedure yoo calls who: yoo is the caller who is the callee Can register be used for temporary storage? Conventions Caller Save Caller saves temporary in its frame before calling Callee Save Callee saves temporary in its frame before using AS 2016 Compiling C Control Flow 62
63 x86-64 integer registers %rax Return value, # varargs %r8 Argument #5 %rbx Callee saved; base ptr %r9 Argument #6 %rcx Argument #4 %r10 Static chain ptr %rdx Argument #3 (& 2 nd return) %r11 Used for linking %rsi Argument #2 %r12 Callee saved %rdi Argument #1 %r13 Callee saved %rsp Stack pointer %r14 Callee saved %rbp Callee saved; frame ptr %r15 Callee saved AS 2016 Compiling C Control Flow 63
64 x86-64 registers Arguments passed to functions via registers If more than 6 integral parameters, then pass rest on stack These registers can be used as caller-saved as well All references to stack frame via stack pointer Eliminates 32-bit need to update %ebp/%rbp Other registers 6+1 callee saved 2 or 3 have special uses AS 2016 Compiling C Control Flow 64
65 x86-64 stack frame example long sum = 0; /* Swap a[i] & a[i+1] */ void swap_ele_su(long a[], int i) { swap(&a[i], &a[i+1]); sum += a[i]; Keeps values of a and i in callee save registers Must set up stack frame to save these registers swap_ele_su: movq %rbx, -16(%rsp) movslq %esi,%rbx movq %r12, -8(%rsp) movq %rdi, %r12 leaq (%rdi,%rbx,8), %rdi subq $16, %rsp leaq 8(%rdi), %rsi call swap movq (%r12,%rbx,8), %rax addq %rax, sum(%rip) movq (%rsp), %rbx movq 8(%rsp), %r12 addq $16, %rsp ret AS 2016 Compiling C Control Flow 65
66 Understanding the x86-64 stack frame swap_ele_su: movq %rbx, -16(%rsp) # Save %rbx movslq %esi,%rbx # Extend & save i %rsp movq %r12, -8(%rsp) # Save %r12 movq %rdi, %r12 # Save a leaq (%rdi,%rbx,8), %rdi # &a[i] subq $16, %rsp # Allocate stack frame leaq 8(%rdi), %rsi # &a[i+1] call swap # swap() movq (%r12,%rbx,8), %rax # a[i] addq %rax, sum(%rip) # sum += a[i] movq (%rsp), %rbx # Restore %rbx %rsp movq 8(%rsp), %r12 # Restore %r12 addq $16, %rsp # Deallocate stack frame ret rtn addr %r12 %rbx rtn addr %r12 %rbx AS 2016 Compiling C Control Flow 66
67 Interesting features of the stack frame Allocate entire frame at once All stack accesses can be relative to %rsp Do by decrementing stack pointer Can delay allocation, since safe to temporarily use red zone - see next slide Simple deallocation Increment stack pointer No base/frame pointer needed AS 2016 Compiling C Control Flow 67
68 x86-64 locals in the red zone /* Swap, using local array */ void swap_a(long *xp, long *yp) { volatile long loc[2]; loc[0] = *xp; loc[1] = *yp; *xp = loc[1]; *yp = loc[0]; swap_a: movq (%rdi), %rax movq %rax, -24(%rsp) movq (%rsi), %rax movq %rax, -16(%rsp) movq -16(%rsp), %rax movq %rax, (%rdi) movq -24(%rsp), %rax movq %rax, (%rsi) ret Avoiding stack pointer change Can hold all information within small window beyond stack pointer rtn Ptr unused loc[1] loc[0] %rsp AS 2016 Compiling C Control Flow 68
69 x86-64 long swap void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; swap: movq movq movq movq ret (%rdi), %rdx (%rsi), %rax %rax, (%rdi) %rdx, (%rsi) Operands passed in registers First (xp) in %rdi, second (yp) in %rsi 64-bit pointers No stack operations required (except ret) Avoiding stack Can hold all local information in registers AS 2016 Compiling C Control Flow 69
70 x86-64 non-leaf w/o stack frame long scount = 0; /* Swap a[i] & a[i+1] */ void swap_ele_se(long a[], int i) { swap(&a[i], &a[i+1]); scount++; No values held while swap being invoked No callee save registers needed swap_ele_se: movslq %esi,%rsi # Sign extend i leaq (%rdi,%rsi,8), %rdi # &a[i] leaq 8(%rdi), %rsi # &a[i+1] call swap # swap() incq scount(%rip) # scount++; ret AS 2016 Compiling C Control Flow 70
71 x86-64 call using a jump long scount = 0; /* Swap a[i] & a[i+1] */ void swap_ele(long a[], int i) { swap(&a[i], &a[i+1]); swap_ele: movslq %esi,%rsi # Sign extend i leaq (%rdi,%rsi,8), %rdi # &a[i] leaq 8(%rdi), %rsi # &a[i+1] jmp swap # swap() Tail call optimization AS 2016 Compiling C Control Flow 71
72 full x86_64/linux stack frame Current stack frame ( top to bottom) Argument build: Parameters for function about to call Local variables If can t keep in registers Saved register context Old frame pointer Caller stack frame AS 2016 Return address Pushed by call instruction Arguments for this call What is this for? When might you want it? Compiling C Control Flow Frame pointer %rbp Stack pointer %rsp Caller Frame Arguments Return Addr Old %rbp Saved Registers + Local Variables Argument Build 72
73 x86-64 procedure summary Use of registers Callee save, argument passing, other Many tricky optimizations What kind of stack frame to use If any at all Rarely need a base (frame) pointer Calling with jump Various allocation techniques But the conventions still hold AS 2016 Compiling C Control Flow 73
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 informationSungkyunkwan 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%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker
Procedure Call Registers Return %rax %eax %r8 %r8d Arg 5 %rbx %ebx %r9 %r9d Arg 6 Arg 4 %rcx %ecx %r10 %r10d Arg 3 %rdx %edx %r11 %r11d Arg 2 %rsi %esi %r12 %r12d Arg 1 %rdi %edi %r13 %r13d ptr %esp %r14
More informationCS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.
CS 33 Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Processor State (x86-64, Partial) %rax %eax %r8 %r8d %rbx %ebx %r9 %r9d %rcx %ecx
More informationThe 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 informationCS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
CS 33 Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Observations about arith int arith(int x, int y, int z) { int t1 = x+y; int t2
More informationMachine-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 informationThe 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 informationMachine-Level Programming II: Control
Machine-Level Programming II: Control CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN Slides adapted from Bryant & O Hallaron s slides 1 Today Control: Condition codes Conditional
More informationMachine-Level Programming II: Control
Mellon Machine-Level Programming II: Control CS140 Computer Organization and Assembly Slides Courtesy of: Randal E. Bryant and David R. O Hallaron 1 First https://www.youtube.com/watch?v=ivuu8jobb1q 2
More informationAssembly Programming IV
Assembly Programming IV CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis 1 Administrivia Homework 2 due
More informationX86 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 informationAssembly 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 Mechanisms in Procedures Passing control To beginning of procedure code
More informationLecture 7: More procedures; array access Computer Architecture and Systems Programming ( )
Systems Group Department of Computer Science ETH Zürich Lecture 7: More procedures; array access Computer Architecture and Systems Programming (252-0061-00) Timothy Roscoe Herbstsemester 2012 1 Last Time
More informationMachine Representa/on of Programs: Control Flow cont d. Previous lecture. Do- While loop. While- Do loop CS Instructors: Sanjeev Se(a
Machine Representa/on of Programs: Control Flow cont d Instructors: Sanjeev Se(a 1 Previous lecture Do- While loop C Code Goto Version While- Do loop do while (Test); if (Test) goto loop Do- While Version
More informationControl flow. Condition codes Conditional and unconditional jumps Loops Switch statements
Control flow Condition codes Conditional and unconditional jumps Loops Switch statements 1 Conditionals and Control Flow Familiar C constructs l l l l l l if else while do while for break continue Two
More informationMachine-Level Programming II: Control
Machine-Level Programming II: Control 15-213: Introduction to Computer Systems 6 th Lecture, Feb. 2, 2017 Instructors: Franz Franchetti and Seth C. Goldstein 1 Office Hours Thanks for Coming! and sorry
More informationASSEMBLY 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 information2 Systems Group Department of Computer Science ETH Zürich. Argument Build 3. %r8d %rax. %r9d %rbx. Argument #6 %rcx. %r10d %rcx. Static chain ptr %rdx
Last Time Lecture 7: More procedures; array access Computer rchitecture and Systems Programming (252-0061-00) Timothy Roscoe Herbstsemester 2012 For loops - goto version goto jump to middle version Jump
More informationAssembly 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 informationAssembly 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 informationx86-64 Programming III & The Stack
x86-64 Programming III & The Stack CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/1652/ Administrative
More informationMachine-Level Programming (2)
Machine-Level Programming (2) Yanqiao ZHU Introduction to Computer Systems Project Future (Fall 2017) Google Camp, Tongji University Outline Control Condition Codes Conditional Branches and Conditional
More informationCS213. 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 informationCarnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition
1 Machine-Level Programming II: Control 15-213: Introduction to Computer Systems 6 th Lecture, Sept. 13, 2018 2 Today Control: Condition codes Conditional branches Loops Switch Statements 3 Recall: ISA
More informationCS367. Program Control
CS367 Program Control outline program control condition codes branching looping conditional moves switches (special case of branching) Condition Codes Processor State (x86-64, Partial) Info about currently
More informationSetting & Examining Condition Codes. int gt(long x, long y) { return x > y; } int has_nonzero_masked(long x, long mask) { return!!
Setting & Examining Condition Codes int has_nonzero_masked(long x, long mask) urn!!(x & mask); # x in %rdi, y in %rsi, urn value in %eax has_nonzero_masked: xorl %eax, %eax testq %rsi, %rdi setne %al int
More informationMachine-Level Programming II: Control Flow
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures Fabián E. Bustamante, Spring 2010 Processor state (ia32, partial) Information about currently
More informationThis is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.
BBC Headline: Slashdot Headline: Robots join the fight against cancer Robot Snakes To Fight Cancer Via Natural Orifice Surgery This is a medical robot, guided by a skilled surgeon and designed to get to
More informationUniversity 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 informationx86-64 Programming III
x86-64 Programming III CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://xkcd.com/1652/ Administrivia Homework 2 due Wednesday (7/11) Lab
More informationRegion 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 informationCompiling C Programs Into X86-64 Assembly Programs
CSE 2421: Systems I Low-Level Programming and Computer Organization Compiling C Programs Into X86-64 Assembly Programs Part B: If Else, Loops, Recursion & Switch Presentation L Read/Study: Bryant 3.6 Gojko
More informationMachine- 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 informationMachine-level Programs Procedure
Computer Systems Machine-level Programs Procedure Han, Hwansoo Mechanisms in Procedures Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value
More informationMachine-Level Programming III: Procedures
Machine-Level Programming III: Procedures CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN Slides adapted from Bryant & O Hallaron s slides Mechanisms in Procedures Passing control
More informationCS429: Computer Organization and Architecture
CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 CS429 Slideset 9: 1 Mechanisms in Procedures
More informationControl flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements
Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements 1 Conditionals and Control Flow Two key pieces 1. Comparisons and tests: check conditions
More informationAssembly Programming IV
Assembly Programming IV CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui The Data That Turned the World Upside Down The company
More informationBryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon
Carnegie Mellon Machine-Level Programming III: Procedures 15-213/18-213/14-513/15-513: Introduction to Computer Systems 7 th Lecture, September 18, 2018 Today Procedures Mechanisms Stack Structure Calling
More informationMachine 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 informationProcedures and the Call Stack
Procedures and the Call Stack Topics Procedures Call stack Procedure/stack instructions Calling conventions Register-saving conventions Why Procedures? Why functions? Why methods? int contains_char(char*
More informationHomework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation
Homework 0: 2.84 Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation Homework 0: 2.84 Given: k-bit exponent, n-bit fraction 7.0: 0111 = 1.11 2
More informationx86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia
x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/648/
More informationIA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping
Lecture 5 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
More informationGiving credit where credit is due
CSCE 230J Computer Organization Machine-Level Programming III: Procedures 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 informationIA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing
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 IA32 Region
More informationMachine- Level Representa2on: Procedure
Machine- Level Representa2on: Procedure CSCI 2021: Machine Architecture and Organiza2on Pen- Chung Yew Department Computer Science and Engineering University of Minnesota With Slides from Bryant, O Hallaron
More informationMachine Program: Procedure. Zhaoguo Wang
Machine Program: Procedure Zhaoguo Wang Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control int Q(int i) { int t, z; return z; Requirements of procedure calls? P() { y = Q(x); y++;
More informationAssembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University
Assembly II: Control Flow Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 Processor State %eax %edx Temporary data Location of runtime stack
More informationMachine-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 informationCS241 Computer Organization Spring Loops & Arrays
CS241 Computer Organization Spring 2015 Loops & Arrays 2-26 2015 Outline! Loops C loops: while, for, do-while Translation to jump to middle! Arrays Read: CS:APP2 Chapter 3, sections 3.6 3.7 IA32 Overview
More informationMachine 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 informationASSEMBLY II: CONTROL FLOW. Jo, Heeseung
ASSEMBLY II: CONTROL FLOW Jo, Heeseung IA-32 PROCESSOR STATE Temporary data Location of runtime stack %eax %edx %ecx %ebx %esi %edi %esp %ebp General purpose registers Current stack top Current stack frame
More informationhnp://
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 informationAssembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University
Assembly II: Control Flow Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Processor State (x86-64) RAX 63 31 EAX 0 RBX EBX RCX RDX ECX EDX General-purpose
More informationAssembly II: Control Flow
Assembly II: Control Flow Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu)
More informationMachine- 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 informationRoadmap. Java: Assembly language: OS: Machine code: Computer system:
Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp %rbp 0111010000011000
More informationCS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address
CS 107 Stack "bottom" Earlier Frames Lecture 13: Assembly Part III Argument n Friday, November 10, 2017 Computer Systems Increasing address Argument 7 Frame for calling function P Fall 2017 Stanford University
More informationPage 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing
CISC 36 Machine-Level Programming III: Procedures Sept. 22, 2 IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element
More information6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames
6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of Program Counter (Instruc. Pointer) PC/IP is used to fetch an instruction PC/IP contains the address of the next instruction The value in
More informationCS429: Computer Organization and Architecture
CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 26, 2018 at 12:02 CS429 Slideset 8: 1 Controlling Program
More informationFoundations of Computer Systems
18-600 Foundations of Computer Systems Lecture 5: Data and Machine-Level Programming I: Basics September 13, 2017 Required Reading Assignment: Chapter 3 of CS:APP (3 rd edition) by Randy Bryant & Dave
More informationMechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing
CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 Mechanisms in Procedures Passing Control
More informationThree Kinds of Instruc;ons
II: C to assembly Move instruc;ons, registers, and operands Complete addressing mode, address computa;on (leal) Arithme;c opera;ons (including some x86-64 instruc;ons) Condi;on codes Control, uncondi;onal
More informationControlling Program Flow
Controlling Program Flow Conditionals (If-statement) Loops (while, do-while, for-loops) Switch Statements New Instructions JMP CMP Conditional jumps (branches) Conditional MOV instruction 1 Conditional
More information2/12/2016. Today. Machine-Level Programming II: Control. Condition Codes (Implicit Setting) Processor State (x86-64, Partial)
Today Machine-Level Programming II: Control CSci 2021: Machine Architecture and Organization Lectures #9-11, February 8th-12th, 2016 Control: Condition codes Conditional branches Loops Switch Statements
More informationCS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook
CS356: Discussion #15 Review for Final Exam Marco Paolieri (paolieri@usc.edu) Illustrations from CS:APP3e textbook Processor Organization Pipeline: Computing Throughput and Delay n 1 2 3 4 5 6 clock (ps)
More informationRoadmap. Java: Assembly language: OS: Machine code: Computer system:
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 movq... popq ret %rbp %rsp, %rbp
More informationMachine Programming 2: Control flow
Machine Programming 2: Control flow CS61, Lecture 4 Prof. Stephen Chong September 13, 2011 Announcements Assignment 1 due today, 11:59pm Hand in at front during break or email it to cs61- staff@seas.harvard.edu
More informationCISC 360. Machine-Level Programming II: Control Flow Sep 17, class06
CISC 360 Machine-Level Programming II: Control Flow Sep 17, 2009 class06 Condition Codes 2 Setting Condition Codes (cont.) 3 Setting Condition Codes (cont.) 4 Reading Condition Codes SetX Condition Description
More informationSystems 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 informationMachine/Assembler Language Putting It All Together
COMP 40: Machine Structure and Assembly Language Programming Fall 2015 Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah
More informationStack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp
Stack Frame Components Frame pointer %rbp Stack pointer Caller Frame rguments 7+ Return ddr Old %rbp Saved Registers + Local Variables rgument Build 1 Using the Stack (4) Stack Structure long call_incr()
More informationMachine Language CS 3330 Samira Khan
Machine Language CS 3330 Samira Khan University of Virginia Feb 2, 2017 AGENDA Logistics Review of Abstractions Machine Language 2 Logistics Feedback Not clear Hard to hear Use microphone Good feedback
More informationOutline. Review: Assembly/Machine Code View. Processor State (x86-64, Par2al) Condi2on Codes (Explicit Se^ng: Compare) Condi2on Codes (Implicit Se^ng)
Outline Machine- Level Representa2on: Control CSCI 2021: Machine Architecture and Organiza2on Pen- Chung Yew Department Computer Science and Engineering University of Minnesota Control: Condi2on codes
More informationProcedure 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 informationCISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008
CISC 360 Machine-Level Programming II: Control Flow Sep 23, 2008 class06 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit
More informationPage # CISC 360. Machine-Level Programming II: Control Flow Sep 23, Condition Codes. Setting Condition Codes (cont.)
CISC 360 Machine-Level Programming II: Control Flow Sep 23, 2008 class06 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit
More informationThe Stack & Procedures
The Stack & Procedures CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/648/
More informationPrinceton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls
Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent
More informationCondition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.
Lecture 4B Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit Registers CF Carry
More informationIA32 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 informationC to Machine Code x86 basics: Registers Data movement instructions Memory addressing modes Arithmetic instructions
C to Machine Code x86 basics: Registers Data movement instructions Memory addressing modes Arithmetic instructions Program, Application Software Hardware next few weeks Programming Language Compiler/Interpreter
More informationL09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson
Assembly Programming III CSE 351 Spring 2017 Guest Lecturer: Justin Hsia Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis
More informationCF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag. ! CF set if carry out from most significant bit. "Used to detect unsigned overflow
Lecture 4B Machine-Level Programming II: Control Flow Topics! Condition Codes " Setting " Testing! Control Flow " If-then-else " Varieties of Loops " Switch Statements Condition Codes Single Bit Registers
More informationThe Hardware/Software Interface CSE351 Spring 2015
The Hardware/Software Interface CSE351 Spring 2015 Lecture 8 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang
More informationCSCI 2021: x86-64 Control Flow
CSCI 2021: x86-64 Control Flow Chris Kauffman Last Updated: Mon Mar 11 11:54:06 CDT 2019 1 Logistics Reading Bryant/O Hallaron Ch 3.6: Control Flow Ch 3.7: Procedure calls Goals Jumps and Control flow
More informationDo not turn the page until 5:10.
University of Washington Computer Science & Engineering Autumn 2018 Instructor: Justin Hsia 2018-10-29 Last Name: First Name: Student ID Number: Name of person to your Left Right All work is my own. I
More informationProcedure 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 informationCondition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics
15-213 The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements
More informationx86 Programming I CSE 351 Winter
x86 Programming I CSE 351 Winter 2017 http://xkcd.com/409/ Administrivia Lab 2 released! Da bomb! Go to section! No Luis OH Later this week 2 Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals
More informationMeet & 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 informationUniversity*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 movq... popq %rbp %rsp,
More informationx86 Programming II CSE 351 Winter
x86 Programming II CSE 351 Winter 2017 http://xkcd.com/1652/ Administrivia 2 Address Computation Instruction v leaq src, dst lea stands for load effective address src is address expression (any of the
More informationGiving credit where credit is due
CSCE 230J Computer Organization Machine-Level Programming II: Control Flow 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 informationAssembly 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 informationMachine- Level Programming IV: x86-64 Procedures, Data
Machine- Level Programming IV: x86-64 Procedures, Data Instructor: Dr. Hyunyoung Lee Based on slides provided by Randy Bryant & Dave O Hallaron 1 Today Procedures (x86-64) Arrays One- dimensional MulA-
More informationPage 1. Condition Codes CISC 360. Machine-Level Programming II: Control Flow Sep 17, Setting Condition Codes (cont.)
CISC 360 Condition Codes Machine-Level Programming II: Control Flow Sep 17, 2009 class06 2 Setting Condition Codes (cont.) Setting Condition Codes (cont.) 3 4 Page 1 Reading Condition Codes Reading Condition
More information