Sungkyunkwan University

Similar documents
Credits to Randy Bryant & Dave O Hallaron

CS241 Computer Organization Spring Addresses & Pointers

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

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

Machine-Level Programming II: Control and Arithmetic

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

The Hardware/Software Interface CSE351 Spring 2013

Machine Level Programming II: Arithmetic &Control

Machine- Level Programming II: Arithme c & Control

Machine- Level Programming II: Arithme6c & Control

The Hardware/Software Interface CSE351 Spring 2015

Machine- Level Programming II: Arithme6c & Control

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Machine-Level Programming II: Control Flow

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

Chapter 3 Machine-Level Programming II Control Flow

Process Layout and Function Calls

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

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

Giving credit where credit is due

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

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

Process Layout, Function Calls, and the Heap

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

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

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

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

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

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

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

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

CS241 Computer Organization Spring 2015 IA

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

Machine Level Programming: Control

X86 Assembly -Data II:1

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

Machine-Level Programming Introduction

x86 Programming II CSE 351 Winter

The Hardware/Software Interface CSE351 Spring 2015

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

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

Three Kinds of Instruc;ons

Machine- level Programming II: Control Flow

x86-64 Programming II

Machine-Level Programming I: Introduction

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

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

Machine Programming 2: Control flow

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

Machine-Level Programming II: Control

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

Assembly Programming III

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

Machine-Level Programming II: Control

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

Machine-Level Programming Introduction

Machine Programming 1: Introduction

System Programming and Computer Architecture (Fall 2009)

Assembly II: Control Flow

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

x86 64 Programming II

CS367. Program Control

Machine- level Programming

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

Assembly Language: IA-32 Instructions

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

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

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

Second Part of the Course

Handout #2: Machine-Level Programs

Machine-Level Programming (2)

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

Giving credit where credit is due

Instruction Set Architecture

Instruction Set Architecture

Controlling Program Flow

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

Intel Instruction Set (gas)

CS429: Computer Organization and Architecture

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

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

Credits and Disclaimers

Machine Level Programming I: Basics

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

Instructor: Alvin R. Lebeck

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

CISC 360 Instruction Set Architecture

Instruction Set Architecture

CS61 Section Solutions 3

Machine-Level Programming II: Control

15-213/ Final Exam Notes Sheet Spring 2013!

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

Credits and Disclaimers

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

Transcription:

- 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 i ] + D ] D (constant) : displacement 1, 2, or 4 bytes R b (base register) : Any of 8 integer registers R i (index register) : Any, except for %esp Unlikely you d use %ebp, either S (scale) : 1, 2, 4, or 8 Special Cases (R b,r i ) Mem[ Reg[ R b ] + Reg[ R i ] ] D(R b,r i ) Mem[ Reg[ R b ] + Reg[ R i ]+D ] (R b,r i,s) Mem[ Reg[ R b ] + S * Reg[ R i ] ]

- 4 - %edx %ecx 0xf000 0x100 Expression Address Computation Address 0x8(%edx) 0xf000 + 0x8 0xf008 (%edx,%ecx) 0xf000 + 0x100 0xf100 (%edx,%ecx,4) 0xf000 + 4*0x100 0xf400 0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080

- 5 - leal Src, Dest Src is address mode expression Set Dest to address denoted by expression Uses Computing addresses without a memory reference e.g., translation of p = &x[i]; Computing arithmetic expressions of the form x+k*y k = 1, 2, 4, or 8 Example int mul12(int x) { return x*12; } Converted to ASM by compiler: leal (%eax,%eax,2), %eax ;t <- x+x*2 sall $2, %eax ;return t<<2

- 6 - Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches While loops

