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

Similar documents
CS241 Computer Organization Spring Addresses & Pointers

Sungkyunkwan University

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

Machine-Level Programming II: Control and Arithmetic

Credits to Randy Bryant & Dave O Hallaron

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

The Hardware/Software Interface CSE351 Spring 2015

The Hardware/Software Interface CSE351 Spring 2013

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme c & Control

Machine- Level Programming II: Arithme6c & Control

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

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

Machine Level Programming II: Arithmetic &Control

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

The Hardware/Software Interface CSE351 Spring 2015

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

X86 Assembly -Data II:1

Three Kinds of Instruc;ons

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

Process Layout and Function Calls

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

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

Machine- level Programming

Machine-Level Programming II: Control Flow

Process Layout, Function Calls, and the Heap

Machine-Level Programming I: Introduction

Chapter 3 Machine-Level Programming II Control Flow

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

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

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

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

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

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

Giving credit where credit is due

Machine-Level Programming Introduction

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

CS241 Computer Organization Spring 2015 IA

Machine Programming 1: Introduction

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

Giving credit where credit is due

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

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

Machine-Level Programming Introduction

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

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

Machine Level Programming: Control

Machine- level Programming II: Control Flow

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

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

Instruction Set Architecture

Instruction Set Architecture

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

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

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

Machine Programming 2: Control flow

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

Machine Level Programming I: Basics

Handout #2: Machine-Level Programs

System Programming and Computer Architecture (Fall 2009)

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

x86 Programming II CSE 351 Winter

CISC 360 Instruction Set Architecture

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

Instruction Set Architecture

Machine-Level Programming II: Control

Machine-Level Programming II: Control

Machine Language CS 3330 Samira Khan

x86 64 Programming II

x86-64 Programming II

Instruction Set Architecture

Assembly Language: Function Calls

Machine-level Programming (3)

Second Part of the Course

Machine- Level Programming: Basics

Assembly Language: Function Calls" Goals of this Lecture"

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

CS367. Program Control

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

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

Assembly Language: Function Calls" Goals of this Lecture"

ASSEMBLY III: PROCEDURES. Jo, Heeseung

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly III: Procedures. Jo, Heeseung

Assembly Programming III

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

CS213. Machine-Level Programming III: Procedures

Assembly II: Control Flow

Instructor: Alvin R. Lebeck

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

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

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

Lecture 4: x86_64 Assembly Language

Transcription:

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

addl: bitwise for signed (& unsigned) 4 bits: 1000 = -8, 0111 = 7-8 + -8 = -16 = 0 1000 + 1000 = 10000 = 0000-8 + 5 = -3 1000 + 0101 = 1101 2+5 = 7 0010 + 0101 = 0111 5+5 = 10 = -6 0101 + 0101 = 1010

Goto Edsger Dijkstra's letter Go To Statement Considered Harmful, published in the March 1968 Communications of the ACM (CACM) http://userweb.cs.utexas.edu/users/ewd/ewd02xx/ewd215.pdf

Goto Edsger Dijkstra's letter Go To Statement Considered Harmful, published in the March 1968 Communications of the ACM (CACM) http://userweb.cs.utexas.edu/users/ewd/ewd02xx/ewd215.pdf Frank Rubin Criticism of Dijkstra's letter in the March 1987 CACM 'GOTO Considered Harmful' Considered Harmful

Goto Edsger Dijkstra's letter Go To Statement Considered Harmful, published in the March 1968 Communications of the ACM (CACM) http://userweb.cs.utexas.edu/users/ewd/ewd02xx/ewd215.pdf Frank Rubin Criticism of Dijkstra's letter in the March 1987 CACM 'GOTO Considered Harmful' Considered Harmful 1987 CACM further replies, both for and against '"GOTO Considered Harmful" Considered Harmful' Considered Harmful?

Huffman coding this is an example of a huffman tree

Huffman coding this is an example of a huffman tree

Huffman coding this is an example of a huffman tree Char Freq Code space 7 111 a 4 010 e 4 000 f 3 1101 h 2 1010 i 2 1000 m 2 0111 n 2 0010 s 2 1011 t 2 0110 l 1 11001 o 1 00110 p 1 10011 r 1 11000 u 1 00111 x 1 10010

Complete Memory Addressing Modes Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant displacement 1, 2, or 4 bytes Rb: Base register: Any of 8 integer registers Ri: Index register: Any, except for %esp Unlikely you d use %ebp, either S: Scale: 1, 2, 4, or 8 Special Cases (Rb,Ri) D(Rb,Ri) (Rb,Ri,S) Mem[Reg[Rb]+Reg[Ri]] Mem[Reg[Rb]+Reg[Ri]+D] Mem[Reg[Rb]+S*Reg[Ri]]

Address Computation Examples %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

Address Computation Examples %edx %ecx 0xf000 0x100 D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] Expression Address Computation Address 0x4(%ecx) (%ecx,%ecx,8) 0xa(%edx,%edx,4) 0xc(%ecx,0xdb,2)

How are we doing? 12

Load Effective Address: Computation 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

Using leal for Arithmetic Expressions 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

Understanding arith 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 Stack %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)

Understanding arith 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 Stack %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)

Understanding arith 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 Stack %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)

Understanding arith 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 Stack %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)

Another Example 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

Another Example 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 (t1) sarl $17,%eax # eax = t1>>17 (t2) andl $8185,%eax # eax = t2 & 8185

Another Example 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 (t1) sarl $17,%eax # eax = t1>>17 (t2) andl $8185,%eax # eax = t2 & 8185

Another Example 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; } 2 13 = 8192, 2 13 7 = 8185 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 (t1) sarl $17,%eax # eax = t1>>17 (t2) andl $8185,%eax # eax = t2 & 8185

See? It s not so bad. 23

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 General purpose registers Current stack top Current stack frame Instruction pointer CF ZF SF OF Condition codes

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

Condition Codes (Explicit Setting: Compare) 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)

Condition Codes (Explicit Setting: Test) 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

Reading Condition Codes 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)