Machine-Level Programming (2)

Size: px
Start display at page:

Download "Machine-Level Programming (2)"

Transcription

1 Machine-Level Programming (2) Yanqiao ZHU Introduction to Computer Systems Project Future (Fall 2017) Google Camp, Tongji University

2 Outline Control Condition Codes Conditional Branches and Conditional Moves Loops Switch Statements Procedures Stack Structure Calling Conventions Passing Control Passing Data Managing Local Data

3 Conditional Branches Introduction to Computer Systems

4 Processor State (x86-64, Partial) Information about currently executing program: Temporary data (%rax, ) Location of runtime stack (%rsp) Location of current code control point (%rip, ) Status of recent tests (CF, ZF, SF, OF)

5 Condition Codes (Implicit Setting) Single bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed) Implicitly set (as side effect) of arithmetic operations. Example: addq Src,Dest t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two s-complement (signed) overflow (a>0 && b>0 && t<0) (a<0 && b<0 && t>=0) Not set by leaq instruction.

6 Condition Codes (Explicit Setting: Compare) Explicit setting by compare instruction: cmpq Src2, Src1 cmpq b,a like computing a-b without setting destination CF set if carry out from most significant bit (used for unsigned comparisons) ZF set if a == b SF set if (a-b) < 0 (as signed) OF set if two s-complement (signed) overflow (a>0 && b<0 && (a-b)<0) (a<0 && b>0 && (a-b)>0)

7 Condition Codes (Explicit Setting: Test) Explicit setting by test instruction: testq Src2, Src1 testq b,a like computing a&b without setting destination Sets condition codes based on value of Src1 & Src2. Useful to have one of the operands be a mask. ZF set when a&b == 0 SF set when a&b < 0

8 Reading Condition Codes setx instructions: Set low-order byte of destination to 0 or 1 based on combinations of condition codes. Does not alter remaining 7 bytes. setx Condition Description sete ZF Equal / Zero setne ~ZF Not Equal / Not Zero sets SF Negative setns ~SF Nonnegative setg ~(SF^OF)&~ZF Greater (Signed) setge ~(SF^OF) Greater or Equal (Signed) setl (SF^OF) Less (Signed) setle (SF^OF) ZF Less or Equal (Signed) seta ~CF&~ZF Above (unsigned) setb CF Below (unsigned)

9 Reading Condition Codes (cont.) setx instructions: Set single byte based on combination of condition codes. One of addressable byte registers: Does not alter remaining bytes. Typically use movzbl to finish job. 32-bit instructions also set upper 32 bits to 0.

10 Reading Condition Codes (cont.) Figure 3.1: Understanding gt().

11 Jumping jx Instructions: Jump to different part of code depending on condition codes. jx Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF) ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned)

12 Expressing with Goto Code C allows goto statement. Jump to position designated by label. long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j (long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }

13 Using Conditional Moves Conditional move instructions: Instruction supports: if (Test) Dest Src Supported in post-1995 x86 processors. GCC tries to use them, but only when known to be safe. Why? Branches are very disruptive to instruction flow through pipelines. Conditional moves do not require control transfer. // C version val = Test? Then_Expr : Else_Expr; // Goto version result = Then_Expr; eval = Else_Expr; nt =!Test; if (nt) result = eval; return result;

14 Using Conditional Moves (cont.) Figure 3.2: Conditional move example.

15 Bad Cases for Conditional Move Expensive computations Bad performance: both values get computed. Only makes sense when computations are very simple. Risky computations val = Test(x)? Hard1(x) : Hard2(x); val = p? *p : 0; Unsafe: both values get computed but may have undesirable effects. Computations with side effects Illegal: must be side-effect free. val = x > 0? x*=7 : x+=3;

16 Loops Introduction to Computer Systems

17 Do-While Loop Figure 3.3: Do-while loop example.

18 Do-While Loop (cont.) Figure 3.4: Do-while loop compilation.

19 General While Translation #1 Jump-to-middle translation Used with -Og While version while (Test) Body Goto Version goto test; loop: Body test: if (Test) goto loop; done:

