Assembly I: Basic Operations. Jo, Heeseung

Similar documents
ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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 I: Introduction Jan. 30, 2001

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

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

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

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

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

The Hardware/Software Interface CSE351 Spring 2015

CS241 Computer Organization Spring Addresses & Pointers

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

Machine- level Programming

X86 Assembly -Data II:1

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

Machine Programming 1: Introduction

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

Sungkyunkwan University

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

Machine-Level Programming Introduction

Giving credit where credit is due

CS241 Computer Organization Spring 2015 IA

Machine-Level Programming II: Control and Arithmetic

Machine-Level Programming Introduction

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

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

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

Credits to Randy Bryant & Dave O Hallaron

Process Layout and Function Calls

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

Process Layout, Function Calls, and the Heap

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

Machine Level Programming I: Basics

Machine-Level Programming I: Introduction

The Hardware/Software Interface CSE351 Spring 2015

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Three Kinds of Instruc;ons

Machine-level Programming (3)

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

CS429: Computer Organization and Architecture

Machine Level Programming I: Basics. Credits to Randy Bryant & Dave O Hallaron

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Today: Machine Programming I: Basics

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

Ge-ng at things on the chip you can t easily reach from C

Machine- Level Programming II: Arithme c & Control

Systems I. Machine-Level Programming I: Introduction

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Stack Discipline Jan. 19, 2018

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming: Basics

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

CS213. Machine-Level Programming III: Procedures

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

Instruction Set Architecture

Instruction Set Architecture

Machine Programming 3: Procedures

Machine-Level Programming III: Procedures

Sungkyunkwan University

CISC 360 Instruction Set Architecture

Instruction Set Architecture

Machine-Level Programming I Introduction

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

MACHINE LEVEL PROGRAMMING 1: BASICS

CS , Fall 2001 Exam 1

MACHINE-LEVEL PROGRAMMING I: BASICS

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

Machine Programming I: Basics

Machine Level Programming II: Arithmetic &Control

X86 Assembly -Procedure II:1

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

Handout #2: Machine-Level Programs

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Machine-Level Programming I Introduction

x86 Programming I CSE 351 Winter

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Credits and Disclaimers

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

Data Representa/ons: IA32 + x86-64

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

+ Machine Level Programming: x86-64 History

Machine- Level Programming III: Switch Statements and IA32 Procedures

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Assembly Language: Function Calls" Goals of this Lecture"

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

Machine- Level Programming I: Basics

The Hardware/Software Interface CSE351 Spring 2013

Chapter 3 Machine-Level Programming II Control Flow

EECS 213 Fall 2007 Midterm Exam

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

Instruction Set Architecture

CS241 Computer Organization Spring Loops & Arrays

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Today: Machine Programming I: Basics. Machine Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

Second Part of the Course

Machine- Level Programming II: Arithme6c & Control

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

Transcription:

Assembly I: Basic Operations Jo, Heeseung

Moving Data (1) Moving data: movl source, dest Move 4-byte ("long") word Lots of these in typical code Operand types Immediate: constant integer data - Like C constant, but prefixed with '$' - e.g. $0x400, $-533 - Encoded with 1, 2, or 4 bytes Register: one of 8 integer registers - But %esp and %ebp reserved for special use - Others have special uses for particular instructions Memory: 4 consecutive bytes of memory - Various "addressing modes" %eax %ebx %ecx %edx %esi %edi %esp %ebp 2

Moving Data (2) movl operand combinations Cannot do memory-memory transfers with single instruction Source Imm Destination Reg Mem movl $0x4,%eax movl $-147,(%eax) C Analog temp = 0x4; *p = -147; movl Reg Reg Mem movl %eax,%edx movl %eax,(%edx) temp2 = temp1; *p = temp; Mem Reg movl (%eax),%edx temp = *p; 3

Simple Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address e.g., movl (%ecx), %eax Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset e.g., movl 8(%ebp), %edx 4

Indexed Addressing Modes (1) 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 & %ebp S: Scale: 1, 2, 4, or 8 Special cases (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+D] Useful to access arrays and structures 5

Indexed Addressing Modes (2) Address computation example %edx %ecx 0xf000 0x0100 Expression 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 6

Swap Example void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Offset 12 yp Stack 8 xp 4 Rtn adr 0 Old %ebp %ebp -4 Old %ebx %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Setup Body Finish 7

Process Address Space Process in memory 0xffffffff stack pointer kernel virtual memory (code, data, heap, stack) user stack (created at runtime) memory invisible to user code brk run-time heap (managed by malloc) read/write segment (.data,.bss) read-only segment (.init,.text,.rodata) data code program 0 unused 8

