Machine Programming II: Instruc8ons



Similar documents
Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Machine-Level Programming II: Arithmetic & Control

Assembly Language: Function Calls" Jennifer Rexford!

Instruction Set Architecture

Machine-Level Programming I: Basics

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

CS61: Systems Programing and Machine Organization

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

Faculty of Engineering Student Number:

Chapter 4 Processor Architecture

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Lecture 27 C and Assembly

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

Computer Organization and Assembly Language

For a 64-bit system. I - Presentation Of The Shellcode

Computer Organization and Architecture

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

x64 Cheat Sheet Fall 2015

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Return-oriented programming without returns

G-Free: Defeating Return-Oriented Programming through Gadget-less Binaries

CHAPTER 6 TASK MANAGEMENT

612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap Famiglie di processori) PROBLEMS

Chapter 7D The Java Virtual Machine

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

The 80x86 Instruction Set

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Outline. Lecture 3. Basics. Logical vs. physical memory physical memory. x86 byte ordering

Reduced Instruction Set Computer (RISC)

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Hacking the Preboot execution Environment

CS:APP Chapter 4 Computer Architecture. Wrap-Up. William J. Taffe Plymouth State University. using the slides of

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture

Compilers I - Chapter 4: Generating Better Code

PCI BIOS SPECIFICATION. Revision 2.1

Instruction Set Architecture (ISA)

Systems Design & Programming Data Movement Instructions. Intel Assembly

Writing an 8086 emulator in Python

Programming Languages

Intel 8086 architecture

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

Computer Architectures

Complete 8086 instruction set

Assembly Language Tutorial

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Modbus RTU Communications RX/WX and MRX/MWX

Stack Overflows. Mitchell Adair

8085 INSTRUCTION SET

64-Bit NASM Notes. Invoking 64-Bit NASM

Language Processing Systems

COMPUTERS ORGANIZATION 2ND YEAR COMPUTE SCIENCE MANAGEMENT ENGINEERING JOSÉ GARCÍA RODRÍGUEZ JOSÉ ANTONIO SERRA PÉREZ

PC Assembly Language. Paul A. Carter

x86-64 Machine-Level Programming

Instruction Set Architecture

8. MACROS, Modules, and Mouse

TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such

Z80 Instruction Set. Z80 Assembly Language

ELEG3924 Microprocessor Ch.7 Programming In C

Buffer Overflows. Security 2011

Flash Microcontroller. Memory Organization. Memory Organization

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

How To Use A Computer With A Screen On It (For A Powerbook)

The Hardware/So7ware Interface CSE351 Spring 2010 (Inaugural Edi5on) 1 st Lecture, 29 March

Chapter 1. Bootstrap. Hardware

The x86 PC: Assembly Language, Design, and Interfacing 5 th Edition

Review: MIPS Addressing Modes/Instruction Formats

Off-by-One exploitation tutorial

Using the RDTSC Instruction for Performance Monitoring

Under The Hood: The System Call

X86-64 Architecture Guide

Mouse Programming Mouse Interrupts Useful Mouse functions Mouselib.h Mouselib.c

Egil Aspevik Martinsen Polymorphic Viruses. Material from Master Thesis «Detection of Junk Instructions in Malicious Software»

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Andreas Herrmann. AMD Operating System Research Center

CVE Adobe Flash Player Integer Overflow Vulnerability Analysis

Instruction Set Design

Keil C51 Cross Compiler

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

8086 Microprocessor (cont..)

Intel Virtualization Technology FlexMigration Application Note

Computer Architectures

An Overview of Stack Architecture and the PSC 1000 Microprocessor

MICROPROCESSOR BCA IV Sem MULTIPLE CHOICE QUESTIONS

Application-Specific Attacks: Leveraging the ActionScript Virtual Machine

CPU Organization and Assembly Language

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

1 The Java Virtual Machine

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

CSC 2405: Computer Systems II

PART B QUESTIONS AND ANSWERS UNIT I

CS Computer Security Thirteenth topic: System attacks. defenses

Abysssec Research. 1) Advisory information. 2) Vulnerable version

Automated Repair of Binary and Assembly Programs for Cooperating Embedded Devices

IA-32 Intel Architecture Software Developer s Manual

Transcription:

Machine Programming II: Instrucons Move instrucons, registers, and operands Complete addressing mode, address computaon (leal) Arithmec operaons (including some x6 6 instrucons) Condion codes Control, uncondional and condional branches While loops CSE351 Inaugural Edion Spring 2010 1 Integer Registers (IA32) %ax %cx %dx %bx %si %di %sp %bp %ah %ch %dh %bh %al %cl %dl %bl 16 bit virtual registers (backwards compability) general purpose accumulate counter data base source index destination index stack pointer base pointer Origin (mostly obsolete) CSE351 Inaugural Edion Spring 2010 2

