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

Similar documents
x86 Programming I CSE 351 Winter

MACHINE LEVEL PROGRAMMING 1: BASICS

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

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

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

Assembly I: Basic Operations. Jo, Heeseung

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

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

Machine-Level Programming I: Basics

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

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

Machine Level Programming: Basics

Machine Level Programming: Basics

CS201- Lecture 7 IA32 Data Access and Operations Part II

CS241 Computer Organization Spring 2015 IA

Machine Level Programming I: Basics

Intel x86-64 and Y86-64 Instruction Set Architecture

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

CSC 252: Computer Organization Spring 2018: Lecture 6

Today: Machine Programming I: Basics

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

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

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Machine-Level Programming I: Basics

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

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

Credits and Disclaimers

x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan

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

CS429: Computer Organization and Architecture

Machine-Level Programming I: Basics

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

+ Machine Level Programming: x86-64 History

The Hardware/Software Interface CSE351 Spring 2015

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

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

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

Giving credit where credit is due

Introduction to IA-32. Jo, Heeseung

Lecture 4: x86_64 Assembly Language

INTRODUCTION TO IA-32. Jo, Heeseung

Instruction Set Architecture (ISA) Data Types

MACHINE-LEVEL PROGRAMMING I: BASICS

The von Neumann Machine

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

Assembly Language Each statement in an assembly language program consists of four parts or fields.

The von Neumann Machine

Instruction Set Architectures

Complex Instruction Set Computer (CISC)

Topics of this Slideset. CS429: Computer Organization and Architecture. x86 Evolution: Programmer s View. Intel x86 Processors

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

CS 16: Assembly Language Programming for the IBM PC and Compatibles

Foundations of Computer Systems

Machine Language CS 3330 Samira Khan

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

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

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

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

How Software Executes

Addressing Modes on the x86

Sungkyunkwan University

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture (02) The Microprocessor and Its Architecture By: Dr. Ahmed ElShafee

Machine/Assembler Language Putting It All Together

Instruction Set Architectures

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

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

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

UNIT 2 PROCESSORS ORGANIZATION CONT.

Credits and Disclaimers

X86 Assembly -Data II:1

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

How Software Executes

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Lecture 3 CIS 341: COMPILERS

Assembly II: Control Flow

CS241 Computer Organization Spring Addresses & Pointers

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Credits to Randy Bryant & Dave O Hallaron

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

Machine-Level Programming II: Control and Arithmetic

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

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

The x86 Architecture

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Machine Programming 3: Procedures

Last Time: Floating Point. Intel x86 Processors. Lecture 4: Machine Basics Computer Architecture and Systems Programming ( )

Machine- Level Programming II: Arithme6c & Control

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

CHAPTER 3 BASIC EXECUTION ENVIRONMENT

x86 Programming II CSE 351 Winter

Advanced Microprocessors

6/17/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Machine-Level Programming Introduction

Chapter 2: The Microprocessor and its Architecture

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Transcription:

Assembly I: Basic Operations Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Basic Execution Environment RAX RBX RCX RDX RSI RDI RBP RSP R8 R9 R10 R11 R12 R13 R14 R15 General-purpose registers 63 31 EAX EBX ECX EDX ESI EDI EBP ESP R8D R9D R10D R11D R12D R13D R14D R15D 0 255 Segment registers CS DS SS ES FS GS 15 0 YMM Registers Sixteen 256-bit registers 127 79 15 Control Status Tag 63 MMX registers Eight 64-bit registers MM0 MM7 FPU registers Eight 80-bit data registers 0 R0 R7 Opcode FPU IPR FPU DPR 47 XMM Registers Sixteen 128-bit registers 10 0 0 0 0 RIP EIP YMM0 YMM15 XMM0 XMM15 RFLAGS EFLAGS MXCSR 31 0 2

general purpose IA-32 Registers Origin (mostly obsolete) %eax %ax %ah %al accumulate %ecx %cx %ch %cl counter %edx %dx %dh %dl data %ebx %bx %bh %bl base %esi %si source index %edi %di destination index %esp %sp stack pointer %ebp %bp base pointer 16-bit virtual registers (backwards compatibility) 3

Moving Data (1) Moving data: Src, Dest Move 8-byte ( quad ) word Operand types Immediate: constant integer data Like C constant, but prefixed with $ Encoded with 1, 2, or 4 bytes e.g. $0x400, $-533 Register: one of 16 integer registers But %rsp reserved for special use Others have special uses for particular instructions Memory: 8 consecutive bytes of memory Various addressing modes %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 4

Moving Data (2) operand combinations Cannot do memory-memory transfer with a single instruction Source Destination C Analog Imm Reg Mem $0x4, %rax $-147, (%rax) temp = 0x4; *p = -147; Reg Reg Mem %rax, %rdx %rax, (%rdx) temp2 = temp1; *p = temp; Mem Reg (%rax), %rdx temp = *p; 5

Simple Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address Pointer dereferencing in C e.g. (%rcx), %rax Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset e.g. 8(%rbp), %rdx 6

General Addressing Modes D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+D] D: constant displacement : 1, 2, or 4 bytes Rb: Base register: any of 16 integer registers Ri: Index register: any, except for %rsp 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]] Useful to access arrays and structures 7

Addressing Modes: Example Address computation %rdx %rcx 0xf000 0x0100 Expression Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400 0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080 8

