Credits and Disclaimers

Slides:



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

Introduction to Machine/Assembler Language Noah Mendelsohn Tufts University Web:
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
COMP 2003: Assembly Language and Digital Logic
Lect 3: Instruction Set and Addressing Modes. 386 Instruction Set (3.4) –Basic Instruction Set : 8086/8088 instruction set –Extended Instruction Set :
Instruction Set Architectures
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Lect 4: Instruction Set and Addressing Modes. 386 Instruction Set (3.4)  Basic Instruction Set : 8086/8088 instruction set  Extended Instruction Set.
1 Machine-Level Programming I: Basics Computer Systems Organization Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
6.828: PC hardware and x86 Frans Kaashoek
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
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:
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
University of Washington Basics of Machine Programming The Hardware/Software Interface CSE351 Winter 2013.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
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.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
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);
Precept 7: Introduction to IA-32 Assembly Language Programming
Instruction Set Architecture
Credits and Disclaimers
Machine-Level Programming I: Basics
Assembly Programming I CSE 351 Spring 2017
Homework Reading Lab with your assigned section starts next week
IA32 Processors Evolutionary Design
Credits and Disclaimers
x86 Programming I CSE 351 Autumn 2016
Homework Reading Continue work on mp1
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming 1 Introduction
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
8086 Registers Module M14.2 Sections 9.2, 10.1.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Machine-Level Programming 2 Control Flow
MIPS Procedure Calls CSE 378 – Section 3.
Authors: Randal E. Bryant and David R. O’Hallaron
Computer Architecture CST 250
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
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.
Machine-Level Programming I: Basics
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Credits and Disclaimers
The von Neumann Machine
Credits and Disclaimers
Credits and Disclaimers
Credits and Disclaimers
Credits and Disclaimers
x86-64 Programming I CSE 351 Spring 2019
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Assembly Programming I CSE 351 Spring 2017
Low level Programming.
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.

Program Translation Overview text C program (p1.c p2.c) Compiler (gcc -S) text Asm program (p1.s p2.s) Assembler (gcc or as) binary Object program (p1.o p2.o) Static libraries (.a) Linker (gcc or ld) binary Executable program (p)

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

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

X86-64 Nomenclature Due to the long history of the x86 architecture, the terminology for data lengths can be somewhat confusing: byte b 8 bits, no surprises there short s 16-bit integer or 32-bit float word w 16-bit value long l 32-bit integer or 64-bit float (aka double word) quad q 64-bit integer The single-character abbreviations are used in the names of many of the x86 assembly instructions to indicate the length of the operands. As long as the widths of the operands match, any of these suffixes can be used with the assembly instructions that are discussed in the following slides; for simplicity, we will generally restrict the examples to operations on long values.

Simple Example: C to Assembly .file "simplest.c" .text .globl main .type main, @function main: pushq %rbp movq %rsp, %rbp subq $16, %rsp movl $5, -4(%rbp) movl $16, -8(%rbp) movl -8(%rbp), %eax movl -4(%rbp), %edx addl %edx, %eax movl %eax, -12(%rbp) movl $0, %eax leave ret .size main, .-main .ident "GCC: (GNU) 4.8.3 20140911 (Red Hat 4.8.3-9)” .section .note.GNU-stack,"",@progbits gcc –O0 -S –Wall -m64 simplest.c int main() { int x, y, t; x = 5; y = 16; t = x + y; return 0; }

Simple Example: Memory Layout Local variables and function parameters are stored in memory, and organized in a stack frame. Two registers are used to keep track of the organization: rsp address of the top element on the stack rbp address of the first element in the current stack frame int main() { int x, y, t; x = 5; y = 16; t = x + y; return 0; } rbp old value of rbp rbp – 4 x rbp – 8 y rbp - 12 t rsp increasing addresses the Stack

Register-Memory Data Transfers Many machine-level operations require that data be transferred between memory and registers. The most basic instructions for this are the variants of the mov instruction: movl src, dest dest := src This copies a 32-bit value from src into dest. movq moves 64 bit values in the same fashion. Despite the name, it has no effect on the value of src. The two operands can be specified in a number of ways: - immediate values - one of the 16 x86-64 integer registers (or their virtual registers) - memory address

Operand Specifications Immediate: Constant integer data Example: $0x400, $-533 Like C constant, but prefixed with ‘$’ Encoded with 1, 2, or 4 bytes Register: One of the16 integer registers Example: %eax, %edx (reg names preceded by '%') But %rsp and %rbp reserved for special use Others have special uses for particular instructions Memory: N consecutive bytes of memory at address given by register, N is specified by the instruction name, movl = 4 bytes, movq = 8 bytes. Simplest example: (%rax) Various other “address modes”

Basic Examples X86-64 assembly C analog movl $0x10, %eax a = 16; movl $42, %ebx b = 42; movl %ecx, %edx d = c; movl %eax, (%rbx) *b = a movl (%rbx), %eax a = *b mov1 -4(%rbp), %eax a = *(rbp – 4) Mapping: reg a %eax b %ebx c %ecx d %edx

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 x rbp – 8 y rbp - 12 t the Stack movl $5, -4(%rbp) Registers movl $16, -8(%rbp) eax ebx ecx edx edi esi movl -8(%rbp), %eax movl -4(%rbp), %edx addl %edx, %eax movl %eax, -12(%rbp)

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 5 rbp – 8 ?? rbp - 12 the Stack movl $5, -4(%rbp) Registers eax ?? edx

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 5 rbp – 8 16 rbp - 12 ?? the Stack movl $5, -4(%rbp) Registers movl $16, -8(%rbp) eax ?? edx

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 5 rbp – 8 16 rbp - 12 ?? the Stack movl $5, -4(%rbp) Registers movl $16, -8(%rbp) eax 16 edx ?? movl -8(%rbp), %eax movl -4(%rbp), %edx addl %edx, %eax movl %eax, -12(%rbp)

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 5 rbp – 8 16 rbp - 12 ?? the Stack movl $5, -4(%rbp) Registers movl $16, -8(%rbp) eax 16 edx 5 movl -8(%rbp), %eax movl -4(%rbp), %edx addl %edx, %eax movl %eax, -12(%rbp)

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 5 rbp – 8 16 rbp - 12 ?? the Stack movl $5, -4(%rbp) Registers movl $16, -8(%rbp) eax 21 edx 5 movl -8(%rbp), %eax movl -4(%rbp), %edx addl %edx, %eax movl %eax, -12(%rbp)

Integer Arithmetic Instructions We have the expected addition operation: addl rightop, leftop leftop = leftop + rightop The operand ordering shown here is probably confusing: - As usual, the destination is listed second. - But, that's also the first (left-hand) operand when the arithmetic is performed. This same pattern is followed for all the binary integer arithmetic instructions. See the discussion of AT&T vs Intel syntax later in the notes for an historical perspective on this.

C to Assembly the Stack Registers int main() { int x, y, t; x = 5; return 0; } rbp old value of rbp rbp – 4 5 rbp – 8 16 rbp - 12 21 the Stack movl $5, -4(%rbp) Registers movl $16, -8(%rbp) eax 21 edx 5 movl -8(%rbp), %eax movl -4(%rbp), %edx addl %edx, %eax movl %eax, -12(%rbp)

More Arithmetic Instructions In addition: subl rightop, leftop leftop = leftop - rightop imull rightop, leftop leftop = leftop * rightop negl op op = -op incl op op = op + 1 decl op op = op - 1