Credits and Disclaimers

Similar documents
Credits and Disclaimers

CS241 Computer Organization Spring 2015 IA

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Credits and Disclaimers

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

The x86 Architecture

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Credits and Disclaimers

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

CS241 Computer Organization Spring Introduction to Assembly

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

x86 Programming I CSE 351 Winter

Instruction Set Architectures

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Process Layout and Function Calls

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Instruction Set Architectures

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

x86 assembly CS449 Spring 2016

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Introduction to IA-32. Jo, Heeseung

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

INTRODUCTION TO IA-32. Jo, Heeseung

Instruction Set Architecture (ISA) Data Types

Sungkyunkwan University

Assembly I: Basic Operations. Jo, Heeseung

x86 64 Programming I CSE 351 Autumn 2018 Instructor: Justin Hsia

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Machine Level Programming I: Basics

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Compiler Construction D7011E

Addressing Modes on the x86

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

CS Bootcamp x86-64 Autumn 2015

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Interfacing Compiler and Hardware. Computer Systems Architecture. Processor Types And Instruction Sets. What Instructions Should A Processor Offer?

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Credits and Disclaimers

Complex Instruction Set Computer (CISC)

Machine-Level Programming Introduction

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Instruction Set Architectures

Introduction to 8086 Assembly

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Today: Machine Programming I: Basics. Machine Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

The von Neumann Machine

Assembly Language: IA-32 Instructions

x86 Assembly Tutorial COS 318: Fall 2017

CSCI 192 Engineering Programming 2. Assembly Language

Introduction to Machine/Assembler Language

x86 assembly CS449 Fall 2017

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

X86 Assembly -Data II:1

Machine Programming 1: Introduction

x86 architecture et similia

Intro to GNU Assembly Language on Intel Processors

Today: Machine Programming I: Basics

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

System calls and assembler

Machine-Level Programming I: Introduction Jan. 30, 2001

W4118: PC Hardware and x86. Junfeng Yang

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

How Software Executes

Reverse Engineering II: The Basics

Lecture 3: Instruction Set Architecture

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

The von Neumann Machine

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013

LAB 5 Arithmetic Operations Simple Calculator

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

CS642: Computer Security

Credits to Randy Bryant & Dave O Hallaron

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Machine-Level Programming II: Control and Arithmetic

Machine/Assembler Language Putting It All Together

Giving credit where credit is due

Code segment Stack segment

Module 3 Instruction Set Architecture (ISA)

Lab 2: Introduction to Assembly Language Programming

Chapter 3: Addressing Modes

Putting the pieces together

Machine Programming 2: Control flow

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Transcription:

Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron /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.5.2 on Ubuntu Linux. Unless noted otherwise, the assembly code was generated using the following command line: gcc S m32 O0 file.c AT&T assembly syntax is used, rather than Intel syntax, since that is what the gcc tools use.

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

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

IA-32 Nomenclature 4 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 5.file.text.globl main.type main: pushl subl leal leave ret.size "simplest.c" main, @function %ebp %esp, %ebp $16, %esp $5, -4(%ebp) $16, -8(%ebp) -8(%ebp), %eax -4(%ebp), %edx (%edx,%eax), %eax %eax, -12(%ebp) $0, %eax gcc -O1 -S -Wall -m32 simplest.c int main() { main,.-main.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2".section.note.GNU-stack,"",@progbits } 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: esp address of the top element on the stack ebp address of the first element in the current stack frame 6 int main() { } int x, y, t; x = 5; y = 16; t = x + y; return 0; ebp ebp 4 ebp 8 ebp - 12 old value of ebp x y t the Stack increasing addresses

Register-Memory Data Transfers 7 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: src, dest dest := src This copies a 32-bit value from src into dest. 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 8 IA-32 integer 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 8 Register: One of 8 integer registers Example: %eax, %edx (reg names preceded by '%') 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

Basic Examples 9 x86 assembly C analog $0x5, %eax a = 5; $42, %ebx b = 42; %ecx, %edx d = c; %eax, (%ebx) *b = a (%ebx), %eax a = *b Mapping: a b c d reg %eax %ebx %ecx %edx

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

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

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

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

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

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

Aside: leal You also noticed the use of the leal instruction: 16... leal... (%eax,%eax,2), %edx # edx = eax + 2*eax The particular form of the instruction used here on the previous slide is: leal (src1, src2), dst dst = src2 + src1 The execution of the instruction offers some additional performance advantages.

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

Integer Arithmetic Instructions We have the expected addition operation: addl rightop, leftop leftop = leftop + rightop 18 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.

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 19 (Yes, there is a division instruction, but its interface is confusing and we will not need it.)