Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified 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:
1 Machine-Level Programming I: Basics Computer Systems Organization Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics /18-213:
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics Sep. 15, 2015.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2015 Instructor: John Barr * Modified slides.
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.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics /18-213:
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics MCS-284: Computer.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics CENG331 - Computer.
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.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today: Machine Programming I: Basics History of Intel.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics CS140 – Computer.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
Spring 2016Assembly Review Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Today: Machine Programming I: Basics
A job ad at a game programming company
Machine-Level Programming I: Basics
Samira Khan University of Virginia Feb 2, 2017
Instruction Set Architecture
Machine-Level Programming I: Basics
Machine-Level Programming I:
Credits and Disclaimers
Assembly Programming II CSE 351 Spring 2017
IA32 Processors Evolutionary Design
Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5th Lecture, Jan. 28, 2016 Instructors: Seth Copen Goldstein & Franz.
Instructor: Phil Gibbons
x86 Programming I CSE 351 Autumn 2016
Machine-Level Programming 1 Introduction
Lecture 5 Machine Programming
Instructor: David Ferry
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
CSE 351 Section 10 The END…Almost 3/7/12
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Assembly Programming II CSE 410 Winter 2017
x86-64 Programming I CSE 351 Autumn 2017
Authors: Randal E. Bryant and David R. O’Hallaron
Machine-Level Programming: Introduction
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
x86-64 Programming I CSE 351 Winter 2018
x86-64 Programming II CSE 351 Summer 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming I CSE 351 Winter 2018
Machine-Level Representation of Programs (x86-64)
x86-64 Programming I CSE 351 Autumn 2018
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Get To Know Your Compiler
Machine-Level Programming I: Basics
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
x86-64 Programming II CSE 351 Winter 2019
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
“Way easier than when we were students”
CS201- Lecture 8 IA32 Flow Control
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Credits and Disclaimers
Lecture 5 Machine Programming
Credits and Disclaimers
Instructor: Brian Railing
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming I CSE 351 Spring 2019
CS201- Lecture 7 IA32 Data Access and Operations Part II
Presentation transcript:

Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron, 2015

Machine Programming I: Basics History of Intel processors and architectures C, assembly, machine code Assembly Basics: Registers, operands, move Intro to x86-64

Special memory areas: stack and heap Kernel virtual memory Memory mapped region for shared libraries Run-time heap (created at runtime by malloc) User stack (created at runtime) Unused Read/write segment (.data, .bss) Read-only segment (.init, .text, .rodata) Stack Space in Memory allocated to each running program (called a process) Used to store “temporary” variable values Used to store variables for functions Heap Space in Memory allocated to each running program (process) Used to store dynamically allocated variables

Example of Simple Addressing Modes void swap (long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } swap: movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret Note: if you look at the assembly code, it’s much more complicated; we’re ignoring some code to simplify.

Understanding Swap() Memory Registers void swap (long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi %rsi %rax %rdx Register Value %rdi xp %rsi yp %rax t0 %rdx t1 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 123 %rdi %rsi %rax %rdx 0x120 0x100 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 123 %rdi %rsi %rax %rdx 0x120 0x100 123 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 123 %rdi %rsi %rax %rdx 0x120 0x100 123 456 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 456 %rdi %rsi %rax %rdx 0x120 0x100 123 456 456 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

Understanding Swap() Memory Registers 0x120 0x118 0x110 0x108 0x100 Address 456 %rdi %rsi %rax %rdx 0x120 0x100 123 456 123 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

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 16 integer registers Ri: Index register: Any, except for %rsp Unlikely you’d use %rbp, 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]] D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+D]

Address Computation Examples %rdx 0xf000 %rcx 0x100 Expression Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)

Address Computation Examples %edx 0xf000 %ecx 0x100 Expression Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2) 0xf000 + 0x8 0xf008 0xf000 + 0x100 0xf100 0xf000 + 4*0x100 0xf400 2*0xf000 + 0x80 0x1e080

Address Computation Instruction leaq Src,Dest Src is address mode expression Set Dest to address denoted by expression Format Looks like a memory access Does not actually access memory Rather, calculates the memory address, then stores in a register Uses Computing addresses without a memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y k = 1, 2, 4, or 8. lea = load effective address

Example Converted to ASM by compiler: long m12(long x) { return x*12; } leaq (%rdi,%rdi,2), %rax ;t <- x+x*2 salq $2, %rax ;return t<<2 ; or t * 4

Address Computation Instruction Expression Result leaq 6(%rax), %rdx leaq (%rax, %rcx), %rdx leaq (%rax, %rcx, 4), %rdx leaq 7(%rax, %rax, 8), %rdx leaq 0xA(,%rax, 4), %rdx leaq 9(%rax,%rcx, 2), %rdx Assume %rax holds value x and %rcx holds value y * Note the leading comma in the next to last entry

Address Computation Instruction Expression Result leal 6(%rax), %rdx leal (%rax, %rcx), %rdx leal (%rax, %rcx, 4), %rdx leal 7(%rax, %rax, 8), %rdx leal 0xA(,%rax, 4), %rdx leal 9(%rax,%rcx, 2), %rdx %rdx  x + 6 %rdx  x + y %rdx x + (y * 4) %rdx x + (x * 8) + 7 = (x * 9) + 7 %rdx  (x * 4) + 10 %rdx x + (y * 2) + 9 Assume %rax holds value x and %rcx holds value y * Note the leading comma in the next to last entry

Machine Programming II: Summary History of Intel processors and architectures Evolutionary design leads to many quirks and artifacts C, assembly, machine code New forms of visible state: program counter, registers,… Compiler must transform statements, expressions, procedures into low-level instruction sequences Assembly Basics: Registers, operands, move The x86-64 move instructions cover wide range of data movement forms Intro to x86-64 A major departure from the style of code seen in IA32