Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 1

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

Princeton University Computer Science 217: Introduction to Programming Systems. Context of this Lecture. Assembly Language: Part 1

Princeton University. Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 1

Assembly Language: Overview

Princeton University COS 217: Introduction to Programming Systems A Subset of x86-64 Assembly Language

Assembly Language: Overview!

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 2

Second Part of the Course

Assembly Language: Part 2

Princeton University COS 217: Introduction to Programming Systems A Subset of IA-32 Assembly Language

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

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

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

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

Do not turn the page until 5:10.

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

x86 Programming I CSE 351 Winter

Assembly Language: IA-32 Instructions

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

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

Assembly II: Control Flow

CSE 351 Midterm Exam

Credits and Disclaimers

x64 Cheat Sheet Fall 2014

x86 Programming II CSE 351 Winter

Machine Language CS 3330 Samira Khan

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Do not turn the page until 11:30.

Process Layout and Function Calls

Machine-Level Programming II: Control

Machine-Level Programming II: Control

The Hardware/Software Interface CSE351 Spring 2013

Instruction Set Architecture (ISA) Data Types

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

Assembly Language II: Addressing Modes & Control Flow

The von Neumann Machine

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Towards the Hardware"

Instruction Set Architectures

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

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

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

CS Bootcamp x86-64 Autumn 2015

x86-64 Programming II

Sungkyunkwan University

The von Neumann Machine

Machine/Assembler Language Putting It All Together

RISC I from Berkeley. 44k Transistors 1Mhz 77mm^2

Introduction to Machine/Assembler Language

CS241 Computer Organization Spring 2015 IA

CS429: Computer Organization and Architecture

15-213/ Final Exam Notes Sheet Spring 2013!

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Assembly Programming III

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

Machine- Level Programming II: Arithme6c & Control

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

x86 64 Programming II

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

How Software Executes

CS61 Section Solutions 3

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

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

Intro to GNU Assembly Language on Intel Processors

Intel x86-64 and Y86-64 Instruction Set Architecture

CS367. Program Control

Credits to Randy Bryant & Dave O Hallaron

Machine-Level Programming II: Control and Arithmetic

Do not turn the page until 12:30.

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

Instruction Set Architectures

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Credits and Disclaimers

Lecture 3 CIS 341: COMPILERS

Review addressing modes

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

How Software Executes

Credits and Disclaimers

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

Process Layout, Function Calls, and the Heap

The x86 Architecture

Machine Level Programming II: Arithmetic &Control

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

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

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

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

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

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

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

CSCI 2021: x86-64 Control Flow

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

Machine Programming 2: Control flow

Machine-Level Programming II: Control Flow

Branching and Looping

University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse March 14, CSE 351 Final Exam. Last Name: First Name:

Transcription:

Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Part 1 1

Context of this Lecture First half of the semester: Programming in the large Second half: Under the hood Starting Now Afterward C Language Assembly Language language levels tour Application Program Operating System service levels tour Machine Language Hardware 2

Von Neumann Architecture Instructions are fetched from RAM (encoded as bits) Control unit interprets instructions to shuffle data between registers and RAM to move data from registers through ALU (arithmetic+logic unit) where operations are performed Control Unit Registers CPU ALU Data bus RAM 3

Agenda Language Levels Instruction-Set Architecture (ISA) Assembly Language: Performing Arithmetic Assembly Language: Control-flow instructions 4

High-Level Languages Characteristics Portable To varying degrees Complex One statement can do much work Structured while (...) {...} if ()... else... Human readable count = 0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } 5

Machine Languages Characteristics Not portable Specific to hardware Simple Each instruction does a simple task Unstructured Not human readable Requires lots of effort! Requires tool support 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 9222 9120 1121 A120 1121 A121 7211 0000 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F 0000 0000 0000 FE10 FACE CAFE ACED CEDE 1234 5678 9ABC DEF0 0000 0000 F00D 0000 0000 0000 EEEE 1111 EEEE 1111 0000 0000 B1B2 F1F5 0000 0000 0000 0000 0000 0000 6

Assembly Languages Characteristics Not portable Each assembly lang instruction maps to one machine lang instruction Simple Each instruction does a simple task Unstructured Human readable!!! (well, in the same sense that Hungarian is human readable, if you know Hungarian). loop: else: movl cmpl jle movl andl je movl jmp sarl $0, %r10d $1, %r11d endloop $1, %r10d %r11d, %eax $1, %eax else %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d endif $1, %r11d endif: jmp endloop: loop 7

Computer: CPU + RAM Control Unit CPU movl $0, %r10d loop: cmpl $1, %r11d jle endloop $1, %r10d ALU movl %r11d, %eax andl $1, %eax je else Registers Data bus movl %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d RAM else: endif: jmp sarl endif $1, %r11d jmp endloop: loop 8

