Credits and Disclaimers

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Copyright 2014 – Noah Mendelsohn UM Macro Assembler Functions Noah Mendelsohn Tufts University Web:
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
1 1 Machine-Level Programming IV: x86-64 Procedures, Data Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements 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.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
Credits and Disclaimers
Machine-Level Programming I: Basics
Credits and Disclaimers
CSCE 212 Computer Architecture
Credits and Disclaimers
143A: Principles of Operating Systems Lecture 4: Calling conventions
The Stack & Procedures CSE 351 Spring 2017
Machine-Level Programming III: Procedures
Recitation: Attack Lab
Machine-Level Programming II: Control
Switch & Procedures & The Stack I CSE 351 Winter 2017
Machine-Level Programming IV: x86-64 Procedures, Data
Machine-Level Programming II: Control Flow
x86-64 Programming III & The Stack CSE 351 Winter 2018
Assembly Programming IV CSE 351 Spring 2017
x86-64 Programming III & The Stack CSE 351 Winter 2018
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Arrays CSE 351 Winter
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:
Carnegie Mellon Wrap-up of Machine-Level Programming II: Control : Introduction to Computer Systems Sept. 18, 2018.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
x86-64 Programming III & The Stack CSE 351 Autumn 2017
The Stack & Procedures CSE 351 Winter 2018
Machine Level Representation of Programs (IV)
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Arrays CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants:
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Get To Know Your Compiler
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Carnegie Mellon Ithaca College
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Instructor: Fatma CORUT ERGİN
CS201- Lecture 8 IA32 Flow Control
Loops, Switches, Intro to Stack & Procedures CSE 351 Winter 2019
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.
Carnegie Mellon Ithaca College
Credits and Disclaimers
Credits and Disclaimers
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Credits and Disclaimers
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

Credits and Disclaimers The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3nd Edition by Bryant and O'Hallaron x86 Assembly/GAS Syntax on WikiBooks (http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax) Using Assembly Language in Linux by Phillip ?? (http://asm.sourceforge.net/articles/linasm.html) The C code was compiled to assembly with gcc version 4.8.3 on CentOS 7. Unless noted otherwise, the assembly code was generated using the following command line: gcc –S –m64 -fno-asynchronous-unwind-tables –mno-red-zone –O0 file.c AT&T assembly syntax is used, rather than Intel syntax, since that is what the gcc tools use.

