x86 Programming II CSE 351 Winter

Similar documents
x86 64 Programming II

x86-64 Programming II

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

Assembly Programming III

Machine-Level Programming II: Control

Machine-Level Programming II: Control

Machine Level Programming: Control

CS429: Computer Organization and Architecture

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

Assembly II: Control Flow

CS367. Program Control

The Hardware/Software Interface CSE351 Spring 2013

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

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

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

Machine-Level Programming (2)

CSC 252: Computer Organization Spring 2018: Lecture 6

Machine-Level Programming II: Control

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

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

Machine- Level Programming II: Arithme6c & Control

L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Sungkyunkwan University

Credits to Randy Bryant & Dave O Hallaron

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

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

CS241 Computer Organization Spring Addresses & Pointers

x64 Cheat Sheet Fall 2014

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

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

Machine-Level Programming II: Control and Arithmetic

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

x86 64 Programming I CSE 351 Autumn 2017 Instructor: Justin Hsia

Machine Language CS 3330 Samira Khan

Foundations of Computer Systems

Intel x86-64 and Y86-64 Instruction Set Architecture

CSE 351 Midterm Exam Spring 2016 May 2, 2015

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

Machine Level Programming II: Arithmetic &Control

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Do not turn the page until 5:10.

Lecture 4: x86_64 Assembly Language

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

x86-64 Programming III

CS201- Lecture 7 IA32 Data Access and Operations Part II

CSE 351 Midterm Exam

How Software Executes

Machine- Level Programming II: Arithme c & Control

MACHINE LEVEL PROGRAMMING 1: BASICS

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

Assembly Language II: Addressing Modes & Control Flow

Machine-Level Programming II: Control Flow

Areas for growth: I love feedback

Machine- Level Representa2on: Procedure

Controlling Program Flow

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

Computer Systems C S Cynthia Lee

Review addressing modes

x86 Programming I CSE 351 Winter

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

The Hardware/Software Interface CSE351 Spring 2015

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

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

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

Machine- Level Programming II: Arithme6c & Control

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

Do not turn the page until 11:30.

last time Assembly part 2 / C part 1 condition codes reminder: quiz

CS 3330 Exam 1 Fall 2017 Computing ID:

CSCI 2021: x86-64 Control Flow

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

CSE 351 Midterm - Winter 2015 Solutions

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

C to Machine Code x86 basics: Registers Data movement instructions Memory addressing modes Arithmetic instructions

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

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

x86 64 Programming I CSE 351 Autumn 2018 Instructor: Justin Hsia

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

Credits and Disclaimers

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

Chapter 3 Machine-Level Programming I: Basics. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

CSE 351 Midterm - Winter 2015

Giving credit where credit is due

CSE 351 Midterm - Winter 2017

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

Assembly Programming IV

System Programming and Computer Architecture (Fall 2009)

CS61 Section Solutions 3

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

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

How Software Executes

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

Machine-Level Programming I: Basics

Von Neumann Architecture. Machine Languages. Context of this Lecture. High-Level Languages. Assembly Language: Part 1. Princeton University.

Machine Program: Procedure. Zhaoguo Wang

Machine Level Programming: Basics

Transcription:

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 formats we ve seen) dst is a register Sets dst to the address computed by the src expression (does not go to memory! it just does math) Example: leaq (%rdx,%rcx,4), %rax v Uses: Computing addresses without a memory reference e.g., translation of p = &x[i]; Computing arithmetic expressions of the form x+k*i Though k can only be 1, 2, 4, or 8 3

Example: lea vs. mov Registers %rax %rbx %rcx 0x4 %rdx 0x100 %rdi %rsi Memory 123 0x400 0xF 0x8 0x10 0x1 Word Address 0x120 0x118 0x110 0x108 0x100 leaq (%rdx,%rcx,4), %rax movq (%rdx,%rcx,4), %rbx leaq (%rdx), %rdi movq (%rdx), %rsi 4

