Assembly Language Alan L. Cox Alan L. CoxAssembly2 Why Learn Assembly Language? You’ll probably never write a program in assembly  Compilers.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Introduction to Machine/Assembler Language Noah Mendelsohn Tufts University Web:
1 Lecture 3: Instruction Set Architecture ISA types, register usage, memory addressing, endian and alignment, quantitative evaluation.
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
ELEN 468 Advanced Logic Design
COMP3221: Microprocessors and Embedded Systems Lecture 2: Instruction Set Architecture (ISA) Lecturer: Hui Wu Session.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Assembly Language Alan L. Cox Some slides adapted from CMU slides.
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010 Slides revised 2/2/2014 by Patrick Kelley.
6.828: PC hardware and x86 Frans Kaashoek
The Instruction Set Architecture Level Dept. of Computer Science Virginia Commonwealth University.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Programmer's view on Computer Architecture by Istvan Haller.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Computer architecture Lecture 11: Reduced Instruction Set Computers Piotr Bilski.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
ISA's, Compilers, and Assembly
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Lecture 3 Translation.
Instruction Set Architecture
Credits and Disclaimers
Machine-Level Programming I: Basics
Instruction Set Architectures
Assembly language.
Credits and Disclaimers
Lecture 6: Assembly Programs
CSCE 212 Computer Architecture
Computer Architecture Instruction Set Architecture
IA32 Processors Evolutionary Design
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
The Stack & Procedures CSE 351 Spring 2017
Basics Of X86 Architecture
Machine-Level Programming III: Procedures
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Instructions - Type and Format
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Lecture 4: MIPS Instruction Set
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Procedure Calls CSE 378 – Section 3.
The Stack & Procedures CSE 351 Winter 2018
Machine Level Representation of Programs (IV)
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Computer Instructions
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Introduction to Microprocessor Programming
Machine-Level Representation of Programs (x86-64)
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Lecture 4: Instruction Set Design/Pipelining
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
The von Neumann Machine
Credits and Disclaimers
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

Assembly Language Alan L. Cox

Alan L. CoxAssembly2 Why Learn Assembly Language? You’ll probably never write a program in assembly  Compilers are much better and more patient than you are But, understanding assembly is key to understanding the machine-level execution model  Behavior of programs in presence of bugs High-level language model breaks down  Tuning program performance Understanding sources of program inefficiency  Implementing system software Compiler has machine code as target Operating systems must manage process state

Alan L. CoxAssembly3 Instruction Set Architecture Contract between programmer and the hardware  Defines visible state of the system  Defines how state changes in response to instructions Assembly Programmer (compiler)  ISA is model of how a program will execute Hardware Designer  ISA is formal definition of the correct way to execute a program

Alan L. CoxAssembly4 Architecture vs. Implementation Instruction Set Architecture  Defines what a computer system does in response to a program and a set of data  Programmer visible elements of computer system Implementation  Defines how a computer does it  Sequence of steps to complete operations  Time to execute each operation  Hidden “bookkeeping” functions

Alan L. CoxAssembly5 Often Many Implementations of an ISA ISAImplementations x86-64 Intel Core i7 AMD Phenom II VIA Nano SPARC V.9 UltraSPARC III HyperSPARC

Alan L. CoxAssembly6 Why separate architecture and implementation? Compatibility  VAX architecture: mainframe  single chip  ARM: 20x performance range high vs. low performance, power, price Longevity  years of ISA  x86/x86-64 in 10th generation of implementations (architecture families)  Retain software investment  Amortize development costs over multiple markets

Alan L. CoxAssembly7 Instruction Set Basics Op ModeRaRb Mem Regs Before State Mem Regs After State instruction Instruction formats Instruction types Addressing modes Data types Operations Machine state Memory organization Register organization PC

Alan L. CoxAssembly8 Typical Machine State Memory General- Purpose Registers Special-Purpose Registers ALU, FPU, … CPU

Alan L. CoxAssembly9 x86-64 Machine State General-purpose registers  bit registers Extended x86: %rax, %rbx, %rcx, %rdx, %rsp, %rbp, %rdi, %rsi New: %r9 – %r15  %rsp is always the stack pointer  Backward compatibility for extended x86 registers %eax %rax 32-bit x86 register

Alan L. CoxAssembly10 x86-64 Machine State Special-purpose registers  bit floating point registers SSE: %xmm0 - %xmm15  Each register stores two double precision or four single precision values Vector arithmetic for speed!

Alan L. CoxAssembly11 x86-64 Machine State Memory  64-bit addresses However, only 48 bits used –47 bits of address space for programs –47 bits of address for operating system  Byte addressed  Little-endian byte ordering within a word Least significant byte comes first in memory

