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

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

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. Assembly Language: Part 1

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

Assembly Language: Overview

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

Assembly Language: Overview!

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

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

Assembly Language: IA-32 Instructions

Assembly Language: Part 2

CSE351 Spring 2018, Midterm Exam April 27, 2018

Second Part of the Course

Process Layout and Function Calls

Credits and Disclaimers

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

Do not turn the page until 5:10.

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

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

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

x86 Programming I CSE 351 Winter

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

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

x64 Cheat Sheet Fall 2014

Assembly II: Control Flow

15-213/ Final Exam Notes Sheet Spring 2013!

Assembly Language II: Addressing Modes & Control Flow

Machine/Assembler Language Putting It All Together

CSE 351 Midterm Exam

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Do not turn the page until 11:30.

CS Bootcamp x86-64 Autumn 2015

CS241 Computer Organization Spring 2015 IA

The von Neumann Machine

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

The Hardware/Software Interface CSE351 Spring 2013

CSE 351 Midterm Exam Spring 2016 May 2, 2015

x86 Programming II CSE 351 Winter

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Machine-Level Programming II: Control

Machine-Level Programming II: Control

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

The von Neumann Machine

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

Towards the Hardware"

Machine Language CS 3330 Samira Khan

Credits and Disclaimers

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

CS61 Section Solutions 3

Instruction Set Architecture (ISA) Data Types

Intro to GNU Assembly Language on Intel Processors

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

Process Layout, Function Calls, and the Heap

Machine- Level Programming II: Arithme6c & Control

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

Sungkyunkwan University

How Software Executes

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

x86-64 Programming II

Introduction to Machine/Assembler Language

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

How Software Executes

Instruction Set Architectures

Do not turn the page until 12:30.

Intel x86-64 and Y86-64 Instruction Set Architecture

Lecture 3 CIS 341: COMPILERS

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

Instruction Set Architectures

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

CS429: Computer Organization and Architecture

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

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

Assembly Programming III

x86 64 Programming II

Credits to Randy Bryant & Dave O Hallaron

Machine-Level Programming II: Control and Arithmetic

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

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

Credits and Disclaimers

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

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

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

CS367. Program Control

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

The x86 Architecture

Machine Programming 2: Control flow

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

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

Credits and Disclaimers

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

Function Calls and Stack

Machine Level Programming II: Arithmetic &Control

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

How Software Executes

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

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

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

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

Transcription:

Princeton University Computer Science 217: Introduction to Programming Systems Context of this Lecture First half lectures: Programming in the large Second half lectures: Under the hood Assembly Language: Part 1 Starting Now C Language Assembly Language language levels tour Afterward Application Program Operating System service levels tour Machine Language Hardware 1 2 Goals of this Lecture Lectures vs. Precepts Help you learn: Language levels The basics of x86-64 architecture Enough to understand x86-64 assembly language The basics of x86-64 assembly language Instructions to define global data Instructions to transfer data and perform arithmetic Approach to studying assembly language: Precepts Study complete pgms Begin with small pgms; proceed to large ones Emphasis on writing code Lectures Study partial pgms Begin with simple constructs; proceed to complex ones Emphasis on reading code 3 4 Agenda High-Level Languages Language Levels Architecture Assembly Language: Defining Global Data Assembly Language: Performing Arithmetic Characteristics Portable To varying degrees Complex One statement can do much work Expressive To varying degrees Good (code functionality / code size) ratio Human readable count = 0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } 5 6

Machine Languages Assembly Languages Characteristics Not portable Specific to hardware Simple Each instruction does a simple task Not expressive Each instruction performs little work Poor (code functionality / code size) ratio 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 7 Characteristics Not portable Each assembly lang instruction maps to one machine lang instruction Simple Each instruction does a simple task Not expressive Poor (code functionality / code size) ratio Human readable!!! movl loop: cmpl jle addl movl andl je movl addl addl addl 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 8 Why Learn Assembly Language? Why Learn x86-64 Assembly Lang? 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! Why learn x86-64 assembly language? Pros X86-64 is popular CourseLab computers are x86-64 computers Program natively on CourseLab instead of using an emulator Cons X86-64 assembly language is big Each instruction is simple, but There are many instructions Instructions differ widely 9 10 x86-64 Assembly Lang Subset We ll study a popular subset As defined by precept x86-64 Assembly Language document We ll study programs define functions that: Do not use floating point values Have parameters that are integers or addresses (but not structures) Have return values that are integers or addresses (but not structures) Have no more than 6 parameters Agenda Language Levels Architecture Assembly Language: Defining Global Data Assembly Language: Performing Arithmetic Claim: a reasonable subset 11 12