Translation: C to x86-64 count = 0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } count r10d n r11d loop: else: endif: movl cmpl jle movl andl je movl jmp sarl jmp endloop: $0, %r10d $1, %r11d endloop $1, %r10d %r11d, %eax $1, %eax else %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d endif $1, %r11d loop 9

Why Learn Assembly Language? Q: Why learn assembly language? A: Knowing assembly language helps you: Write faster code In assembly language In a high-level language! Understand what s happening under the hood Someone needs to develop future computer systems Maybe that will be you! 10

Why Learn x86-64 Assembly Lang? Why learn x86-64 assembly language? Pros X86-64 is widely used CourseLab computers are x86-64 computers Program natively on CourseLab instead of using an emulator Cons X86-64 assembly language is big and ugly There are many instructions Instructions differ widely 11

Agenda Language Levels Architecture Assembly Language: Performing Arithmetic Assembly Language: Control-flow instructions 12

RAM RAM (Random Access Memory) Conceptually: large array of bytes Contains data (program variables, structs, arrays) and the program! Control Unit CPU ALU Registers Data bus RAM 13

John Von Neumann (1903-1957) In computing Stored program computers Cellular automata Self-replication Other interests Mathematics Inventor of game theory Nuclear physics (hydrogen bomb) Princeton connection Princeton Univ & IAS, 1930-1957 Known for Von Neumann architecture (1950) In which programs are just data in the memory Contrast to the now-obsolete Harvard architecture 14

Von Neumann Architecture RAM (Random Access Memory) Conceptually: large array of bytes Control Unit CPU ALU Instructions are fetched from RAM Registers Data bus RAM 15

Registers Registers Small amount of storage on the CPU Much faster than RAM Top of the storage hierarchy Above RAM, disk, Control Unit Registers CPU ALU Data bus RAM 16

Registers (x86-64 architecture) General purpose registers: 63 31 15 7 0 RAX EAX AX AL RBX EBX BX BL RCX ECX CX CL RDX EDX DX DL 17

Registers (x86-64 architecture) General purpose registers (cont.): 63 31 15 7 0 RSI ESI SI SIL RDI EDI DI DIL RBP EBP BP BPL RSP ESP SP SPL RSP is unique; see upcoming slide 18

Registers (x86-64 architecture) General purpose registers (cont.): 63 31 15 7 0 R8 R8D R8W R8B R9 R9D R9W R9B R10 R10D R10W R10B R11 R11D R11W R11B R12 R12D R12W R12B R13 R13D R13W R13B R14 R14D R14W R14B R15 R15D R15W R15B 19

Registers summary 16 general-purpose 64-bit pointer/long-integer registers, many with stupid names: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, r8, r9, r10, r11, r12, r13, r14, r15 sometimes used as a frame pointer or base pointer stack pointer If you re operating on 32-bit int data, use these stupid names instead: eax, ebx, ecx, edx, esi, edi, ebp, rsp, r8d, r9d, r10d, r11d, r12d, r13d, r14d, r15d it doesn t really make sense to put 32-bit ints in the stack pointer 20

STACK frame RSP Register RSP (Stack Pointer) register Contains address of top (low address) of current function s stack frame RSP low memory high memory Allows use of the STACK section of memory (See Assembly Language: Function Calls lecture) 21

EFLAGS Register Special-purpose register EFLAGS (Flags) register Contains CC (Condition Code) bits Affected by compare (cmp) instruction And many others Used by conditional jump instructions je, jne, jl, jg, jle, jge, jb, jbe, ja, jae, jb (See Assembly Language: Part 2 lecture) 22

TEXT section RIP Register Special-purpose register RIP (Instruction Pointer) register Stores the location of the next instruction Address (in TEXT section) of machine-language instructions to be executed next Value changed: Automatically to implement sequential control flow By jump instructions to implement selection, repetition RIP 23

Registers summary 16 general-purpose 64-bit pointer/long-integer registers, many with stupid names: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, r8, r9, r10, r11, r12, r13, r14, r15 sometimes used as a frame pointer or base pointer stack pointer If you re operating on 32-bit int data, use these stupid names instead: eax, ebx, ecx, edx, esi, edi, ebp, rsp, r8d, r9d, r10d, r11d, r12d, r13d, r14d, r15d it doesn t really make sense to put 32-bit ints in the stack pointer 2 special-purpose registers: eflags rip condition codes program counter 24

Registers and RAM Typical pattern: Load data from RAM to registers Manipulate data in registers Store data from registers to RAM Many instructions combine steps 25

ALU ALU (Arithmetic Logic Unit) Performs arithmetic and logic operations Control Unit CPU src1 src2 ALU operationalu ALU EFLAGS Registers dest flag/carry Data bus RAM 26