Example: lea vs. mov (solution) Registers %rax 0x110 %rbx 0x8 %rcx 0x4 %rdx 0x100 %rdi 0x100 Memory 123 0x400 0xF 0x8 0x10 0x1 Word Address 0x120 0x118 0x110 0x108 0x100 %rsi 0x1 leaq (%rdx,%rcx,4), %rax movq (%rdx,%rcx,4), %rbx leaq (%rdx), %rdi movq (%rdx), %rsi 5

L01:L09: Intro, x86combinational ProgrammingLogic II CSE369, CSE351,Autumn Winter 2016 2017 Arithmetic Example long arith(long x, long y, long z) { long t1 = x + y; long t2 = z + t1; long t3 = x + 4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } arith: leaq addq leaq salq leaq imulq ret (%rdi,%rsi), %rax %rdx, %rax (%rsi,%rsi,2), %rdx $4, %rdx 4(%rdi,%rdx), %rcx %rcx, %rax v Register Use(s) %rdi 1st argument (x) %rsi 2nd argument (y) %rdx 3rd argument (z) Interesting Instructions leaq: address computation salq: shift imulq: multiplication Only used once! 6

L01:L09: Intro, x86combinational ProgrammingLogic II Arithmetic Example long arith(long x, long y, long z) { long t1 = x + y; long t2 = z + t1; long t3 = x + 4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } arith: leaq addq leaq salq leaq imulq ret (%rdi,%rsi), %rax %rdx, %rax (%rsi,%rsi,2), %rdx $4, %rdx 4(%rdi,%rdx), %rcx %rcx, %rax # # # # # # rax/t1 rax/t2 rdx rdx/t4 rcx/t5 rax/rval CSE369, CSE351,Autumn Winter 2016 2017 Register Use(s) %rdi x %rsi y %rdx z, t4 %rax t1, t2, rval %rcx t5 = = = = = = x + y t1 + z 3 * y (3*y) * 16 x + t4 + 4 t5 * t2 7

x86 Control Flow v Condition codes v Conditional and unconditional branches v Loops v Switches 8

Control Flow Register %rdi %rsi %rax Use(s) 1 st argument (x) 2 nd argument (y) return value long max(long x, long y) { long max; if (x > y) { max = x; } else { max = y; } return max; } max:??? movq?????? movq??? ret %rdi, %rax %rsi, %rax 9

Control Flow Register %rdi %rsi %rax Use(s) 1 st argument (x) 2 nd argument (y) return value long max(long x, long y) { long max; if (x > y) { Conditional jump max = x; } else { Unconditional jump max = y; } return max; } max: if x <= y then jump to else movq %rdi, %rax jump to done else: movq %rsi, %rax done: ret 10

Conditionals and Control Flow v Conditional branch/jump Jump to somewhere else if some condition is true, otherwise execute next instruction v Unconditional branch/jump Always jump when you get to this instruction v Together, they can implement most control flow constructs in high-level languages: if (condition) then { } else { } while (condition) { } do { } while (condition) for (initialization; condition; iterative) { } switch { } 11

Jumping v j* Instructions Jumps to target (argument actually just an address) Conditional jump relies on special condition code registers Instruction Condition Description jmp target 1 Unconditional je target ZF Equal / Zero jne target ~ZF Not Equal / Not Zero js target SF Negative jns target ~SF Nonnegative jg target ~(SF^OF)&~ZF Greater (Signed) jge target ~(SF^OF) Greater or Equal (Signed) jl target (SF^OF) Less (Signed) jle target (SF^OF) ZF Less or Equal (Signed) ja target ~CF&~ZF Above (unsigned) jb target CF Below (unsigned) 12

Processor State (x86-64, partial) v 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 ) Single bit registers: Registers %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp current top of the Stack %rip %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 Program Counter (instruction pointer) CF ZF SF OF Condition Codes 13

Condition Codes (Implicit Setting) v Implicitly set by arithmetic operations (think of it as side effects) Example: addq src, dst t = a+b CF=1 if carry out from MSB (unsigned overflow) ZF=1 if t==0 SF=1 if t<0 (assuming signed, actually just if MSB is 1) OF=1 if two s complement (signed) overflow (a>0 && b>0 && t<0) (a<0 && b<0 && t>=0) Not set by lea instruction (beware!) CF Carry Flag ZF Zero Flag SF Sign Flag OF Overflow Flag 14

