Assembly Language Alan L. Cox Some slides adapted from CMU 15.213 slides.

Slides:



Advertisements
Similar presentations
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Machine Programming I: Basics
Machine-Level Programming I: Basics
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-243: Introduction to Computer Systems 4 th Lecture, Sep. 2, 2010 Instructors: Randy Bryant.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Tue. May 27, 2015 Instructors: Nathaniel.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Machine-Level Programming I: Introduction Sept. 10, 2007 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Sep. 11, 2012 Instructors: Dave O’Hallaron,
1 Machine-Level Programming I: Basics Computer Systems Organization Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Jan 28, 2014 Instructors: Seth Copen.
Lee CSCE 312 TAMU 1 Machine-Level Programming I: Basics Instructor: Dr. Hyunyoung Lee Based on slides provided by: Randy Bryant and Dave O’Hallaron.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics Sep. 15, 2015.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2015 Instructor: John Barr * Modified slides.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Carnegie Mellon 1 Machine-Level Programming I: Basics Slides based on from those of Bryant and O’Hallaron.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 x86 Programming Model Microprocessor Computer Architectures Lab Components of any Computer System Control – logic that controls fetching/execution of.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Assembly Language Alan L. Cox Alan L. CoxAssembly2 Why Learn Assembly Language? You’ll probably never write a program in assembly  Compilers.
1 Binghamton University Machine-Level Programming I: Basics CS220: Computer Systems II.
A job ad at a game programming company
Reading Condition Codes (Cont.)
Instruction Set Architecture
Machine-Level Programming I: Basics
Homework Reading Lab with your assigned section starts next week
Credits and Disclaimers
IA32 Processors Evolutionary Design
Homework Reading Continue work on mp1
Machine-Level Programming 1 Introduction
Machine-Level Programming 4 Procedures
BIC 10503: COMPUTER ARCHITECTURE
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Machine-Level Programming III: Procedures Sept 18, 2001
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
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 I: Basics
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
“Way easier than when we were students”
The von Neumann Machine
Credits and Disclaimers
Presentation transcript:

Assembly Language Alan L. Cox Some slides adapted from CMU slides

Cox / RixnerAssembly2 Objectives Be able to read simple x86-64 assembly language programs

Cox / RixnerAssembly3 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

Cox / RixnerAssembly4 Assembly Language One assembly instruction  Straightforward translation to a group of machine language bits that describe one instruction 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

Cox / RixnerAssembly5 Assembly Language (cont.) 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

Cox / RixnerAssembly6 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% clang -S main.c main.c: Run the command: Output a file named main.s containing the assembly code for main.c

Cox / RixnerAssembly7 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

Cox / RixnerAssembly8 A Breakdown of the 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 Instructions, Pseudo-Instructions, & Label Definitions

Cox / RixnerAssembly9 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

Cox / RixnerAssembly10 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

Cox / RixnerAssembly11 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

Cox / RixnerAssembly12 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

Cox / RixnerAssembly13 Often Many Implementations of an ISA ISAImplementations x86-64 Intel Core i7 AMD FX-83XX VIA Nano ARMv7-A Apple A6/A6X Qualcomm Krait

Cox / RixnerAssembly14 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

Cox / RixnerAssembly15 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

Cox / RixnerAssembly16 Typical Machine State Memory General- Purpose Registers Special-Purpose Registers ALU, FPU, … CPU

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

Moving Data: IA32 Moving Data movl Source, Dest: Operand Types  Immediate: Constant integer data Example: $0x400, $-533 Like C constant, but prefixed with ‘$’ Encoded with 1, 2, or 4 bytes  Register: One of 8 integer registers Example: %eax, %edx But %esp and %ebp reserved for special use Others have special uses for particular instructions  Memory: 4 consecutive bytes of memory at address given by register Simplest example: (%eax) Various other “address modes” %eax %ecx %edx %ebx %esi %edi %esp %ebp Cox / RixnerAssembly18

movl Operand Combinations Cannot do memory-memory transfer with a single instruction movl Imm Reg Mem Reg Mem Reg Mem Reg SourceDestC Analog movl $0x4,%eaxtemp = 0x4; movl $-147,(%eax)*p = -147; movl %eax,%edxtemp2 = temp1; movl %eax,(%edx)*p = temp; movl (%eax),%edxtemp = *p; Src,Dest Cox / RixnerAssembly19

Simple Memory Addressing Modes Normal(R)Mem[Reg[R]]  Register R specifies memory address movl (%ecx),%eax Displacement D(R)Mem[Reg[R]+D]  Register R specifies start of memory region  Constant displacement D specifies offset movl 8(%ebp),%edx Cox / RixnerAssembly20

Using Simple Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Set Up Finish swap: pushl %ebp movl %esp, %ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx) popl %ebx popl %ebp ret Cox / RixnerAssembly21