20 General While Translation #2 Do-while conversion Used with -O1 While version while (Test) Body Goto Version if (!Test) goto done; loop: Body if (Test) goto loop; done: Do-While Version if (!Test) goto done; do Body while(test); done:

21 Switch Statements Introduction to Computer Systems

22 Jump Table Figure 3.5: Jump table structure.

23 Switch Statement Figure 3.6: Switch statement example.

24 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. Indirect: jmp *.L4(,%rdi,8) Start of jump table:.l4. Must scale by factor of 8 (addresses are 8 bytes). Fetch target from effective Address.L4 + x * 8. Only for 0 x 6. Jump table.section.rodata.align 8.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

25 Wrapping Up Assembler Control Conditional jump Conditional move Indirect jump (via jump tables) Compiler generates code sequence to implement more complex control Standard Techniques Loops converted to do-while or jump-to-middle form Large switch statements use jump tables Sparse switch statements may use decision trees (if-elseif-elseif-else)

26 Procedures Introduction to Computer Systems

27 Mechanisms in Procedures (1/5) Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value Memory management Allocate during procedure execution Deallocate upon return P() { y = Q(x); print(y) } int Q(int i) { int t = 3 * i; int v[10]; return v[t]; }

28 Mechanisms in Procedures (2/5) Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value Memory management Allocate during procedure execution Deallocate upon return P() { y = Q(x); print(y) } int Q(int i) { int t = 3 * i; int v[10]; return v[t]; }

29 Mechanisms in Procedures (3/5) Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value Memory management Allocate during procedure execution Deallocate upon return P() { y = Q(x); print(y) } int Q(int i) { int t = 3 * i; int v[10]; return v[t]; }

30 Mechanisms in Procedures (4/5) Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value Memory management Allocate during procedure execution Deallocate upon return P() { y = Q(x); print(y) } int Q(int i) { int t = 3 * i; int v[10]; return v[t]; }

31 Mechanisms in Procedures (5/5) Mechanisms all implemented with machine instructions. x86-64 implementation of a procedure uses only those mechanisms require. Machine instructions implement the mechanisms, but the choices are determined by designers. These choices make up the Application Binary Interface (ABI).

32 x86-64 Stack (1/3) Region of memory managed with stack discipline. Memory viewed as array of bytes. Different regions have different purposes. Like ABI, a policy decision. stack code

33 x86-64 Stack (2/3) Region of memory managed with stack discipline. stack bottom stack code Stack Pointer: %rsp stack top

34 x86-64 Stack (3/3) Region of memory managed with stack discipline. stack bottom Grows toward lower addresses. Register %rsp contains lowest stack address. Address of top element Stack Pointer: %rsp stack top increasing addresses stack grows down

35 x86-64 Stack: Push pushq Src Fetch operand at Src val Decrement %rsp by 8 Write operand at address given by %rsp stack bottom increasing addresses Stack Pointer: %rsp stack top stack grows down

36 x86-64 Stack: Push pushq Src Fetch operand at Src val Decrement %rsp by 8 Write operand at address given by %rsp stack bottom increasing addresses Stack Pointer: %rsp 8 stack top stack grows down

37 x86-64 Stack: Push pushq Src Fetch operand at Src Decrement %rsp by 8 Write operand at address given by %rsp stack bottom increasing addresses Stack Pointer: %rsp 8 val stack grows down stack top

38 x86-64 Stack: Pop popq Dest Read value at address given by %rsp Increment %rsp by 8 Store value at Dest (usually a register) stack bottom increasing addresses stack grows down Stack Pointer: %rsp val stack top

39 x86-64 Stack: Pop popq Dest Read value at address given by %rsp Increment %rsp by 8 Store value at Dest (usually a register) stack bottom increasing addresses Stack Pointer: %rsp +8 val stack grows down stack top

