The Hardware/Software Interface CSE351 Spring 2013

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

CS241 Computer Organization Spring Addresses & Pointers

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

Credits to Randy Bryant & Dave O Hallaron

Machine-Level Programming II: Control and Arithmetic

The Hardware/Software Interface CSE351 Spring 2015

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

Sungkyunkwan University

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

Machine- Level Programming II: Arithme6c & Control

Machine-Level Programming II: Control Flow

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Machine Level Programming II: Arithmetic &Control

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

Machine Programming 2: Control flow

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

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

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

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

Machine-Level Programming II: Control

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

Machine-Level Programming II: Control

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06

Machine Level Programming: Control

Assembly II: Control Flow

Giving credit where credit is due

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

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Machine- Level Programming II: Arithme c & Control

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

Machine- Level Programming II: Arithme6c & Control

Machine- level Programming II: Control Flow

Chapter 3 Machine-Level Programming II Control Flow

x86-64 Programming II

CS241 Computer Organization Spring Loops & Arrays

x86 Programming II CSE 351 Winter

CS367. Program Control

Three Kinds of Instruc;ons

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Assembly Programming III

CS429: Computer Organization and Architecture

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

Controlling Program Flow

Machine-Level Programming (2)

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

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

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

Data Representa/ons: IA32 + x86-64

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

Process Layout and Function Calls

x86 64 Programming II

System Programming and Computer Architecture (Fall 2009)

Machine-Level Programming II: Control

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

CSC 8400: Computer Systems. Machine-Level Representation of Programs

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

Systems Programming and Computer Architecture ( )

Assembly Language: IA-32 Instructions

Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Machine Language CS 3330 Samira Khan

Sungkyunkwan University

Process Layout, Function Calls, and the Heap

hnp://

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

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

Second Part of the Course

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

CS61 Section Solutions 3

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

Credits and Disclaimers

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

x64 Cheat Sheet Fall 2014

Credits and Disclaimers

Machine-Level Programming I: Introduction

How Software Executes

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

Machine Programming 3: Procedures

CS241 Computer Organization Spring 2015 IA

CSE351 Spring 2018, Midterm Exam April 27, 2018

Credits and Disclaimers

Assembly Language II: Addressing Modes & Control Flow

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

Instruction Set Architecture

Instruction Set Architecture

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

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

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

Instruction Set Architecture

Machine Representa/on of Programs: Control Flow cont d. Previous lecture. Do- While loop. While- Do loop CS Instructors: Sanjeev Se(a

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

Handout #2: Machine-Level Programs

CPS104 Recitation: Assembly Programming

Do not turn the page until 5:10.

The Hardware/Software Interface CSE351 Spring 2015

You may work with a partner on this quiz; both of you must submit your answers.

Transcription:

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 branch is sufficient to implement most control flow constructs offered in higher level languages if (condition) then... else while (condition) do while (condition) for (initialization; condition; iterative)... Unconditional branches implement some related control flow constructs break, continue In x86, we ll refer to branches as jumps (either conditional or unconditional)

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

5 Processor State (IA32, Partial) Information about currently executing program Temporary data ( %eax, ) Location of runtime stack ( %ebp,%esp) Location of current code control point ( %eip ) Status of recent tests ( CF,ZF,SF,OF ) %eax %ecx %edx %ebx %esi %edi %esp %ebp %eip CF ZF SF OF General purpose registers Current stack top Current stack frame Instruction pointer Condition codes

6 Condition Codes (Implicit Setting) Single-bit registers CF Carry Flag (for unsigned) ZF Zero Flag SF Sign Flag (for signed) OF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmetic operations Example: addl/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 lea instruction (beware!) Full documentation (IA32): http://www.jegerlehner.ch/intel/intelcodetable.pdf

7 Condition Codes (Explicit Setting: Compare) Single-bit registers CF Carry Flag (for unsigned) ZF Zero Flag SF Sign Flag (for signed) Explicit Setting by Compare Instruction cmpl/cmpq Src2,Src1 OF Overflow Flag (for signed) cmpl 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)

8 Condition Codes (Explicit Setting: Test) Single-bit registers CF Carry Flag (for unsigned) ZF Zero Flag Explicit Setting by Test instruction SF Sign Flag (for signed) OF Overflow Flag (for signed) testl/testq Src2,Src1 testl 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 if a&b == 0 SF set if a&b < 0 testl %eax, %eax Sets SF and ZF, check if eax is +,0,-