John Von Neumann (1903-1957) Von Neumann Architecture 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 13 14 Von Neumann Architecture Von Neumann Architecture (Random Access Memory) Conceptually: large array of bytes Contains data (program variables, structs, arrays) and the program! Instructions are fetched from So is data Small amount of storage on the Much faster than Top of the storage hierarchy Above, disk, 15 16 (x86-64 architecture) (x86-64 architecture) General purpose registers: General purpose registers (cont.): 63 31 15 7 0 63 31 15 7 0 RAX EAX AX AL RSI ESI SI SIL RBX EBX BX BL RDI EDI DI DIL RCX ECX CX CL RBP EBP BP BPL RDX EDX DX DL RSP ESP SP SPL 17 RSP is unique; see upcoming slide 18

(x86-64 architecture) RSP Register General purpose registers (cont.): 63 31 15 7 0 R8 R8D R8W R8B R9 R9D R9W R9B R10 R10D R10W R10B RSP (Stack Pointer) register Contains address of top (low address) of current function s stack frame RSP low memory STACK frame R11 R11D R11W R11B R12 R12D R12W R12B R13 R13D R13W R13B R14 R14D R14W R14B R15 R15D R15W R15B high memory Allows use of the STACK section of memory, and special-purpose stack manipulation instructions (See Assembly Language: Function Calls lecture) 19 20 EFLAGS Register RIP Register Special-purpose 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,... (See Assembly Language: Part 2 lecture) 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 21 22 TEXT section summary and 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 stack pointer or base 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 Typical pattern: Load data from to registers Manipulate data in registers Store data from registers to Many instructions combine steps 2 special-purpose registers: eflags rip condition codes program counter 23 24

(Arithmetic Logic ) Performs arithmetic and logic operations src1 src2 Fetches and decodes each machine-language instruction Sends proper data to operation EFLAGS dest flag/carry 25 26 Agenda (Central Processing ) unit Fetch, decode, and execute Execute low-level operations High-speed temporary storage Language Levels Architecture Assembly Language: Defining Global Data Assembly Language: Performing Arithmetic 27 28 Defining Data: DATA Section 1 Defining Data: DATA Section 2 static char c = 'a'; static short s = 12; static int i = 345; static long l = 6789; c:.byte 'a' s:.word 12 i:.long 345 l:.quad 6789.section instruction (to announce DATA section) label definition (marks a spot in ).byte instruction (1 byte).word instruction (2 bytes).long instruction (4 bytes).quad instruction (8 bytes) char c = 'a'; short s = 12; int i = 345; long l = 6789;.globl c c:.byte 'a'.globl s s:.word 12.globl i i:.long 345.globl l l:.quad 6789 Can place label on same line as next instruction.globl instruction 29 30

Defining Data: BSS Section static char c; static short s; static int i; static long l; c: s: i: l:.section ".bss".skip 1.skip 2.skip 4.skip 8.section instruction (to announce BSS section).skip instruction 31 Defining Data: RODATA Section "hello\n";.section ".rodata" hellolabel:.string "hello\n".section instruction (to announce RODATA section).string instruction 32 Agenda Language Levels Architecture Assembly Language: Defining Global Data Assembly Language: Performing Arithmetic 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 33 34 Instruction Format Many instructions have this format: Instruction Format Many instructions have this format: name{b,w,l,q} src, dest 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 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 35 36

Performing Arithmetic: Long Data Operands static int length; static int width; static int perim; perim = (length + width) * 2;.section ".bss" length:.skip 4 width:.skip 4 perim:.skip 4 movl length, %eax addl width, %eax sall $1, %eax movl %eax, perim movl instruction addl instruction sall instruction Register operand Immediate operand Memory operand.section instruction (to announce TEXT section) 37 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 38 Performing Arithmetic: Byte Data static char grade = 'B'; grade--; Comment movb instruction subb instruction decb instruction grade:.byte 'B' movb grade, %al subb $1, %al movb %al, grade subb $1, grade # Option 3 decb grade iclicker Question Q: What would happen if we used movl instead of movb? A. Would always work correctly B. Would always work incorrectly C. Would sometimes work correctly D. This code would work, but something else might go wrong that would cause you sleepless nights of painful debugging grade:.byte 'B' movb grade, %al subb $1, %al movb %al, grade subb $1, grade # Option 3 decb grade 39 iclicker Question Q: What would happen if we used subl instead of subb? A. Would always work correctly B. Would always work incorrectly C. Would sometimes work correctly D. This code would work, but something else might go wrong that would cause you sleepless nights of painful debugging grade:.byte 'B' movb grade, %al subb $1, %al movb %al, grade subb $1, grade # Option 3 decb grade More 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 Operand notation: src source; dest destination R register; I immediate; M memory 42