C Example int main() { int x = 42, y = 99; int* p1 = &x; // p1 stores address of variable x int* p2 = &y; // p2 stores address of variable y int** p3 = &p2; // p3 stores address of variable p2 int aa = *p1; // aa stores value of the target of p1, 42 *p1 = 10; // the target of p1, which is x, stores 10 int bb = **p3; // bb stores value of the target of the // target of p3; p3 points to p1 and // p1 points to x, so bb gets value 99 return 0; }

x86-64 Assembly View the Stack . . . main: movl $42, -28(%rbp) leaq -28(%rbp), %rax movq %rax, -8(%rbp) leaq -32(%rbp), %rax movq %rax, -40(%rbp) leaq -40(%rbp), %rax movq %rax, -16(%rbp) movq -8(%rbp), %rax movl (%rax), %eax movl %eax, -20(%rbp) movl $10, (%rax) movq -16(%rbp), %rax movq (%rax), %rax movl %eax, -24(%rbp) rbp old rbp rbp - 8 p1 rbp - 16 p3 rbp - 20 aa rbp - 24 bb rbp – 28 x rbp – 32 y rbp - 40 p2 the Stack

x86-64 Assembly View the Stack . . . main: # x = 42; y = 99; movl $42, -28(%rbp) movl $99, -32(%rbp) rbp old rbp rbp - 8 p1 rbp - 16 p3 rbp - 20 aa rbp - 24 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p2 the Stack

x86-64 Assembly View the Stack . . . main: # p1 = &x leaq -28(%rbp), %rax # rax = rbp – 28 = &x movq %rax, -8(%rbp) # p1 = *(rbp – 8) = rax # p2 = &y leaq -32(%rbp), %rax movq %rax, -40(%rbp) rbp old rbp rbp - 8 p1: rbp - 28 rbp - 16 p3 rbp - 20 aa rbp - 24 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p2: rbp - 32 &x the address of x: rbp - 28 &y the address of y: rbp - 32 the Stack

x86-64 Assembly View the Stack . . . main: # p3 = &p2 leaq -40(%rbp), %rax # rax = rbp – 28 = &p2 movq %rax, -16(%rbp) # p3 = *(rbp – 16) = eax rbp old rbp rbp - 8 p1: rbp - 28 rbp - 16 p3: rpb - 40 rbp - 20 aa rbp - 24 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p2: rbp - 32 &p2 the address of p2: rbp – 40 the Stack

x86-64 Assembly View the Stack . . . main: # aa = *p1 movq -8(%rbp), %rax # rax = *(rbp – 8) = p1 movl (%rax), %eax # rax = *rax = *(*(rbp – 8) = *p1 movl %eax, -20(%rbp) # aa = *(rbp – 20) = eax rbp old rbp rbp - 8 p1: rbp - 28 rbp - 16 p3: rpb - 40 rbp - 20 aa: 42 rbp - 24 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p2: rbp - 32 the Stack

x86-64 Assembly View the Stack . . . main: # *p1 = 10 movq -8(%rbp), %rax # rax = *(rbp – 8) = p1 movl $10, (%rax) # *p1 = *rax = 10 rbp old rbp rbp - 8 p1: rbp - 28 rbp - 16 p3: rpb - 40 rbp - 20 aa: 42 rbp - 24 bb rbp – 28 x: 10 rbp – 32 y: 99 rbp - 40 p2: rbp - 32 the Stack

x86-64 Assembly View the Stack . . . main: # bb = **p3 movq -16(%rbp), %rax # rax = *(rbp – 16) = p3 movq (%rax), %rax # rax = *rax = *(*(rbp – 16)) = *p3 movl (%rax), %eax # rax = *(*(*(rbp – 16) ) = y = **p3 movl %eax, -24(%rbp) # bb = **p3 rbp old rbp rbp - 8 p1: rbp - 28 rbp - 16 p3: rpb - 40 rbp - 20 aa: 42 rbp - 24 bb: 99 rbp – 28 x: 10 rbp – 32 y: 99 rbp - 40 p2: rbp - 32 the Stack

C and x86-64 Array Example uint32_t ArrayExample(int * array, uint32_t size) { if (array == NULL) return 0; for (uint32_t x = 0; x < size; x++) array[x] = rand() % 1024; } return size;

x86-64 Assembly View the Stack . . . ArrayExample: cmpq $0, -40(%rbp) jne .L2 movl $0, %eax jmp .L3 .L2: movl $0, -20(%rbp) jmp .L4 .L5: # code snip .L4: movl -20(%rbp), %eax cmpl -44(%rbp), %eax jb .L5 movl -44(%rbp), %eax .L3: rbp old rbp rbp - 4 ... rbp – 20 loop variable x rbp - 40 array rbp - 44 size the Stack

x86-64 Assembly View the Stack . . . ArrayExample: # Test if int* array (i.e. %rbp – 40) is NULL. cmpq $0, -40(%rbp) # If array is not NULL jump to the body of the function. jne .L2 # We return 0 if array == NULL, so we put 0 in %eax then # jump to the very end of the function. movl $0, %eax jmp .L3 rbp old rbp rbp - 4 ... rbp – 20 loop variable x rbp - 40 array rbp - 44 size the Stack

x86-64 Assembly View the Stack . . . # After the body, do the loop test .L4: # Put the loop variable in %eax movl -20(%rbp), %eax # The loop test, compare x and size. # The condition in the C code was x < size # Performs the operation x – size, discarding the result. cmpl -44(%rbp), %eax # Repeat if x is below size jb .L5 rbp old rbp rbp - 4 ... rbp – 20 loop variable x rbp - 40 array rbp - 44 size the Stack

x86-64 Assembly View # The function body, after the if statement... movl $0, -20(%rbp) # initialize loop variable x jmp .L4 # jump to the loop test. .L5: movl -20(%rbp), %eax # get x for this iteration leaq 0(,%rax,4), %rdx # rdx = 4*rax, # i.e. x*sizeof(uint32_t) movq -40(%rbp), %rax # get the pointer, array leaq (%rdx,%rax), %rbx # add array + x*sizeof(uint32_t) # giving us a pointer to the # current element in the array # snip. . . %eax stores the result of rand() % 1024 movl %eax, (%rbx) # Remember: rbx is a pointer # to the current array element. # i.e. array + x*sizeof(uint32_t) # So we are storing the random # number into array index x addl $1, -20(%rbp) # update the loop variable x .L4 . . .

Aside: leal You also noticed another use of the leal instruction: . . . leal 0(,%rax,4), %rdx # rdx = 4*rax # i.e. x*sizeof(uint32_t) The particular form of the instruction used here is: leal Imm1(src1, src2, Imm2), dst dst = Imm2*src2 + src1 + Imm1 The execution of the instruction offers some additional performance advantages and is often used when indexing arrays, both for the “sizeof” calculation and to compute the pointer to the corresponding element.

Pointers as Parameters #include <stdint.h> int main() { uint32_t X = 100; uint32_t Y = 200; Swap(&X, &Y); return 0; } void Swap(uint32_t* A, uint32_t* B) { uint32_t Temp = *A; // Temp = 100 *A = *B; // X = 200 *B = Temp; // Y = 100 The pass-by-pointer protocol provides a called function with the ability to modify the value of the caller's variable.

x86-64 Assembly View the Stack . . . Swap: pushq %rbp movq %rsp, %rbp subq $32, %rsp movq %rdi, -24(%rbp) movq %rsi, -32(%rbp) movq -24(%rbp), %rax movl (%rax), %eax movl %eax, -4(%rbp) movq -32(%rbp), %rax movl (%rax), %edx movl %edx, (%rax) movl -4(%rbp), %edx leave ret (frame for main) X: 100 Y: 200 rbp + 8 return address rbp old rbp rbp - 4 Temp: ?? rbp - 8 rbp - 12 ... rbp – 24 &X rbp – 32 &Y the Stack