Alan L. CoxAssembly12 Assembly Language One assembly instruction  One group of machine language bits But, assembly language has additional features:  Distinguishes instructions & data  Labels = names for program control points  Pseudo-instructions = special directives to the assembler  Macros = user-definable abbreviations for code & constants

Alan L. CoxAssembly13 Instructions What do these instructions do?  Same kinds of things as high-level languages! Arithmetic & logic  Core computation Data transfer  Copying data between memory locations and/or registers Control transfer  Changing which instruction is next

Alan L. CoxAssembly14 Machine Language Encodings Variable-width:  1 to dozens of bytes  Flexible ISA  CISC  Harder hardware implementation  x86/x86-64, VAX, … Fixed-width:  1 word, typically  Limited ISA  RISC  Easier hardware implementation  SPARC, MIPS, PowerPC, … Look at representative examples

Alan L. CoxAssembly15 Machine Language Instruction Formats Different instructions have different kinds of operands Can’t encode all fields of all instructions within one word  Use different formats for different instructions  Different architectures use different formats  Different architectures group different sets of instructions SPARC:  Format 1: 1 30-bit immediate (call with immediate address)  Format 2: 1 reg & 1 condition & 1 22-bit immediate (branches)  Format 3: 3 regs, or 2 regs & 1 13-bit immediate (arithmetic, loads)

Alan L. CoxAssembly16 Machine Language: Arithmetic 32 registers  5 bits Not all bits important Only used if i=1 add %o2,%o3,%o1 0x B A variant of Format 3 opmath10 rd %o op3 add rs1 %o iimmediate?0 -N/A rs2 %o

Alan L. CoxAssembly17 Machine Language: Memory Ops ld [%o0+4], %o2 0xD A variant of Format 3 opmemory11 rd %o op3 ld rs1 %o iimmediate?1 simm13(13 bits)

Alan L. CoxAssembly18 Machine Language: Calls call there 0x Format 1 opcall01 disp30(30 bits) call there branch delay … there:… Assume assembler/linker assigns there to address 0x call also saves the next program counter in %o7, which is used by ret/retl to return from a procedure ( ret uses %i7, retl uses %o7) Instruction after branch/jump is always executed!

Alan L. CoxAssembly19 Machine Language: Branches # words relative to the branch instruction be dest 0x Format 2 opbranch00 aannul?0 cond e 0001 op2 b 010 disp22(22 bits) branch to dest branch delay instruction dest: destination instruction 3 instructions

Alan L. CoxAssembly20 Application Binary Interface (ABI) Standardizes the use of memory and registers by C compilers  Enables interoperability of code compiled by different C compilers E.g., a program compiled with Intel’s optimizing C compiler can call a library function that was compiled by the GNU C compiler  Sets the size of built-in data types E.g., int, long, etc.  Dictates the implementation of function calls E.g., how parameters and return values are passed

Alan L. CoxAssembly21 Register Usage The x86-64 ABI specifies that registers are used as follows  Temporary (callee can change these) %rax, %r10, %r11  Parameters to function calls %rdi, %rsi, %rdx, %rcx, %r8, %r9  Callee saves (callee can only change these after saving their current value) %rbx, %rbp, %r12-%r15 –%rbp is typically used as the “frame” pointer to the current function’s local variables  Return values %rax, %rdx

Alan L. CoxAssembly22 Procedure Calls and the Stack Where are local variables stored?  Registers (only 16)  Stack Stack provides as much local storage as necessary  Until memory exhausted  Each procedure allocates its own space on the stack Referencing the stack  %rsp points to the bottom of the stack in x86-64 Shared Libraries Heap Read/Write Data Read-only Code and Data Unused 0x7FFFFFFFFFFF 0x % rsp Unused Stack 0x x39A

Alan L. CoxAssembly23 Control Flow: Function Calls What must assembly/machine language do? CallerCallee 1.Save function arguments 2.Branch to function body 3.Execute body May allocate memory May call functions 4.Save function result 5.Branch to where called 1. Use registers %o0–%o5 (become %i0–%i5 ), then stack2. Use call (jump to procedure, save return location in %o7 )3. Use save to create new stack frame4. Use %i0 (becomes %o0 )5. Use ret (jump to return location using %i7 ) then restore

Alan L. CoxAssembly24 Program Stack SP - %o6 old SP (FP) - %i6 max Arguments to function(s) called by callee Possible saved register window Possible return address and local variables Callee’s frame Arguments to callee Possible saved register window Possible return address and local variables Caller’s frame … Misusing space allocated for local variable can trash return address! Register windows are saved/restored under hardware control