40 x86-64 Stack: Pop popq Dest Read value at address given by %rsp val Increment %rsp by 8 Store value at Dest (usually a register) stack bottom increasing addresses The memory doesn t change, only the value of %rsp. Stack Pointer: %rsp val stack top stack grows down

41 Code Examples void multstore(long x, long y, long *dest) { long t = mult2(x, y); *dest = t; } <multstore>: : push %rbx # Save %rbx : mov %rdx, %rbx # Save dest : callq <mult2> # mult2(x,y) : mov %rax, (%rbx) # Save at dest 40054c: pop %rbx # Restore %rbx 40054d: retq # Return

42 Code Examples (cont.) long mult2(long a, long b) { long s = a * b; return s; } <mult2>: : mov %rdi, %rax # a : imul %rsi, %rax # a * b : retq # Return

43 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 the next instruction right after call Example from disassembly Procedure return: ret Pop address from stack Jump to address

44 Control Flow Example (1/4) 0x130 0x128 0x <multstore>: : callq <mult2> : mov %rax, (%rbx) %rsp %rip 0x120 0x <mult2>: : mov %rdi, %rax : retq

45 Control Flow Example (2/4) 0x130 0x128 0x120 0x118 0x <multstore>: : callq <mult2> : mov %rax, (%rbx) %rsp %rip 0x118 0x <mult2>: : mov %rdi, %rax : retq

46 Control Flow Example (3/4) 0x130 0x128 0x120 0x118 0x <multstore>: : callq <mult2> : mov %rax, (%rbx) %rsp %rip 0x118 0x <mult2>: : mov %rdi, %rax : retq

47 Control Flow Example (4/4) 0x130 0x128 0x <multstore>: : callq <mult2> : mov %rax, (%rbx) %rsp %rip 0x120 0x <mult2>: : mov %rdi, %rax : retq

48 Procedure Data Flow Registers First 6 arguments Return value %rdi %rsi %rdx %rcx %r8 %r9 %rax Stack Only allocate stack space when needed Arg n Arg 8 Arg 7

49 Recall: x86-64 Integer Registers

50 Data Flow Examples void multstore(long x, long y, long *dest) { long t = mult2(x, y); *dest = t; } <multstore>: # x in %rdi, y in %rsi, dest in %rdx : mov %rdx,%rbx # Save dest : callq <mult2> # mult2(x,y) # t in %rax : mov %rax,(%rbx) # Save at dest

51 Data Flow Examples (cont.) long mult2(long a, long b) { long s = a * b; return s; } <mult2>: # a in %rdi, b in %rsi : mov %rdi,%rax # a : imul %rsi,%rax # a * b # s in %rax : retq # Return

52 Stack-Based Languages Languages that support recursion E.g., C, Pascal, Java Code must be Reentrant. Multiple simultaneous instantiations of single procedure. Need some place to store state of each instantiation. Arguments Local variables Return pointer

53 Stack-Based Languages (cont.) Stack discipline State for given procedure needed for limited time. From when called to when return. Callee returns before caller does. Stack allocated in Frames. State for single procedure instantiation

54 Call Chain Example (1/10) Example Call Chain: yoo() { who(); } who() { ami(); ami(); } ami() { ami(); } yoo who ami ami ami Figure 3.7: Procedure ami() is recursive.

55 Stack Frames Contents Return information Local storage (if needed) Temporary space (if needed) Management Space allocated when enter procedure. Set-up code Includes push by call instruction Deallocated when return. Finish code Includes pop by ret instruction Frame Pointer: %rbp (optional) Stack Pointer: %rsp Previous Frame Frame for proc Stack Top

56 Call Chain Example (2/10) Stack yoo() { who(); } yoo who ami ami Previous yoo %rbp %rsp ami

57 Call Chain Example (3/10) Stack yoo() who() { { who(); ami(); } ami(); } yoo who ami ami ami Previous yoo who %rbp %rsp

58 Call Chain Example (4/10) Stack yoo() who() { { ami() who(); ami(); { } ami(); ami(); } } yoo who ami ami ami Previous yoo who ami %rbp %rsp

