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

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

Credits and Disclaimers

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

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

Instruction Set Architecture (ISA) Data Types

CS241 Computer Organization Spring 2015 IA

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

Credits and Disclaimers

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

Instruction Set Architectures

The x86 Architecture

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

Instruction Set Architectures

x86 Programming I CSE 351 Winter

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

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

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

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

EEM336 Microprocessors I. Addressing Modes

The von Neumann Machine

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

RISC I from Berkeley. 44k Transistors 1Mhz 77mm^2

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

A4 Sample Solution Ch3

The von Neumann Machine

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

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

The Hardware/Software Interface CSE351 Spring 2013

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

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

Introduction to IA-32. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

How Software Executes

Lecture (02) The Microprocessor and Its Architecture By: Dr. Ahmed ElShafee

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

Machine/Assembler Language Putting It All Together

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

Process Layout and Function Calls

CS Bootcamp x86-64 Autumn 2015

UNIT 2 PROCESSORS ORGANIZATION CONT.

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

Introduction to Machine/Assembler Language

How Software Executes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Machine Level Programming I: Basics

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CS 16: Assembly Language Programming for the IBM PC and Compatibles

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

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

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

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

Sungkyunkwan University

Complex Instruction Set Computer (CISC)

Computer Architecture and System Programming Laboratory. TA Session 3

Instruction Set Architectures

Reverse Engineering II: The Basics

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

CS241 Computer Organization Spring Introduction to Assembly

Advanced Microprocessors

Today: Machine Programming I: Basics

Do not turn the page until 11:30.

EEM336 Microprocessors I. The Microprocessor and Its Architecture

Function Calls and Stack

Module 3 Instruction Set Architecture (ISA)

MACHINE-LEVEL PROGRAMMING I: BASICS

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

Assembly Language: IA-32 Instructions

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

Do not turn the page until 5:10.

16.317: Microprocessor Systems Design I Spring 2014

Basic Execution Environment

Addressing Modes on the x86

6/17/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

x64 Cheat Sheet Fall 2014

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Chapter 3: Addressing Modes

Code segment Stack segment

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Chapter 2: The Microprocessor and its Architecture

LAB 5 Arithmetic Operations Simple Calculator

Computer Systems Organization V Fall 2009

ADDRESSING MODES. Operands specify the data to be used by an instruction

Machine-Level Programming Introduction

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

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

Hardware and Software Architecture. Chapter 2

CS 107 Lecture 10: Assembly Part I

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

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

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

Lecture 3 CIS 341: COMPILERS

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Machine Level Programming I: Basics. Credits to Randy Bryant & Dave O Hallaron

Lecture 2 Assembly Language

Intel 8086: Instruction Set

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

Introduction to 8086 Assembly

Transcription:

x86 Assembler Computer Systems: Sections 3.1-3.5

Disclaimer I am not an x86 assembler expert. I have never written an x86 assembler program. (I am proficient in IBM S/360 Assembler and LC3 Assembler.) You will NOT be expected to WRITE x86 assembler. You WILL be expected to be able to READ x86 assembler!

x86 Generalizations Designed to be Compiler Friendly Stack manipulation instructions ALU uses data from memory as well as registers! Downward Compatible Newer versions are supersets of older versions CISC (Complex Instruction Set Computing)

RISC Concept ADD ACCUM,Y

CISC Concept IMULT X,Y Micro-code IMULT -> SHIFTL 1,X ADD ACCUM,Y SHIFTL 1, X ADD ACCUM,Y

x86 Integer Registers 64 32 16 8 Origin %rax %eax %ax %ah %al Accumulate %rcx %ecx %cx %ch %cl Counter %rdx %edx %dx %dh %dl Data %rbx %ebx %bx %bh %bl Base %rsi %esi %si Source Index %rdi %edi %di Destination Index %rsp %esp %sp Stack Pointer %rbp %ebp %bp base Pointer