Alan L. CoxAssembly25 Example C Program #include void hello(char *name, int hour, int min) { printf("Hello, %s, it's %d:%02d.", name, hour, min); } int main(void) { hello(“Alan", 2, 55); return (0); } UNIX% gcc -S main.c main.c: Run the command: Output a file named main.s containing the assembly code for main.c

Alan L. CoxAssembly26 C Compiler’s Output.file "main.c".section.rodata.LC0:.string "Hello, %s, it's %d:%02d.".text.globl hello.type hello:.LFB2: pushq %rbp.LCFI0: movq %rsp, %rbp.LCFI1: subq $16, %rsp.LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax call printf leave ret.LFE2:.size hello,.-hello.section.rodata.LC1:.string "Alan".text.globl main.type main:.LFB3: pushq %rbp.LCFI3: movq %rsp, %rbp.LCFI4: movl $55, %edx movl $2, %esi movl $.LC1, %edi call hello movl $0, %eax

Alan L. CoxAssembly27 Instructions: Opcodes.file "main.c".section.rodata.LC0:.string "Hello, %s, it's %d:%02d.".text.globl hello.type hello:.LFB2: pushq %rbp.LCFI0: movq %rsp, %rbp.LCFI1: subq $16, %rsp.LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax call printf leave ret.LFE2:.size hello,.-hello Arithmetic, data transfer, & control transfer

Alan L. CoxAssembly28 Instructions: Operands.file "main.c".section.rodata.LC0:.string "Hello, %s, it's %d:%02d.".text.globl hello.type hello:.LFB2: pushq %rbp.LCFI0: movq %rsp, %rbp.LCFI1: subq $16, %rsp.LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax call printf leave ret.LFE2:.size hello,.-hello Registers, constants, & labels

Alan L. CoxAssembly29 What are Pseudo-Instructions? Assembler directives, with various purposes Data & instruction encoding:  Separate instructions & data into sections  Reserve memory with initial data values  Reserve memory w/o initial data values  Align instructions & data Provide information useful to linker or debugger  Correlate source code with assembly/machine  …

Alan L. CoxAssembly30 Instructions & Pseudo-Instructions.file "main.c".section.rodata.LC0:.string "Hello, %s, it's %d:%02d.".text.globl hello.type hello:.LFB2: pushq %rbp.LCFI0: movq %rsp, %rbp.LCFI1: subq $16, %rsp.LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax call printf leave ret.LFE2:.size hello,.-hello Instructions, Pseudo-Instructions, & Label Definitions

Alan L. CoxAssembly31 Label Types.file "main.c".section.rodata.LC0:.string "Hello, %s, it's %d:%02d.".text.globl hello.type hello:.LFB2: pushq %rbp.LCFI0: movq %rsp, %rbp.LCFI1: subq $16, %rsp.LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax call printf leave ret.LFE2:.size hello,.-hello Definitions, internal references, & external references

Alan L. CoxAssembly32 Assembly/Machine Language – Semantics Basic model of execution  Fetch instruction, from PC  Increment PC  Decode instruction  Fetch operands, from registers or memory  Execute operation  Store result(s), in registers or memory

Alan L. CoxAssembly33 Recall C Program #include void hello(char *name, int hour, int min) { printf("Hello, %s, it's %d:%02d.", name, hour, min); } int main(void) { hello(“Alan", 2, 55); return (0); }

Alan L. CoxAssembly34 Program Execution.file "main.c".section.rodata.LC0:.string "Hello, %s, it's %d:%02d.".text.globl hello.type hello:.LFB2: pushq %rbp.LCFI0: movq %rsp, %rbp.LCFI1: subq $16, %rsp.LCFI2: movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl %edx, -16(%rbp) movl -16(%rbp), %ecx movl -12(%rbp), %edx movq -8(%rbp), %rsi movl $.LC0, %edi movl $0, %eax call printf leave ret.LFE2:.size hello,.-hello.section.rodata.LC1:.string "Alan".text.globl main.type main:.LFB3: pushq %rbp.LCFI3: movq %rsp, %rbp.LCFI4: movl $55, %edx movl $2, %esi movl $.LC1, %edi call hello movl $0, %eax

Alan L. CoxAssembly35 Program Execution (cont.) movl $0, %eax leave ret.LFE3:.size main,.-main

Alan L. CoxAssembly36 More x86-64 Assembly x86-64 notes on course web page  Simple assembly language manual  Some example code  Along with lecture notes, should cover everything you need for the course Some code sequences generated by the compiler can still be confusing  Usually not important for this class (web is helpful if you are still curious)

Alan L. CoxAssembly37 Next Time Program Linking