- 7 - addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest - Src imull Src,Dest Dest = Dest * Src sall Src,Dest Dest = Dest << Src sarl Src,Dest Dest = Dest >> Src arithmetic shrl Src,Dest Dest = Dest >> Src logical xorl Src,Dest Dest = Dest ^ Src andl Src,Dest Dest = Dest & Src orl Src,Dest Dest = Dest Src incl Src,Dest Dest = Dest + 1 decl Src,Dest Dest = Dest - 1 negl Src,Dest Dest = - Dest notl Src,Dest Dest = Dest ~ Src

Carnegie Mellon One Operand Instructions incl Dest Dest = Dest + 1 decl Dest Dest = Dest 1 negl Dest Dest = Dest notl Dest Dest = ~Dest See book for more instructions

- 9 - int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } arith: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish

- 10 - Stack int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Offset 16 12 8 4 0 z y x Rtn adr Old %ebp %ebp movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval)

- 11 - Stack int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } Offset 16 12 8 4 0 z y x Rtn adr Old %ebp %ebp movl 8(%ebp),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = 3*y sall $4,%edx # edx = 48*y (t4) addl 16(%ebp),%ecx # ecx = z+t1 (t2) leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5) imull %ecx,%eax # eax = t5*t2 (rval)

- 12 - int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Set Up Body Finish movl 8(%ebp),%eax # eax = x xorl 12(%ebp),%eax # eax = x^y sarl $17,%eax # eax = t1>>17 andl $8185,%eax # eax = t2 & 8185 2 13 = 8192, 2 13 7 = 8185

- 13 - Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches While loops

- 14 - 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 stop Current stack frame Instruction pointer Conditional codes

- 15 - Single bit registers CF Carry Flag (for unsigned) ZF Zero Flag Implicitly set SF Sign Flag (for signed) OF Overflow Flag (for signed) Think of it as side effect by arithmetic operations 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

- 16 - Explicit Setting by Compare Instruction cmpl/cmpq Src2,Src1 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)

- 17 - Explicit Setting by Test instruction 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 when a&b == 0 SF set when a&b < 0

- 18 - SetX Instructions Set single byte 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)

- 19 - SetX Instructions Set single byte based on combination of condition codes One of 8 addressable byte registers %eax %ecx %ax %cx %ah %ch %al %cl Does not alter remaining 3 bytes Typically use movzbl to finish job %edx %ebx %dx %bx %dh %bh %dl %bl int gt (int x, int y) { return x > y; } %esi %edi %esp movl 12(%ebp), %eax # eax = y cmpl %eax, 8(%ebp) # Compare x : y setg %al # al = x > y movzbl %al, %eax # Zero rest of %eax %ebp Note inverted ordering!

- 20 - SetX Instructions: Set single byte based on combination of condition codes Does not alter remaining 3 bytes int gt (long x, long y) { return x > y; } long lgt (long x, long y) { return x > y; } ASM is the same for both (gt and lgt) xorl %eax, %eax # eax = 0 cmpq %rsi, %rdi # Compare x and y setg %al # al = x > y Is %rax zero? Yes: 32-bit instructions set high order 32 bits to 0!

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

- 22 - Jump targets are written using symbolic labels PC-relative Difference between address of target instruction and address of the instruction immediately following jump This offset can be encoded using one, two or four bytes Absolute Four bytes to directly specify the target

- 23 - jle.l4.p2align 4,,7.L5: movl %edx, %eax sarl $1, %eax subl %eax, %edx testl %edx, %edx jg.l5.l4: movl %edx, %eax 8: 7e 11 jle 1b <silly+0x1b> a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi 10: 89 d0 mov %edx,%eax 12: c1 f8 01 sar $0x1,%eax 15: 29 c2 sub %eax, %edx 17: 85 d2 test %edx,%edx 19: 7f f5 jg 10 <silly+0x10> 1b: 89 d0 mov %edx,%eax

- 24 - http://csapp.cs.cmu.edu/public/errata.html for errata A. What is the target of the jbe instruction below? SOL> 8048d1c: 76 da jbe XXXXXX 8048dle: eb 24 jmp 8048d44 value in PC is 0x848dle. 0xda + 0x8048dle