Moving Data: IA32 Moving Data movx Source, Dest x is one of b, w, l movl Source, Dest: Move byte long word movw Source, Dest: Move 2 byte word movb Source, Dest: Move 1 byte byte Lots of these in typical code CSE351 Inaugural Edion Spring 2010 3 Moving Data: IA32 Moving Data movl Source, Dest: Operand Types Immediate: Constant integer data Example: $0x00, $-533 Like C constant, but prefixed with $ Encoded with 1, 2, or bytes Register: One of integer registers Example:, But and reserved for special use Others have special uses for parkcular instruckons Memory: consecukve bytes of memory at address given by register Simplest example: () Various other address modes CSE351 Inaugural Edion Spring 2010

movl Operand Combinaons Source Dest Src,Dest C Analog Imm Reg Mem movl $0x, temp = 0x; movl $-17,() *p = -17; movl Reg Reg Mem movl, movl,() temp2 = temp1; *p = temp; Mem Reg movl (), temp = *p; Cannot do memory memory transfer with a single instruc<on CSE351 Inaugural Edion Spring 2010 5 Simple Memory Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address movl (), Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl (), CSE351 Inaugural Edion Spring 2010 6

Using Simple Addressing Modes void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; swap: pushl movl, pushl movl (), movl (), movl (), movl (), movl,() movl,() movl -(), movl, popl ret Set Up Body Finish CSE351 Inaugural Edion Spring 2010 7 Using Simple Addressing Modes void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; swap: pushl movl, pushl movl (), movl (), movl (), movl (), movl,() movl,() movl -(), movl, popl ret Set Up Body Finish CSE351 Inaugural Edion Spring 2010

Understanding Swap void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; Register Value yp xp t1 t0 0 - yp xp Old Old Stack (in memory) movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 9 Understanding Swap yp xp 0 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx - 3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 CSE351 Inaugural Edion Spring 2010 10

Understanding Swap yp xp 0 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx - 3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 CSE351 Inaugural Edion Spring 2010 11 Understanding Swap yp xp 0-3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010

Understanding Swap 56 yp xp 0-3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 13 Understanding Swap 56 3 yp xp 0-3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 1

Understanding Swap 56 56 3 yp xp 0-56 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 15 Understanding Swap 56 3 yp xp 0-56 3 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 16

Complete Memory Addressing Modes Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant displacement 1, 2, or bytes Rb: Base register: Any of integer registers Ri: Index register: Any, except for Unlikely you d use, either S: Scale: 1, 2,, or (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]] CSE351 Inaugural Edion Spring 2010 17 Address Computaon Examples 0xf000 Expression Address Computaon Address 0x() (,) (,,) 0x0(,,2) CSE351 Inaugural Edion Spring 2010 1

Address Computaon Examples 0xf000 Expression Address Computaon Address 0x() 0xf000 + 0x 0xf00 (,) 0xf000 + 0xf100 (,,) 0xf000 + * 0xf00 0x0(,,2) 2*0xf000 + 0x0 0x1e00 CSE351 Inaugural Edion Spring 2010 19 Address Computaon Instrucon leal Src,Dest Src is address mode expression Set Dest to address denoted by expression Uses CompuKng addresses without a memory reference E.g., translakon of p = &x[i]; CompuKng arithmekc expressions of the form x + k*i k = 1, 2,, or CSE351 Inaugural Edion Spring 2010 20

Some Arithmec Operaons Two Operand Instrucons: Format Computa<on addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest - Src imull Src,Dest Dest = Dest * Src sall Src,Dest Dest = Dest << Src Also called shll sarl Src,Dest Dest = Dest >> Src Arithme<c shrl Src,Dest Dest = Dest >> Src Logical xorl Src,Dest Dest = Dest ^ Src andl Src,Dest Dest = Dest & Src orl Src,Dest Dest = Dest Src No disncon between signed and unsigned int (why?) CSE351 Inaugural Edion Spring 2010 21 Some Arithmec Operaons One Operand Instrucons incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = -Dest notl Dest Dest = ~Dest See book for more instrucons CSE351 Inaugural Edion Spring 2010 22

Using leal for Arithmec Expressions int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; arith: pushl movl, movl (), movl (), leal (,), leal (,,2), sall $, addl 16(), leal (,), imull, movl, popl ret Set Up Body Finish CSE351 Inaugural Edion Spring 2010 23 Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # What does each of ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx these instrucons = *y (t) addl 16(), # ecx = mean? z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 2

Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 25 Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 26

Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 27 Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 2

Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), # eax = x xorl (), # eax = x^y sarl $17, # eax = t1>>17 andl $15, # eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 29 Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), eax = x xorl (), eax = x^y (t1) sarl $17, eax = t1>>17 (t2) andl $15, eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 30

Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), eax = x xorl (), eax = x^y (t1) sarl $17, eax = t1>>17 (t2) andl $15, eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 31 Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; 2 13 = 192, 2 13 7 = 15 logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), eax = x xorl (), eax = x^y (t1) sarl $17, eax = t1>>17 (t2) andl $15, eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 32