Condition Codes (Explicit Setting: Compare) v Explicitly set by Compare instruction cmpq src2, src1 cmpq b, a sets flags based on a-b, but doesn t store CF=1 if carry out from MSB (used for unsigned comparison) ZF=1 if a==b SF=1 if (a-b)<0 (signed) OF=1 if two s complement (signed) overflow (a>0 && b<0 && (a-b)<0) (a<0 && b>0 && (a-b)>0) CF Carry Flag ZF Zero Flag SF Sign Flag OF Overflow Flag 15

Condition Codes (Explicit Setting: Test) v Explicitly set by Test instruction testq src2, src1 testq b, a sets flags based on a&b, but doesn t store Useful to have one of the operands be a mask Can t have carry out (CF) or overflow (OF) ZF=1 if a&b==0 SF=1 if a&b<0 (signed) Example: testq %rax, %rax Tells you if (+), 0, or ( ) based on ZF and SF CF Carry Flag ZF Zero Flag SF Sign Flag OF Overflow Flag 16

Reading Condition Codes v set* Instructions Set a low-order byte to 0 or 1 based on condition codes Does not alter remaining 7 bytes Instruction Condition Description sete dst ZF Equal / Zero setne dst ~ZF Not Equal / Not Zero sets dst SF Negative setns dst ~SF Nonnegative setg dst ~(SF^OF)&~ZF Greater (Signed) setge dst ~(SF^OF) Greater or Equal (Signed) setl dst (SF^OF) Less (Signed) setle dst (SF^OF) ZF Less or Equal (Signed) seta dst ~CF&~ZF Above (unsigned > ) setb dst CF Below (unsigned < ) 17

x86-64 Integer Registers v Accessing the low-order byte: %rax %al %r8 %r8b %rbx %bl %r9 %r9b %rcx %cl %r10 %r10b %rdx %dl %r11 %r11b %rsi %sil %r12 %r12b %rdi %dil %r13 %r13b %rsp %spl %r14 %r14b %rbp %bpl %r15 %r15b 18

Reading Condition Codes L01: L09: Intro, x86 Combinational Programming Logic II v set* Instructions %rax Set a low-order byte to 0 or 1 based on condition codes Operand is byte register (e.g. al, dl) or a byte in memory Do not alter remaining bytes in register Typically use movzbl (zero-extended mov) to finish job int gt(long x, long y) { return x > y; } Register %rdi %rsi Use(s) 1 st argument (x) 2 nd argument (y) return value cmpq %rsi, %rdi # setg %al # movzbl %al, %eax # ret 19

Reading Condition Codes L01: L09: Intro, x86 Combinational Programming Logic II v set* Instructions %rax Set a low-order byte to 0 or 1 based on condition codes Operand is byte register (e.g. al, dl) or a byte in memory Do not alter remaining bytes in register Typically use movzbl (zero-extended mov) to finish job int gt(long x, long y) { return x > y; } Register %rdi %rsi Use(s) 1 st argument (x) 2 nd argument (y) return value cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %eax # Zero rest of %rax ret 20

Aside: movz and movs movz src, regdest movs src, regdest Move with zero extension Move with sign extension Copy from a smaller source value to a larger destination Source can be memory or register; Destination must be a register Fill remaining bits of dest with zero (movz) or sign bit (movs) movzsd / movssd: S size of source (b = 1 byte, w = 2) D size of dest (w = 2 bytes, l = 4, q = 8) Example: movzbq %al, %rbx 0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0xFF %rax 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF %rbx 21