Control Unit Control Unit Fetches and decodes each machine-language instruction Sends proper data to ALU Control Unit CPU ALU Registers Data bus RAM 27

CPU CPU (Central Processing Unit) Control unit Fetch, decode, and execute ALU Execute low-level operations Registers High-speed temporary storage Control Unit Registers CPU ALU Data bus RAM 28

Agenda Language Levels Architecture Assembly Language: Performing Arithmetic Assembly Language: Control-flow instructions 29

Instruction Format Many instructions have this format: name{b,w,l,q} src, dest name: name of the instruction (mov, add, sub, and, etc.) byte operands are one-byte entities word operands are two-byte entities long operands are four-byte entities quad operands are eight-byte entitles 30

Instruction Format Many instructions have this format: name{b,w,l,q} src, dest src: source operand The source of data Can be Register operand: %rax, %ebx, etc. Memory operand: 5 (legal but silly), somelabel Immediate operand: $5, $somelabel 31

Instruction Format Many instructions have this format: name{b,w,l,q} src, dest dest: destination operand The destination of data Can be Register operand: %rax, %ebx, etc. Memory operand: 5 (legal but silly), somelabel Cannot be Immediate operand 32

Performing Arithmetic: Long Data static int length; static int width; static int perim; perim = (length + width) * 2; Note: movl instruction instruction sall instruction Register operand Immediate operand Memory operand.section instruction (to announce TEXT section) Registers.section ".bss" length:.skip 4 width:.skip 4 perim:.skip 4.section ".text" movl length, %eax width, %eax sall $1, %eax movl %eax, perim Memory length 5 EAX 14 75 width 2 R10 perim 14 33

Performing Arithmetic: Byte Data static char grade = 'B'; grade--; EAX Note: Comment movb instruction subb instruction decb instruction Registers BA grade What would happen if we use movl instead of movb? Memory AB A D 0.section ".data" grade:.byte 'B'.byte 'A'.byte 'D'.byte 0.section ".text" # Option 1 movb grade, %al subb $1, %al movb %al, grade # Option 2 subb $1, grade # Option 3 decb grade 34

Operands Immediate operands $5 use the number 5 (i.e. the number that is available immediately within the instruction) $i use the address denoted by i (i.e. the address that is available immediately within the instruction) Can be source operand; cannot be destination operand Register operands %rax read from (or write to) register RAX Can be source or destination operand Memory operands 5 load from (or store to) memory at address 5 (silly; seg fault*) i load from (or store to) memory at the address denoted by i Can be source or destination operand (but not both) There s more to memory operands; see next lecture *if you re lucky 35

Notation Instruction notation: q quad (8 bytes); l long (4 bytes); w word (2 bytes); b byte (1 byte) Operand notation: src source; dest destination R register; I immediate; M memory 36

Generalization: Data Transfer Data transfer instructions mov{q,l,w,b} srcirm, destrm movsb{q,l,w} srcrm, destr movsw{q,l} srcrm, destr movslq srcrm, destr movzb{q,l,w} srcrm, destr movzw{q,l} srcrm, destr movzlq srcrm, destr dest = src dest = src (sign extend) dest = src (sign extend) dest = src (sign extend) dest = src (zero fill) dest = src (zero fill) dest = src (zero fill) cqto cltd cwtl cbtw reg[rdx:rax] = reg[rax] (sign extend) reg[edx:eax] = reg[eax] (sign extend) reg[eax] = reg[ax] (sign extend) reg[ax] = reg[al] (sign extend) mov is used often; others less so 37

Generalization: Arithmetic Arithmetic instructions add{q,l,w,b} srcirm, destrm sub{q,l,w,b} srcirm, destrm inc{q,l,w,b} destrm dec{q,l,w,b} destrm neg{q,l,w,b} destrm dest += src dest -= src dest++ dest-- dest = -dest Q: Is this adding signed numbers or unsigned? A: Yes! [remember properties of 2 s complement] signed 2 s complement 3 0011 B + -4 + 1100 B -- ---- -1 1111 B unsigned 3 0011 B + 12 + 1100 B -- ---- 15 1111 B 38

Generalization: Bit Manipulation Bitwise instructions and{q,l,w,b} srcirm, destrm or{q,l,w,b} srcirm, destrm xor{q,l,w,b} srcirm, destrm not{q,l,w,b} destrm sal{q,l,w,b} srcir, destrm sar{q,l,w,b} srcir, destrm shl{q,l,w,b} srcir, destrm shr{q,l,w,b} srcir, destrm dest = src & dest dest = src dest dest = src ^ dest dest = ~dest dest = dest << src dest = dest >> src (sign extend) (Same as sal) dest = dest >> src (zero fill) signed (arithmetic right shift) 44 / 2 2 000101100 B = 11 000001011 B unsigned (logical right shift) 44 / 2 2 000101100 B = 11 000001011 B -44 / 2 2 111010100 B = -11 111110101 B copies of sign bit 468 / 2 2 111010100 B = 117 001110101 B zeros 39