59 Call Chain Example (5/10) Stack yoo() yoo Previous who() { { who yoo ami() who(); ami(); { ami() ami ami who } { ami(); ami(); ami(); } ami ami } } ami %rbp %rsp

60 Call Chain Example (6/10) Stack yoo() who() { { ami() who(); ami(); { } ami(); ami(); } } yoo who ami ami ami Previous yoo who ami %rbp %rsp

61 Call Chain Example (7/10) Stack yoo() who() { { who(); ami(); } ami(); } yoo who ami ami ami Previous yoo who %rbp %rsp

62 Call Chain Example (8/10) Stack yoo() who() { { ami() who(); ami(); { } ami(); ami(); } } yoo who ami ami ami Previous yoo who ami %rbp %rsp

63 Call Chain Example (9/10) Stack yoo() who() { { who(); ami(); } ami(); } yoo who ami ami ami Previous yoo who %rbp %rsp

64 Call Chain Example (10/10) Stack yoo() { who(); } yoo who ami ami Previous yoo %rbp %rsp ami

65 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 (optional) Caller Stack Frame Return address Pushed by call instruction Arguments for this call Arguments 7+ Return Address Old %rbp Saved Registers + Local Variables Argument Build (optional) Caller Frame Frame Pointer (optional) %rbp Stack Pointer %rsp Figure 3.8: x86-64/linux stack frame.

66 Example: incr long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x; } incr: movq addq movq ret (%rdi), %rax %rax, %rsi %rsi, (%rdi) Register %rdi %rsi %rax Use(s) Argument p Argument val, y x, Return value

67 Example: Calling incr (1/6) long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } Initial Stack Structure Rtn address %rsp Resulting Stack Structure call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Rtn address Unused %rsp+8 %rsp

68 Example: Calling incr (2/6) long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Stack Structure Rtn address Aside 1: movl $3000, %esi Remember, movl -> %exx zeros out high order %rsp+8 32 bits. Why use movl instead of movq? 1 byte shorter. Aside 2: leaq 8(%rsp), %rdi Unused %rsp Computes %rsp+8 Actually, used for what it is meant! Register %rdi Use(s) &v1 %rsi 3000

69 Example: Calling incr (3/6) long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } Stack Structure Rtn address call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Unused Register Use(s) %rdi &v1 %rsi 3000 %rsp+8 %rsp

70 Example: Calling incr (4/6) long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Stack Structure Rtn address Unused Register Use(s) %rax Return value %rsp+8 %rsp

71 Stack Structure Example: Calling incr (5/6) long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } Rtn address Unused %rsp+8 %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Register Use(s) %rax Return value Updated Stack Structure Rtn address %rsp

72 Example: Calling incr (6/6) Updated Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } Rtn address %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Register Use(s) %rax Return value Final Stack Structure %rsp

73 Register Saving Conventions When procedure yoo calls who: yoo is the caller. who is the callee. Can register be used for temporary storage? Contents of register %rdx overwritten by who. This could be trouble something should be done! Need some coordination. yoo: movq $15213, %rdx call who addq %rdx, %rax ret who: subq $18213, %rdx ret

74 Register Saving Conventions (cont.) When procedure yoo calls who: yoo is the caller. who is the callee. Conventions Caller Saved Caller saves temporary values in its frame before the call. Callee Saved Callee saves temporary values in its frame before using. Callee restores them before returning to caller.

75 x86-64 Linux Register Usage %rax Return value, also caller-saved Can be modified by procedure %rdi,, %r9 Arguments, also caller-saved Can be modified by procedure %r10, %r11 Caller-saved Can be modified by procedure %rax %rdi %rsi %rdx %rcx %r8 %r9 %r10 %r11 Return value Arguments Caller-saved temporaries

76 x86-64 Linux Register Usage (cont.) %rbx, %r12, %r13, %r14 Callee-saved Callee must save and restore %rbp Callee-saved Callee must save and restore May be used as frame pointer Can mix and match %rsp Special form of callee save Restored to original value upon exit from procedure Special %rbx %r12 %r13 %r14 %rbp %rsp Callee-saved Temporaries