Data Transfer Instructions Multiplication and Division Signed multiplication and division instructions mov{q,l,w,b} srcirm, destrm dest = src movsb{q,l,w} srcrm, destr dest = src (sign extend) movsw{q,l} srcrm, destr dest = src (sign extend) movslq srcrm, destr dest = src (sign extend) movzb{q,l,w} srcrm, destr dest = src (zero fill) movzw{q,l} srcrm, destr dest = src (zero fill) movzlq srcrm, destr dest = src (zero fill) cqto reg[rdx:rax] = reg[rax] (sign extend) cltd reg[edx:eax] = reg[eax] (sign extend) cwtl reg[eax] = reg[ax] (sign extend) cbtw reg[ax] = reg[al] (sign extend) imulq srcrm imull srcrm imulw srcrm imulb srcrm idivq srcrm idivl srcrm idivw srcrm idivb srcrm reg[rdx:rax] = reg[rax]*src reg[edx:eax] = reg[eax]*src reg[dx:ax] = reg[ax]*src reg[ax] = reg[al]*src reg[rax] = reg[rdx:rax]/src reg[rdx] = reg[rdx:rax]%src reg[eax] = reg[edx:eax]/src reg[edx] = reg[edx:eax]%src reg[ax] = reg[dx:ax]/src reg[dx] = reg[dx:ax]%src reg[al] = reg[ax]/src reg[ah] = reg[ax]%src See Bryant & O Hallaron book for description of signed vs. unsigned multiplication and division 43 44 Multiplication and Division Unsigned multiplication and division instructions mulq srcrm mull srcrm mulw srcrm mulb srcrm divq srcrm divl srcrm divw srcrm divb srcrm reg[rdx:rax] = reg[rax]*src reg[edx:eax] = reg[eax]*src reg[dx:ax] = reg[ax]*src reg[ax] = reg[al]*src reg[rax] = reg[rdx:rax]/src reg[rdx] = reg[rdx:rax]%src reg[eax] = reg[edx:eax]/src reg[edx] = reg[edx:eax]%src reg[ax] = reg[dx:ax]/src reg[dx] = reg[dx:ax]%src reg[al] = reg[ax]/src reg[ah] = reg[ax]%src See Bryant & O Hallaron book for description of signed vs. unsigned multiplication and division 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) 45 46 Summary Appendix Language levels The basics of computer architecture Enough to understand x86-64 assembly language The basics of x86-64 assembly language Instructions to define global data Instructions to perform data transfer and arithmetic Big-endian vs little-endian byte order 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 47 48

Byte Order x86-64 is a little endian architecture Least significant byte of multi-byte entity is stored at lowest memory address Little end goes first The int 5 at address 1000: Some other systems use big endian Most significant byte of multi-byte entity is stored at lowest memory address Big end goes first The int 5 at address 1000: 1000 1001 1002 1003 1000 1001 1002 1003 00000101 00000101 Byte Order Example 1 #include <stdio.h> int main(void) { unsigned int i = 0x003377ff; unsigned char *p; int j; p = (unsigned char *)&i; for (j=0; j<4; j++) printf("byte %d: %2x\n", j, p[j]); } Output on a little-endian machine Byte 0: ff Byte 1: 77 Byte 2: 33 Byte 3: 00 Output on a big-endian machine Byte 0: 00 Byte 1: 33 Byte 2: 77 Byte 3: ff 49 50 Byte Order Example 2 Byte Order Example 3 Flawed code; uses b instructions to manipulate a four-byte memory area x86-64 is little endian, so what will be the value of grade? grade:.long 'B' movb grade, %al subb $1, %al movb %al, grade subb $1, grade Flawed code; uses l instructions to manipulate a one-byte memory area What would happen? grade:.byte 'B' movl grade, %eax subl $1, %eax movl %eax, grade subl $1, grade What would be the value of grade if x86-64 were big endian? 51 52