9 Reading Condition Codes SetX Instructions Set a single byte to 0 or 1 based on combinations of condition codes 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)

Reading Condition Codes (Cont.) SetX Instructions: Set single byte to 0 or 1 based on combination of condition codes One of 8 addressable byte registers Does not alter remaining 3 bytes Typically use movzbl to finish job %eax %ecx %edx %ebx %esi %ah %al %ch %cl %dh %dl %bh %bl int gt (int x, int y) return x > y; %edi %esp %ebp Body: y at 12(%ebp), x at 8(%ebp) movl 12(%ebp),%eax cmpl %eax,8(%ebp) setg %al movzbl %al,%eax # eax = y # Compare x : y # al = x > y # Zero rest of %eax What does each of these instructions do? 10

Reading Condition Codes (Cont.) SetX Instructions: Set single byte to 0 or 1 based on combination of condition codes One of 8 addressable byte registers Does not alter remaining 3 bytes Typically use movzbl to finish job %eax %ecx %edx %ebx %esi %ah %al %ch %cl %dh %dl %bh %bl int gt (int x, int y) return x > y; %edi %esp %ebp Body: y at 12(%ebp), x at 8(%ebp) movl 12(%ebp),%eax # eax = y cmpl %eax,8(%ebp) # Compare x and y setg %al # al = x > y movzbl %al,%eax # Zero rest of %eax (x y) 11

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

13 Conditional Branch Example int absdiff(int x, int y) int result; if (x > y) result = x-y; else result = y-x; return result; absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle.l7 subl %eax, %edx movl %edx, %eax.l8: leave ret.l7: subl %edx, %eax jmp.l8 Setup Body1 Finish Body2

14 Conditional Branch Example (Cont.) int absdiff(int x, int y) int result; if (x > y) result = x-y; else result = y-x; return result; int goto_ad(int x, int y) int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; C allows goto as means of transferring control Closer to machine-level programming style Generally considered bad coding style

15 Conditional Branch Example (Cont.) int goto_ad(int x, int y) int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; int x int y %edx %eax absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle.l7 subl %eax, %edx movl %edx, %eax.l8: leave ret.l7: subl %edx, %eax jmp.l8

16 Conditional Branch Example (Cont.) int goto_ad(int x, int y) int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; int x int y %edx %eax absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle.l7 subl %eax, %edx movl %edx, %eax.l8: leave ret.l7: subl %edx, %eax jmp.l8

17 Conditional Branch Example (Cont.) int goto_ad(int x, int y) int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; int x int y %edx %eax absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle.l7 subl %eax, %edx movl %edx, %eax.l8: leave ret.l7: subl %edx, %eax jmp.l8

18 Conditional Branch Example (Cont.) int goto_ad(int x, int y) int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; int x int y %edx %eax absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle.l7 subl %eax, %edx movl %edx, %eax.l8: leave ret.l7: subl %edx, %eax jmp.l8

19 Conditional Branch Example (Cont.) int goto_ad(int x, int y) int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; int x int y %edx %eax absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle.l7 subl %eax, %edx movl %edx, %eax.l8: leave ret.l7: subl %edx, %eax jmp.l8

20 General Conditional Expression Translation C Code val = Test? Then-Expr : Else-Expr; result = x>y? x-y : y-x; if (Test) val = Then-Expr; else val = Else-Expr; Goto Version nt =!Test; if (nt) goto Else; val = Then-Expr; Done:... 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 How might you make this more efficient?

Conditionals: x86-64 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 # eax = x movl %esi, %edx # edx = y subl %esi, %eax # eax = x-y subl %edi, %edx # edx = y-x cmpl %esi, %edi # x:y cmovle %edx, %eax # eax=edx if <= 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 21

22 PC Relative Addressing 0x100 cmp r2, r3 0x1000 0x102 je 0x70 0x1002 0x104 0x1004 0x172 add r3, r4 0x1072 PC relative branches are relocatable Absolute branches are not

23 Compiling Loops C/Java code: while ( sum!= 0 ) <loop body> Machine code: looptop: loopdone: cmpl $0, %eax je loopdone <loop body code> jmp looptop How to compile other loops should be straightforward The only slightly tricky part is to be sure where the conditional branch occurs: top or bottom of the loop How would for(i=0; i<100; i++) be implemented?