77 Callee-Saved Example (1/8) x comes in register %rdi. We need %rdi for the call to incr. Where should be put x, so we can use it after the call to incr? long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } Initial Stack Structure Rtn address %rsp

78 Callee-Saved Example (2/8) long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Initial Stack Structure Rtn address Resulting Stack Structure Rtn address Saved %rbx %rsp %rsp

79 Callee-Saved Example (3/8) Initial Stack Structure long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } Rtn address Saved %rbx %rsp call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Resulting Stack Structure Rtn address Saved %rbx %rsp + 8 %rsp

80 Callee-Saved Example (4/8) long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret x saved in %rbx A callee saved register. Stack Structure Rtn address Saved %rbx %rsp + 8 %rsp

81 Callee-Saved Example (5/8) long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Stack Structure Rtn address Saved %rbx Unused %rsp + 8 %rsp

82 Callee-Saved Example (6/8) long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret x is safe in %rbx Return result in %rax Stack Structure Rtn address Saved %rbx Unused %rsp + 8 %rsp

83 Callee-Saved Example (7/8) long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Stack Structure Rtn address Saved %rbx Unused %rsp

84 Initial Stack Structure Callee-Saved Example (8/8) long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x + v2; } Rtn address Saved %rbx Unused %rsp call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Final Stack Structure Rtn address Saved %rbx Unused %rsp

85 Wrapping Up Important Points Stack is the right data structure for procedure call/return. If P calls Q, then Q returns before P. Recursion (& mutual recursion) handled by normal calling conventions. Can safely store values in local stack frame and in callee-saved registers. Put function arguments at top of stack. Result return in %rax. Pointers are addresses of values. On stack or global

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Bryant 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 information

Machine-Level Programming II: Control

Machine-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 information

Machine-Level Programming III: Procedures

Machine-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 information

Machine-Level Programming II: Control

Machine-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 information

Machine-level Programs Procedure

Machine-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 information

Machine- Level Representa2on: Procedure

Machine- 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 information

Machine Level Programming: Control

Machine Level Programming: Control Machine Level Programming: Control Computer Systems Organization (Spring 2017) CSCI-UA 201, Section 3 Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU) Mohamed

More information

CS429: Computer Organization and Architecture

CS429: 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 information

2/12/2016. Today. Machine-Level Programming II: Control. Condition Codes (Implicit Setting) Processor State (x86-64, Partial)

2/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 information

CS429: Computer Organization and Architecture

CS429: 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 information

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Carnegie 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 information

CS367. Program Control

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

Outline. Review: Assembly/Machine Code View. Processor State (x86-64, Par2al) Condi2on Codes (Explicit Se^ng: Compare) Condi2on Codes (Implicit Se^ng)

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

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Mechanisms 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 information

Machine-Level Programming II: Control

Machine-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 information

Assembly Programming IV

Assembly 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 information

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

L09: 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 information

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

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly 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 information

Assembly II: Control Flow

Assembly 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 information

x86-64 Programming III

x86-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 information

Procedures and the Call Stack

Procedures 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 information

x86 Programming II CSE 351 Winter

x86 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 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 Mechanisms in Procedures Passing control To beginning of procedure code

More information

x86-64 Programming II

x86-64 Programming II x86-64 Programming II CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/409/ Administrative Homework

More information

The Stack & Procedures

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

Assembly Programming III

Assembly Programming III Assembly Programming III CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui Facebook Stories puts a Snapchat clone above the News

More information

Assembly Programming IV

Assembly 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 information

Control 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 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 information

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

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme6c & Control Machine- Level Programming II: Arithme6c & Control 15-213: Introduc0on to Computer Systems 5 th Lecture, Sep. 7, 2010 Instructors: Randy Bryant and Dave O Hallaron Modified by Karen L. Karavanic 2015 1

More information

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

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

Lecture 4: x86_64 Assembly Language