Process Address Space 0xFFFFFFFF stack (dynamically allocated mem) SP address space 0x00000000 heap (dynamically allocated mem) static data (data segment) code (text segment) PC 9

Understanding Swap (1) void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 Offset 12 8 4 0-4 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) yp xp Rtn adr Old %ebp Old %ebx Stack # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx %ebp 10

Understanding Swap (2) Offset yp 12 xp 8 4 %ebp 0-4 123 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 11

Understanding Swap (3) Offset yp 12 xp 8 4 %ebp 0-4 123 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax %edx %ecx 0x120 %ebx %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 12

Understanding Swap (4) Offset yp 12 xp 8 4 %ebp 0-4 123 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax %edx 0x124 %ecx 0x120 %ebx %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 13

Understanding Swap (5) Offset yp 12 xp 8 4 %ebp 0-4 123 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax 456 %edx 0x124 %ecx 0x120 %ebx %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 14

Understanding Swap (6) Offset yp 12 xp 8 4 %ebp 0-4 123 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax 456 %edx 0x124 %ecx 0x120 %ebx 123 %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 15

Understanding Swap (7) Offset yp 12 xp 8 4 %ebp 0-4 456 456 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax 456 %edx 0x124 %ecx 0x120 %ebx 123 %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 16

Understanding Swap (8) Offset yp 12 xp 8 4 %ebp 0-4 456 123 0x120 0x124 Rtn adr Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 %eax 456 %edx 0x124 %ecx 0x120 %ebx 123 %esi %edi %esp %ebp 0x104 movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) Register Allocation (By compiler) Register %ecx %edx %eax %ebx Variable yp xp t1 t0 # ecx = yp # edx = xp # eax = *yp (t1) # ebx = *xp (t0) # *xp = eax # *yp = ebx 17

Arithmetic/Logical Ops. (1) Two operands instructions addl Src, Dest Dest = Dest + Src subl Src, Dest Dest = Dest - Src mull Src, Dest Dest = Dest * Src (unsigned) imull Src, Dest Dest = Dest * Src (signed) sall Src, Dest Dest = Dest << Src (= shll) sarl Src, Dest Dest = Dest >> Src (Arith.) 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 18

Arithmetic/Logical Ops. (2) One operand instructions incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = -Dest notl Dest Dest = ~Dest 19

Address Computation leal Src, Dest Src is address mode expression Set Dest to address denoted by expression leal (%edx,%edx,2),%edx x = 3 * x; Uses Computing address without doing memory reference - e.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y - k = 1, 2, 4, or 8 20

Example: arith (1) 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 21

Example: arith (2) 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; } 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 Offset 16 12 8 4 0 z y x Rtn adr Old %ebp # eax = x # edx = y # ecx = x + y (t1) # edx = 3 * y # edx = 48 * y (t4) # ecx = z + t1 (t2) # eax = x + t4 + 4 (t5) # eax = t2 * t5 (rval) Stack %ebp 22

Example: logical 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 23

Example: logical 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; } Offset 12 8 4 0 y x Rtn adr Old %ebp Stack %ebp 2 13 = 8192, 2 13 7 = 8185 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 24

CISC Properties CISC (Complex Instruction Set Computer) Instruction can reference different operand types - Immediate, register, memory Arithmetic operations can read/write memory Memory reference can involve complex computation - D(Rb, Ri, S) -> Rb + S*Ri + D - Useful for arithmetic expressions, too Instructions can have varying lengths - IA-32 instructions can range from 1 to 15 bytes 25

Summary (1) Machine level programming Assembly code is textual form of binary object code Low-level representation of program - Explicit manipulation of registers - Simple and explicit instructions - Minimal concept of data types - Many C control constructs must be implemented with multiple instructions 26

Summary (2) Machine Models Data Control mem C processor Compiler 1) char 2) int, float 3) double 4) struct, array 5) pointer 1) loops 2) conditionals 3) switch 4) Proc. call 5) Proc. return Assembly mem regs alu Stack Cond. Codes processor 1) 1 byte 2) 4 byte 3) 8 byte 4) contiguous byte allocation 5) address of initial byte 1) branch/jump 2) call 3) ret 27

Exercise doit: pushl %ebp movl %esp,%ebp movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%edx),%eax movl %eax,(%edx) void doit(int *xp, int *yp) { int t0 = *xp; *yp = t0; } movl %ebp,%esp popl %ebp ret 28

Exercise int doit (int x, int y) { int rval; int t1 = x + y; t1 = t1 * 4; return rval; } doit: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx sall $4,%ecx movl (%ecx), %eax movl %ebp,%esp popl %ebp ret 29