24 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

25 Do-While Loop Compilation Goto Version int fact_goto(int x) int result = 1; Assembly Registers: %edx %eax result fact_goto: pushl %ebp # Setup movl %esp,%ebp # Setup movl $1,%eax # eax = 1 movl 8(%ebp),%edx # edx = x x loop: result *= x; x = x-1; if (x > 1) goto loop;.l11: imull %edx,%eax # result *= x decl %edx # x-- Translation? cmpl $1,%edx # Compare x : 1 jg.l11 # if > goto loop return result; movl %ebp,%esp popl %ebp ret # Finish # Finish # Finish

26 Do-While Loop Compilation Goto Version int fact_goto(int x) int result = 1; Assembly Registers: %edx %eax result fact_goto: pushl %ebp # Setup movl %esp,%ebp # Setup movl $1,%eax # eax = 1 movl 8(%ebp),%edx # edx = x x loop: result *= x; x = x-1; if (x > 1) goto loop;.l11: imull %edx,%eax # result *= x decl %edx # x-- cmpl $1,%edx # Compare x : 1 jg.l11 # if > goto loop return result; movl %ebp,%esp popl %ebp ret # Finish # Finish # Finish

27 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

28 While Loop Translation C Code int fact_while(int x) int result = 1; while (x > 1) result *= x; x = x-1; ; return result; Goto Version int fact_while_goto(int x) int result = 1; goto middle; loop: result *= x; x = x-1; middle: if (x > 1) goto loop; return result; Used by GCC for both IA32 & x86-64 First iteration jumps over body computation within loop straight to test

29 While Loop 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

For Loop Example: Square-and-Multiply /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned int 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 1 + 2 2 p 2 + 2 n 1 p n 1 Gives: x p = z 0 z 1 2 (z 2 2 ) 2 ( ((z n 12 ) 2 ) ) 2 z i = 1 when p i = 0 z i = x when p i = 1 Complexity O(log p) n 1 times Example 3 10 = 3 2 * 3 8 = 3 2 * ((3 2 ) 2 ) 2 30

ipwr Computation /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned int 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=10 1 1 3 10=1010 2 2 1 9 5= 101 2 3 9 81 2= 10 2 4 9 6561 1= 1 2 5 59049 43046721 0 2 31

32 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 Init Test Update Body result = 1 p!= 0 p = p >> 1 if (p & 0x1) result *= x; x = x*x;

33 For While For Version for (Init; Test; Update ) Body While Version Init; while (Test ) Body Update ; Goto Version Init; goto middle; loop: Body Update ; middle: if (Test) goto loop; done:

For-Loop: Compilation 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: 34

35 Quick Review Complete memory addressing mode (%eax), 17(%eax), 2(%ebx, %ecx, 8), Arithmetic operations that do set condition codes subl %eax, %ecx # ecx = ecx + eax sall $4,%edx # edx = edx << 4 addl 16(%ebp),%ecx # ecx = ecx + Mem[16+ebp] imull %ecx,%eax # eax = eax * ecx Arithmetic operations that do NOT set condition codes leal 4(%edx,%eax),%eax # eax = 4 + edx + eax

36 Quick Review x86-64 vs. IA32 Integer registers: 16 x 64-bit vs. 8 x 32-bit movq, addq, vs. movl, addl, %rax %rbx %rcx %eax %edx %ecx %r8 %r9 %r10 %r8d %r9d %r10d movq -> move quad word or 4*16-bits x86-64: better support for passing function arguments in registers %rdx %rsi %rdi %rsp %rbp %ebx %esi %edi %esp %ebp %r11 %r12 %r13 %r14 %r15 %r11d %r12d %r13d %r14d %r15d Control Condition code registers Set as side effect or by cmp, test Used: Read out by setx instructions (setg, setle, ) Or by conditional jumps (jle.l4, je.l10, ) Or by conditional moves (cmovle %edx, %eax) CF ZF SF OF

Quick Review 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: or goto middle; loop: Body middle: if (Test) goto loop; 37

38 Summarizing 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 Large switch statements use jump tables Sparse switch statements may use decision trees (see text) Conditions in CISC CISC machines generally have condition code registers

39