Aside: movz and movs movz src, regdest movs src, regdest Move with zero extension Move with sign extension Copy from a smaller source value to a larger destination Source can be memory or register; Destination must be a register Fill remaining bits of dest with zero (movz) or sign bit (movs) movzsd / movssd: S size of source (b = 1 byte, w = 2) D size of dest (w = 2 bytes, l = 4, q = 8) Note: In x86-64, any instruction that generates a 32-bit (long word) value for a register also sets the high-order portion of the register to 0. Good example on p. 184 in the textbook. Example: movsbl (%rax), %ebx Copy 1 byte from memory into 8-byte register & sign extend it 0x00 0x00 0x7F 0xFF 0xC6 0x1F 0xA4 0xE8 %rax... 0x?? 0x?? 0x80 0x?? 0x?? 0x??... MEM 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0x80 %rbx 22

Choosing instructions for conditionals cmp b,a test a,b je Equal a == b a&b == 0 jne Not equal a!= b a&b!= 0 js Sign (negative) a&b < 0 jns (non-negative) a&b >= 0 jg Greater a > b a&b > 0 jge Greater or equal a >= b a&b >= 0 jl Less a < b a&b < 0 jle Less or equal a <= b a&b <= 0 ja Above (unsigned >) a > b jb Below (unsigned <) a < b cmp 5,(p) je: *p == 5. jne: *p!= 5. jg: *p > 5. jl: *p < 5. test a,a je: a == 0. jne: a!= 0. jg: a > 0. jl: a < 0. test a,0x1 je: a LSB == 0. jne: a LSB == 1. 23

Choosing instructions for conditionals cmp b,a test a,b je Equal a == b a&b == 0 jne Not equal a!= b a&b!= 0 js Sign (negative) a&b < 0 jns (non-negative) a&b >= 0 jg Greater a > b a&b > 0 jge Greater or equal a >= b a&b >= 0 jl Less a < b a&b < 0 jle Less or equal a <= b a&b <= 0 ja Above (unsigned >) a > b jb Below (unsigned <) a < b Register %rdi %rsi %rax if (x < 3) { return 1; } return 2; Use(s) argument x argument y return value cmpq $3, %rdi jge T2 T1: # x < 3: movq $1, %rax ret T2: #!(x < 3): movq $2, %rax ret 24

L01:L09: Intro, x86combinational ProgrammingLogic II Your Turn! long absdiff(long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } v CSE369, CSE351,Autumn Winter 2016 2017 Register Use(s) %rdi 1st argument (x) %rsi 2nd argument (y) %rax return value absdiff: # x > y: movq subq ret.l4: movq subq ret %rdi, %rax %rsi, %rax # x <= y: %rsi, %rax %rdi, %rax Can view in provided control.s gcc Og -S fno-if-conversion control.c 25

L01:L09: Intro, x86combinational ProgrammingLogic II Your Turn! (solution) long absdiff(long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } v CSE369, CSE351,Autumn Winter 2016 2017 Register Use(s) %rdi 1st argument (x) %rsi 2nd argument (y) %rax return value absdiff: cmpq %rsi, %rdi jle.l4 # x:y # x > y: movq subq ret.l4: movq subq ret %rdi, %rax %rsi, %rax # x <= y: %rsi, %rax %rdi, %rax Can view in provided control.s gcc Og -S fno-if-conversion control.c 26

Choosing instructions for conditionals cmp b,a test a,b je Equal a == b a&b == 0 jne Not equal a!= b a&b!= 0 js Sign (negative) a&b < 0 jns (non-negative) a&b >= 0 jg Greater a > b a&b > 0 jge Greater or equal a >= b a&b >= 0 jl Less a < b a&b < 0 jle Less or equal a <= b a&b <= 0 ja Above (unsigned >) a > b jb Below (unsigned <) a < b if (x < 3 && x == y) { return 1; } else { return 2; } cmpq $3, %rdi setl %al cmpq %rsi, %rdi sete %bl testb %al, %bl je T2 T1: # x < 3 && x == y: movq $1, %rax ret T2: # else movq $2, %rax ret 27

Summary v lea is address calculation instruction Does NOT actually go to memory Used to compute addresses or some arithmetic expressions v Control flow in x86 determined by status of Condition Codes Showed Carry, Zero, Sign, and Overflow, though others exist Set flags with arithmetic instructions (implicit) or Compare and Test (explicit) Set instructions read out flag values Jump instructions use flag values to determine next instruction to execute 28