x86 Data Types No type checking - Instruction and/or context implies data type Arithmetic instructions treat operands as numbers Either signed or unsigned! Instruction suffix used to identify precision of arguments b 1 byte (8 bits) w word (2 bytes, 16 bits) l long word (4 bytes, 32 bits) q quad word (8 bytes, 64 bits) With no suffix, register type implies precision of arguments ah/al b 8 bits ax w 16 bits eax l 32 bits rax q 64 bits Floating point 4, 8, or 10 bytes

x86 Assembler Syntax [<label>:] <mnemonic> arg1[,arg2 ] [; comment] Label optional identifies start of this line mnemonic See http://ref.x86asm.net/ for a complete list Up to 4 arguments Comment ends at the end of this line

Assembler Argument Generalities Reads LEFT to RIGHT (AT&T syntax INTEL syntax is RtoL) mov 5,eax ; move the constant 5 => eax add bx, ax ; add bx + ax => ax (ax = bx+ax; ) Arguments may be: register, constant value, memory reference Only ONE argument may be a memory reference! at least one argument must be a register or constant Optional argument prefixes % - register e.g. movl 5,%eax $ - constant value e.g. movl $5,%eax

Constant (literal) values Similar to C Conventions. Numbers are decimal by default, octal if preceded by 0, hex if preceded by 0x Single characters are enclosed in single quotes, including special characters such as \n, \t Strings are arrays of characters enclosed in double quotes Labels may be used in place of addresses

Basic x86 addressing modes Normal: Register contains address of target in memory Parenthesis indicate Use Value at this address movl (%ecx),%eax ; Put the value at memory[ecx] into eax Displacement: Register is near address of target in memory Offset from register specified before parenthesis movl 8(%ebp),%edx ; Put the value at 8 past the base pointer into edx See http://en.wikipedia.org/wiki/x86#addressing_modes if you need the entire story but it s complicated

Table Addressing Mode Generally: <OFFSET>(<BASEREG>,<ROWREG>,<WIDTH>) e.g. 4(%ebx,%ecx,12) Address: = (<BASEREG>)+(ROWREG)*<WIDTH> + <OFFSET> (<BASEREG>) <WIDTH> e.g. (%ebx)+(%ecx)*12+4 <(ROWREG)> <OFFSET>

Table Addressing Mode Also: <BASE>(<COLREG>,<ROWREG>,<WIDTH>) e.g. mytable(%ebx,%ecx,12) Address: = <BASE> + (<COLREG>)+(ROWREG)*<WIDTH> <BASE> <WIDTH> e.g. mytable+(%ecx)*12+(%ebx) <(ROWREG)> (<OFFREG>)

The MOV instruction Most often used instruction! More copy than move Copies 1,2,4, or 8 bytes from ARG1 to ARG2 mov $-12,%eax ; put -12 into 4 byte eax register mov $0xffff,(%esp) ; put -1 at top of stack mov 12(%ebp),%eax ; copy data at 12 past base pointer into eax movb chartab(%eax,%ebx,14),%al ; copy byte at row %ebx, col %eax of chartab into %al

Logic Instructions Standard 2 arg: and or xor shl shr and %ebx,%eax ; eax=eax & ebx shr $4,%eax; eax = (unsigned)eax>>4 Single argument: not neg not %eax ; flip bits in eax neg %ebx; take two s complement (flip bits and add 1) of ebx

Arithmetic Instructions Standard integer arithmetic: add sub add $10,(%eax); (*eax)=(*eax)+10 sub $4,%esp ; esp=esp-4 (move stack pointer down) Special integer arithmetic: imul idiv imul cannot write to memory idiv divides register pair (EDX:EAX) and puts quotient/remainder back Single argument: inc dec inc %eax; eax=eax+1 same as add eax,1 dec (%esp) ; decrement the value at the top of the stack by 1 Floating Point Instructions

Dealing with Pointers Load effective address: lea Used for implicit arrays/structures, etc. Calculates address from first argument, and writes that address to second lea $8(%esp),%eax ; %eax->array[0] move $0,%edx ; sum=0 loop: add (%eax),%edx ; sum=sum+(%eax) add $4,%eax ; increment pointer cmp $0,(%eax) ; is (%eax) >=0? jge loop ; Yes continue

Not done yet Next x86 control instructions