Using Simple Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp, %ebp pushl %ebx movl8(%ebp), %edx movl12(%ebp), %ecx movl(%edx), %ebx movl(%ecx), %eax movl%eax, (%edx) movl%ebx, (%ecx) popl%ebx popl%ebp ret Body Set Up Finish Cox / RixnerAssembly22

Understanding Swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Stack (in memory) RegisterValue %edxxp %ecxyp %ebxt0 %eaxt1 yp xp Rtn adr Old % ebp %ebp Offset Old % ebx -4 %esp movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly23

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp0x104 movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly24

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x124 0x104 0x120 movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly25

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x120 0x104 0x124 movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly26

456 Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x124 0x x104 movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly27

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 0x movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly28

456 Understanding Swap 0x120 0x124 Rtn adr %ebp Offset -4 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x x movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly29

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 0x movl8(%ebp), %edx# edx = xp movl12(%ebp), %ecx# ecx = yp movl(%edx), %ebx# ebx = *xp (t0) movl(%ecx), %eax# eax = *yp (t1) movl%eax, (%edx)# *xp = t1 movl%ebx, (%ecx)# *yp = t0 Cox / RixnerAssembly30

Complete Memory Addressing Modes 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 Unlikely you’d use %ebp, either  S: Scale: 1, 2, 4, or 8 (why these numbers?) 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]] Cox / RixnerAssembly31

%rsp x86-64 Integer Registers  Extend existing registers. Add 8 new ones.  Make %ebp / %rbp general purpose %eax %ebx %ecx %edx %esi %edi %esp %ebp %r8d %r9d %r10d %r11d %r12d %r13d %r14d %r15d %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Cox / Rixner32

Instructions Long word l (4 Bytes) ↔ Quad word q (8 Bytes) New instructions:  movl ➙ movq  addl ➙ addq  sall ➙ salq  etc. 32-bit instructions that generate 32-bit results  Set higher order bits of destination register to 0  Example: addl Cox / RixnerAssembly33

32-bit code for swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Set Up Finish swap: pushl %ebp movl %esp,%ebp pushl %ebx movl8(%ebp), %edx movl12(%ebp), %ecx movl(%edx), %ebx movl(%ecx), %eax movl%eax, (%edx) movl%ebx, (%ecx) popl%ebx popl%ebp ret Cox / RixnerAssembly34

64-bit code for swap Operands passed in registers (why useful?)  First ( xp ) in %rdi, second ( yp ) in %rsi  64-bit pointers No stack operations required 32-bit data  Data held in registers %eax and %edx  movl operation void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Set Up Finish swap: movl(%rdi), %edx movl(%rsi), %eax movl%eax, (%rdi) movl%edx, (%rsi) ret Assembly35

64-bit code for long int swap 64-bit data  Data held in registers %rax and %rdx  movq operation “q” stands for quad-word void swap_l(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } Body Set Up Finish swap_l: movq (%rdi), %rdx movq (%rsi), %rax movq %rax, (%rdi) movq %rdx, (%rsi) ret Cox / RixnerAssembly36

Cox / RixnerAssembly37 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

Cox / RixnerAssembly38 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

Cox / RixnerAssembly39 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

Cox / RixnerAssembly40 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 %rdi, %rsi, etc., then stack2. Use call (jump to procedure, save return location on stack)3. Use sub %rsp to create new stack frame4. Use %rax 5. Use ret

Cox / RixnerAssembly41 Program Stack Figure 3.3 is reproduced from the AMD64 ABI Draft by Matz et al.

Cox / RixnerAssembly42 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  …

Cox / RixnerAssembly43 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 Separate instructions & data

Cox / RixnerAssembly44 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 Reserve memory with initial data values

Cox / RixnerAssembly45 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 Correlate source code with assembly/machine

Cox / RixnerAssembly46 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 The label’s value is the address of the subsequent instruction or pseudo- instruction

Cox / RixnerAssembly47 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

Cox / RixnerAssembly48 Simulate 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

Cox / RixnerAssembly49 Simulate Program … (cont.) movl $0, %eax leave ret.LFE3:.size main,.-main

Exercise Cox / RixnerAssembly50 ` ` ` ` 0x x x x c 0x x x x c 0x x x x c 0x x x x c 0x x x x c 0x x x x c 0x x x x c 0x x x x c 0x x x %rbp %rsp %rdi %rsi %esi %edi initial values: %rbp %rsp 0x x c %eax %ecx %edx

Cox / RixnerAssembly51 More x86-64 Assembly Chapter 3 of the textbook explains x86 and x86-64 assembly in greater detail  More examples translated from C  More detail than you’re expected to know for this 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)

Cox / RixnerAssembly52 Next Time Program Linking