Multiplication & Division Signed Unsigned imulq srcrm reg[rdx:rax] = reg[rax]*src mulq srcrm reg[rdx:rax] = reg[rax]*src imull srcrm reg[edx:eax] = reg[eax]*src mull srcrm reg[edx:eax] = reg[eax]*src imulw srcrm reg[dx:ax] = reg[ax]*src mulw srcrm reg[dx:ax] = reg[ax]*src imulb srcrm reg[ax] = reg[al]*src mulb srcrm reg[ax] = reg[al]*src idivq srcrm reg[rax] = reg[rdx:rax]/src divq srcrm reg[rax] = reg[rdx:rax]/src reg[rdx] = reg[rdx:rax]%src reg[rdx] = reg[rdx:rax]%src idivl srcrm reg[eax] = reg[edx:eax]/src reg[edx] = reg[edx:eax]%src divl srcrm reg[eax] = reg[edx:eax]/src reg[edx] = reg[edx:eax]%src idivw srcrm reg[ax] = reg[dx:ax]/src reg[dx] = reg[dx:ax]%src divw srcrm reg[ax] = reg[dx:ax]/src reg[dx] = reg[dx:ax]%src idivb srcrm reg[al] = reg[ax]/src reg[ah] = reg[ax]%src divb srcrm reg[al] = reg[ax]/src reg[ah] = reg[ax]%src See Bryant & O Hallaron book for description of signed vs. unsigned multiplication and division 40

Translation: C to x86-64 count r10d n r11d count = 0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } loop: movl cmpl jle movl andl je movl $0, %r10d $1, %r11d endloop $1, %r10d %r11d, %eax $1, %eax else %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d Control Unit CPU ALU else: endif: jmp loop Registers endloop: 41 jmp sarl endif $1, %r11d

Agenda Language Levels Architecture Assembly Language: Performing Arithmetic Assembly Language: Control-flow instructions 42

Control Flow with Signed Integers Comparing (signed or unsigned) integers cmp{q,l,w,b} srcirm, destrm Compare dest with src Sets condition-code bits in the EFLAGS register Beware: operands are in counterintuitive order Beware: many other instructions set condition-code bits Conditional jump should immediately follow cmp 43

Control Flow with Signed Integers Unconditional jump jmp X Jump to address X Conditional jumps after comparing signed integers je X jne X jl X jle X jg X jge X Jump to X if equal Jump to X if not equal Jump to X if less Jump to X if less or equal Jump to X if greater Jump to X if greater or equal Examine condition-code bits in EFLAGS register 44

Assembly lang. Machine lang. movl loop: cmpl jle movl andl je movl jmp else: sarl endif: jmp endloop: $0, %r10d $1, %r11d endloop $1, %r10d %r11d, %eax $1, %eax else %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d endif $1, %r11d loop address: contents (in hex) 1000: 41ba00000000 1006: 4183fb01 100a: 7e25 25 = 2f 0a (hex) 100c: 4183c201 1010: 4489d8 1013: 8324250000000001 101b: 740f 101d: 4489d8 1020: 4101c3 1023: 4101c3 1026: 4183c301 102a: eb03 102c: 41d1fb 102f: 83c301 1031: 45

Label stands for an address movl loop: cmpl jle movl andl je movl jmp else: sarl endif: jmp endloop: $0, %r10d $1, %r11d endloop $1, %r10d %r11d, %eax $1, %eax else %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d endif $1, %r11d loop address: contents (in hex) 1000: 41ba00000000 1006: 4183fb01 100a: 7e25 25 = 31 0c (hex) 100c: 4183c201 1010: 4489d8 1013: 8324250000000001 101b: 740f 101d: 4489d8 1020: 4101c3 1023: 4101c3 1026: 4183c301 102a: eb03 102c: 41d1fb 102f: 83c301 1031: 46

Translation: C to x86-64 movl $0, %r10d count = 0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } loop: cmpl jle movl andl je movl $1, %r11d endloop $1, %r10d %r11d, %eax $1, %eax else %r11d, %eax %eax, %r11d %eax, %r11d $1, %r11d jmp else: sarl endif: jmp endloop: endif $1, %r11d loop 47

Summary Language levels The basics of computer architecture Enough to understand x86-64 assembly language The basics of x86-64 assembly language Registers Arithmetic Control flow To learn more Study more assembly language examples Chapter 3 of Bryant and O Hallaron book Study compiler-generated assembly language code gcc217 S somefile.c 48