Swap Example Source code in C: void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } Corresponding assembly code: swap: ret (%rdi), %rax (%rsi),%rdx %rdx, (%rdi) %rax, (%rsi) 9

Understanding Swap (1) void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi %rsi %rax %rdx Memory Register Allocation (By compiler) Register %rdi %rsi %rax %rdx Variable xp yp t0 t1 swap: ret (%rdi), %rax # t0 = *xp (%rsi), %rdx # t1 = *yp %rdx, (%rdi) # *xp = t1 %rax, (%rsi) # *yp = t0 10

Understanding Swap (2) void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi %rsi %rax %rdx 0x120 0x100 0x120 0x118 0x110 0x108 0x100 Memory 123 456 Register Allocation (By compiler) Register %rdi %rsi %rax %rdx Variable xp yp t0 t1 swap: ret (%rdi), %rax # t0 = *xp (%rsi), %rdx # t1 = *yp %rdx, (%rdi) # *xp = t1 %rax, (%rsi) # *yp = t0 11

Understanding Swap (3) void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi 0x120 %rsi 0x100 %rax 123 %rdx 0x120 0x118 0x110 0x108 0x100 Memory 123 456 Register Allocation (By compiler) Register %rdi %rsi %rax %rdx Variable xp yp t0 t1 swap: ret (%rdi), %rax # t0 = *xp (%rsi), %rdx # t1 = *yp %rdx, (%rdi) # *xp = t1 %rax, (%rsi) # *yp = t0 12

Understanding Swap (4) void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi 0x120 %rsi 0x100 %rax 123 %rdx 456 0x120 0x118 0x110 0x108 0x100 Memory 123 456 Register Allocation (By compiler) Register %rdi %rsi %rax %rdx Variable xp yp t0 t1 swap: ret (%rdi), %rax # t0 = *xp (%rsi), %rdx # t1 = *yp %rdx, (%rdi) # *xp = t1 %rax, (%rsi) # *yp = t0 13

Understanding Swap (5) void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi 0x120 %rsi 0x100 %rax 123 %rdx 456 0x120 0x118 0x110 0x108 0x100 Memory 456 456 Register Allocation (By compiler) Register %rdi %rsi %rax %rdx Variable xp yp t0 t1 swap: ret (%rdi), %rax # t0 = *xp (%rsi), %rdx # t1 = *yp %rdx, (%rdi) # *xp = t1 %rax, (%rsi) # *yp = t0 14

Understanding Swap (5) void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi 0x120 %rsi 0x100 %rax 123 %rdx 456 0x120 0x118 0x110 0x108 0x100 Memory 456 123 Register Allocation (By compiler) Register %rdi %rsi %rax %rdx Variable xp yp t0 t1 swap: ret (%rdi), %rax # t0 = *xp (%rsi), %rdx # t1 = *yp %rdx, (%rdi) # *xp = t1 %rax, (%rsi) # *yp = t0 15

Arithmetic/Logical Operations (1) Two operand instructions addq Src, Dest Dest = Dest + Src subq Src, Dest Dest = Dest Src mulq Src, Dest Dest = Dest * Src (unsigned) imulq Src, Dest Dest = Dest * Src (signed) salq Src, Dest Dest = Dest << Src (= shlq) sarq Src, Dest Dest = Dest >> Src (arithmetic) shrq Src, Dest Dest = Dest >> Src (logical) xorq Src, Dest Dest = Dest ^ Src andq Src, Dest Dest = Dest & Src orq Src, Dest Dest = Dest Src 16

Arithmetic/Logical Operations (2) One operand instructions incq Dest Dest = Dest + 1 decq Dest Dest = Dest 1 negq Dest Dest = Dest notq Dest Dest = ~ Dest See books for more instructions 17

Address Computation Instruction leal Src, Dest Src is address mode expression Set Dest to address denoted by expression Uses Computing address without a memory reference e.g. translation of p = &x[i]; Computing arithmetic expression of the form x + k*y k = 1, 2, 4, or 8 long m12 (long x) { return x * 12; } leaq salq (%rdi, %rdi, 2), %rax $2, %rax 18

Example: arith 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; } x in %rdi y in %rsi z in %rdx arith: leaq (%rdi, %rsi), %rax # %rax = x + y (t1) addq %rdx, %rax # %rax = z + t1 (t2) leaq (%rsi, %rsi, 2), %rdx # %rdx = 3 * y salq $4, %rdx # %rdx = 48 * y (t4) leaq 4(%rdi, %rdx), %rcx # %rcx = x + t4 + 4 (t5) imulq %rcx, %rax # %rax = t5 * t2 (rval) ret 19

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; } x in %edi y in %esi logical: movl %edi, %eax # %eax = x xorl %esi, %eax # %eax = x ^ y (t1) sarl $17, %eax # %eax = t1 >> 17 (t2) andl $8185, %eax # %eax = t2 & 8185 (rval) ret 20

CISC Properties Complex Instruction Set Computer (CISC) Instruction can reference different operand types Immediate, register, memory Arithmetic operations can read/write memory Source or destination can be a memory address Memory reference can involve complex computation Rb + S * Ri + D Useful for arithmetic expressions, too Instructions can have varying lengths x86-64 instructions can range from 1 to 15 bytes 21

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 22

Summary Machine Models Data Control mem C proc 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) byte 2) 2-byte word 3) 4-byte long word 4) 8-byte quad word 5) contiguous byte allocation 6) address of initial byte 1) branch/jump 2) call 3) ret 23