Lecture 4: x86_64 Assembly Language CSCI-GA.1144-001 PAC II Lecture 4: x86_64 Assembly Language Some slides adapted (and slightly modified) from: Randy Bryant Dave O Hallaron Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com

More information

The Stack & Procedures

The Stack & Procedures The Stack & Procedures CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Homework 2 due

More information

Machine Language CS 3330 Samira Khan

Machine 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 information

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective void P() { y = Q(x); print(y); return;... int Q(int t) { int v[10]; return v[t]; Computer Systems: A Programmer s Perspective %rax %rbx 0x101 0x41 0x7FFFFA8 0x1 0x7FFFFF8 0xB5A9 0x7FFFFF0 0x789ABC 0x7FFFFE8

More information

x86-64 Programming III & The Stack

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

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Stack 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 information

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

%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 information

Foundations of Computer Systems

Foundations of Computer Systems 18-600 Foundations of Computer Systems Lecture 6: Machine-Level Programming III: Loops, Procedures, the Stack, and More September 18, 2017 Required Reading Assignment: Chapter 3 of CS:APP (3 rd edition)

More information

x86 64 Programming II

x86 64 Programming II x86 64 Programming II CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie

More information

Controlling Program Flow

Controlling 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 information

Machine Program: Procedure. Zhaoguo Wang

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

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

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

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

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

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data Processor state Information about currently executing program Temporary data %rax, %rdi, current parameters, local variables Location of runtime stack %rsp Location of current instruction %rip Status of

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

Foundations of Computer Systems

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

Machine-Level Programming II: Control Flow

Machine-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 information

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

CS 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 information

Areas for growth: I love feedback

Areas for growth: I love feedback Assembly part 2 1 Areas for growth: I love feedback Speed, I will go slower. Clarity. I will take time to explain everything on the slides. Feedback. I give more Kahoot questions and explain each answer.

More information

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

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

CSCI 2021: x86-64 Control Flow

CSCI 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 information

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code. Where We Are Source code if (b == 0) a = b; Low-level IR code Optimized Low-level IR code Assembly code cmp $0,%rcx cmovz %rax,%rdx Lexical, Syntax, and Semantic Analysis IR Generation Optimizations Assembly

More information

Assembly Language II: Addressing Modes & Control Flow

Assembly Language II: Addressing Modes & Control Flow Assembly Language II: Addressing Modes & Control Flow Learning Objectives Interpret different modes of addressing in x86 assembly Move data seamlessly between registers and memory Map data structures in

More information

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

ASSEMBLY 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 information

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

x64 Cheat Sheet Fall 2014

x64 Cheat Sheet Fall 2014 CS 33 Intro Computer Systems Doeppner x64 Cheat Sheet Fall 2014 1 x64 Registers x64 assembly code uses sixteen 64-bit registers. Additionally, the lower bytes of some of these registers may be accessed

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Intel x86

More information

Compiling C Programs Into X86-64 Assembly Programs

Compiling 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 information

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics

Condition 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 information

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.

Condition 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 information

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSE351 Spring 2018, Midterm Exam April 27, 2018 CSE351 Spring 2018, Midterm Exam April 27, 2018 Please do not turn the page until 11:30. Last Name: First Name: Student ID Number: Name of person to your left: Name of person to your right: Signature indicating:

More information

Intel x86-64 and Y86-64 Instruction Set Architecture

Intel x86-64 and Y86-64 Instruction Set Architecture CSE 2421: Systems I Low-Level Programming and Computer Organization Intel x86-64 and Y86-64 Instruction Set Architecture Presentation J Read/Study: Bryant 3.1 3.5, 4.1 Gojko Babić 03-07-2018 Intel x86

More information

CF 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

CF 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 information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent instructions and conventions 2 Function Call Problems (1) Calling and returning

More information

Systems Programming and Computer Architecture ( )

Systems Programming and Computer Architecture ( ) Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 AS 2016 Compiling C Control Flow 1 8: Compiling C

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 5 LAST TIME Began exploring x86-64 instruction set architecture 16 general-purpose registers Also: All registers are 64 bits wide rax-rdx are

More information

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

Machine- Level Programming II: Arithme c & Control

Machine- Level Programming II: Arithme c & Control Machine- Level Programming II: Arithme c & Control 15-213 / 18-213: Introduc on to Computer Systems 6 th Lecture, Sep. 12, 2013 Instructors: Randy Bryant, David O Hallaron, and Greg Kesden 1 Today Complete

More information

Giving credit where credit is due

Giving 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 information

CSC 252: Computer Organization Spring 2018: Lecture 6

CSC 252: Computer Organization Spring 2018: Lecture 6 CSC 252: Computer Organization Spring 2018: Lecture 6 Instructor: Yuhao Zhu Department of Computer Science University of Rochester Action Items: Assignment 2 is out Announcement Programming Assignment

More information

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme6c & Control Machine- Level Programming II: Arithme6c & Control Computer Architecture Instructor: Norbert Lu*enberger based on the book by Randy Bryant and Dave O Hallaron 1 Today Complete addressing mode, address

More information

CISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008

CISC 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 information

Page # CISC 360. Machine-Level Programming II: Control Flow Sep 23, Condition Codes. Setting Condition Codes (cont.)

Page # 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 information

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

Homework 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 information

Chapter 3 Machine-Level Programming II Control Flow

Chapter 3 Machine-Level Programming II Control Flow Chapter 3 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 Flag

More information

Lecture 3 CIS 341: COMPILERS

Lecture 3 CIS 341: COMPILERS Lecture 3 CIS 341: COMPILERS HW01: Hellocaml! Announcements is due tomorrow tonight at 11:59:59pm. HW02: X86lite Will be available soon look for an announcement on Piazza Pair-programming project Simulator

More information

Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu

Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu Computer Architecture I: Outline and Instruction Set Architecture CENG331 - Computer Organization Murat Manguoglu Adapted from slides of the textbook: http://csapp.cs.cmu.edu/ Outline Background Instruction

More information

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CSE 351 Midterm Exam Spring 2016 May 2, 2015 Name: CSE 351 Midterm Exam Spring 2016 May 2, 2015 UWNetID: Solution Please do not turn the page until 11:30. Instructions The exam is closed book, closed notes (no calculators, no mobile phones, no laptops,

More information

Computer Organization: A Programmer's Perspective

Computer Organization: A Programmer's Perspective A Programmer's Perspective Instruction Set Architecture Gal A. Kaminka galk@cs.biu.ac.il Outline: CPU Design Background Instruction sets Logic design Sequential Implementation A simple, but not very fast

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

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

Control 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 information

Do not turn the page until 5:10.

Do 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 information

System Programming and Computer Architecture (Fall 2009)

System Programming and Computer Architecture (Fall 2009) System Programming and Computer Architecture (Fall 2009) Recitation 2 October 8 th, 2009 Zaheer Chothia Email: zchothia@student.ethz.ch Web: http://n.ethz.ch/~zchothia/ Topics for Today Classroom Exercise

More information

CSE 351 Midterm Exam

CSE 351 Midterm Exam University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse February 5, 2018 CSE 351 Midterm Exam Last Name: First Name: SOLUTIONS UW Student ID Number: UW NetID (username):

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 12 CS24 MIDTERM Midterm format: 6 hour overall time limit, multiple sittings (If you are focused on midterm, clock should be running.) Open book

More information

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest Instruction Set Architecture Computer Architecture: Instruction Set Architecture CSci 2021: Machine Architecture and Organization Lecture #16, February 24th, 2016 Your instructor: Stephen McCamant Based

More information

The Hardware/Software Interface CSE351 Spring 2015

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

Review addressing modes

Review addressing modes Review addressing modes Op Src Dst Comments movl $0, %rax Register movl $0, 0x605428 Direct address movl $0, (%rcx) Indirect address movl $0, 20(%rsp) Indirect with displacement movl $0, -8(%rdi, %rax,

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