ECE 498 Linux Assembly Language Lecture 3

Similar documents
3.1 DATA MOVEMENT INSTRUCTIONS 45

Introduction to 8086 Assembly

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

x64 Cheat Sheet Fall 2014

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

APPENDIX C INSTRUCTION SET DESCRIPTIONS

8086 INSTRUCTION SET

CSE351 Spring 2018, Midterm Exam April 27, 2018

IFE: Course in Low Level Programing. Lecture 6

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

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

UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING. The Intel 8086 Instruction Set

Do not turn the page until 5:10.

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label

Assembly II: Control Flow

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

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

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

Basic Assembly Instructions

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Assembly Language Programming 64-bit environments

Branching and Looping

Intel Instruction Set (gas)

Conditional Processing

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

Assembly Language: IA-32 Instructions

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Selection and Iteration. Chapter 7 S. Dandamudi

Computer Architecture and System Programming Laboratory. TA Session 3

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

Week /8086 Microprocessor Programming

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

Lab 6: Conditional Processing

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CMSC 313 Lecture 05 [draft]

Do not turn the page until 11:30.

Basic Execution Environment

CSE 351 Midterm Exam

CPS104 Recitation: Assembly Programming

Lecture 2 Assembly Language

x86 Programming I CSE 351 Winter

Assembly Language II: Addressing Modes & Control Flow

Intel 8086: Instruction Set

Credits and Disclaimers

CS-202 Microprocessor and Assembly Language

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

Chapter Four Instructions Set

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Instruction Set Architectures

CS61 Section Solutions 3

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 3: Assembly Language Instructions II

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

How Software Executes

PESIT Bangalore South Campus

SOEN228, Winter Revision 1.2 Date: October 25,

Branching and Looping

CSCI 2021: x86-64 Control Flow

Summer 2003 Lecture 4 06/14/03

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

EC 333 Microprocessor and Interfacing Techniques (3+1)

Intel x86-64 and Y86-64 Instruction Set Architecture

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

x86 Programming II CSE 351 Winter

EEM336 Microprocessors I. Arithmetic and Logic Instructions

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

Practical Malware Analysis

Rev101. spritzers - CTF team. spritz.math.unipd.it/spritzers.html

Chapter 6 (Part a) Conditional Processing

The Hardware/Software Interface CSE351 Spring 2013

Computer Systems C S Cynthia Lee

We will first study the basic instructions for doing multiplications and divisions

Week /8086 Microprocessor Programming I

Machine Program: Procedure. Zhaoguo Wang

Am186 and Am188 Family Instruction Set Manual. February, 1997

Assembly Language Tutorial

Lab 3. The Art of Assembly Language (II)

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

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

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

CSE 351 Midterm - Winter 2015 Solutions

Defining and Using Simple Data Types

An Introduction to x86 ASM

CHAPTER 3 BASIC EXECUTION ENVIRONMENT

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

x86-64 Programming II

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 1

Corrections made in this version not seen in first lecture:

System calls and assembler

Review addressing modes

TUTORIAL. Emulador Emu8086 do. Microprocessador 8086

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

Von Neumann Architecture. Machine Languages. Context of this Lecture. High-Level Languages. Assembly Language: Part 1. Princeton University.

Transcription:

ECE 498 Linux Assembly Language Lecture 3 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 20 November 2012

Statically Linked C Hello-World Disassembly of section.text : 08048320 < main >: 8048320: 55 push % ebp 8048321: 89 e5 mov % esp,% ebp 8048323: 83 e4 f0 and $0xfffffff0,% esp 8048326: 83 ec 10 sub $0x10,% esp 8048329: c7 04 24 b0 84 04 08 movl $0x80484b0,(% esp ) 8048330: e8 bb ff ff ff call 80482 f0 < puts@plt > 8048335: b8 05 00 00 00 mov $0x5,% eax 804833 a: c9 leave 804833 b: c3 ret 1

Compile Hello World with C Compiler run objdump --disassemble-all./hello world and search all the sections: bss, data, rodata look for <main> and see what it does look for <puts> Why was printf() converted into puts()? Note the setting up of the arguments for the function on the stack. 2

Statically Linked Hello World run objdump --disassemble-all./hello world.static and repeat This time the code for puts is included 3

Use C compiler to create assembly gcc -m32 -O2 -S hello world.c Look at generated hello world.s 4

Compiler-generated assembly.lc0 : main :.LFB0 :.file " hello_world.c ".section.rodata.string " Hello World!".text.globl main.type main, @function.cfi_startproc pushq % rbp movq %rsp, % rbp subq $16, % rsp movl %edi, -4(% rbp ) movq %rsi, -16(% rbp ) movl $.LC0, % edi call puts movl $5, % eax leave ret.cfi_endproc 5

x86 Addressing Modes register : mov %eax, %ebx immediate : mov $5, %eax direct : mov 0xdeadbeef,%eax register indirect: mov (%ebx),%eax base scaled index w displacement: mov 0xdeadbeef(%eax,%ebx,4),%ecx gets value from 0xdeadbeef+(%eax+(%ebx*4)) 6

IP relative (64-bit only): mov 0x8(%rip), %eax Useful for position independent code and keeping local variables nearby. 7

AMD64-bit extensions Registers now 64 bit (EAX RAX, EBX RBX, etc). 8 new general purpose registers, R8 - R15 Can access low 32, 16, and 8-bits: R8D, R8W, R8L Instructions for accessing new 8 registers are encoded with extra REX prefix. Can no longer access high-bytes (AH, BH, CH, DL) if using a REX-prefixed (new) instruction but can now access the low bytes of RSI, RDI, RBP and RSP (SIL, 8

DIL, BPL, SPL) Some instructions dropped (aaa, single-byte inc/dec) 8 additional XMM registers 32-bit loads zero-extend into 64-bit (8 and 16 bit loads ignore top bits) RIP addressing relative to RIP 9

64-bit System Calls System Call numbers are all different. Done for performance. Newer linux architectures use common generic syscall numbers. System call number in %rax Arguments in %rdi %rsi %rdx %r10 %r8 %r9 %r11 and %rcx are destroyed across syscall Return value in %rax 10

syscall instruction used int $0x80 can still be used to enter 32-bit syscalls 11

Size of int/long/pointer 32-bit Linux - ILP32 (integer/long/pointer all 32-bit) 64-bit Linux - LP64 (long and pointer 64) 64-bit Window IL32/P64 (only pointer 64-bit) new Linux x32 : ILP32 but can use 64-bit instructions 12

String Instructions b/w/l/q postfix (specify size) [note intel Manual uses b/w/d/q] auto increment (decrement if D (direction) flag set) after instruction cmps compare (%edi) with (%esi), increment lods load value from (%esi) into %eax, increment ins/outs input byte from i/o into %eax, increment 13

movs move (%edi) to (%esi), incrememnt scas scan (%edi) for %eax, increment stos store %eax to (%edi), increment rep/repe/repz/repne/repnz prefixes: repeat instruction ECX times 14

LEA Instruction lea load effective address Computes the address calculation and stores calculated address into register what does lea (%ebx,%ebx,4),%ebx do? quick way to multiply %ebx by 5 (much faster than using %mul or discrete shift and add instructions) 15

BCD Instructions aaa, aad, aam, aas, daa, das Adjust BCD results when doing Binary-Coded-Decimal arithmatic 16

MOV instruction mov move a value to or from a register movzx move with zero extend xchg exchange two registers. 17

Stack Instructions pop, push push or pop a register, constant, or memory location onto the stack, then decrement the stack by the appropriate amount pusha/popa (push/pop all) pushf/popf (push/pop flags) 18

ALU Instructions add, adc add, add with carry sub, sbb subtract, subtract with borrow dec, inc decrement/increment div, idiv divide AX or DX:AX with resulting Quotient in AL and Remainder in AH (or Quotient in AX and Remainder in DX) idiv is signed divide, div unsigned 19

mul unsigned multiply. multiply by AX or DX:AX and put result in DX:AX imul signed multiply. Can be like mul, or can also multiply two arbitrary registers, or even a register by a constant and store in a third. cmp compare (subtract, but sets flags only, no result stored) neg negate (2s complement) nop same as xchg %eax, %eax. 20

Why does this have to be special cased on 64-bit? There are also fancier nops of various sizes. cbw/cwde/cdwq sign extend %eax cwd/cdq/cqo sign extend %eax into %edx also a quick way to clear %edx 21

Bit Instructions and bitwise and bsf, bsr bit scan forward or reverse test bit test ( bitwise and, set flags, don t save result) bt/btc/btr/bts bit test with complement/reset/set bit not bitwise not 22

or bitwise or xor bitwise xor. Fast way to clear a register is to xor with self rcl/rcr/rol/ror rotate left/right, through carry sal/sar/shl/shr shift left/right arithmatic/logical shld, shrd doubler precision shift 23

Control Flow call/ret call by pushing next address on stack, jumping, return call *%ebx call to address in register enter / leave create stack frame Jcc conditional jumps based on flags ja, jna (above / not above) jae, jnae (above equal) 24

jb, jnb (below) jbe, jnbe (below equal) jc, jnc (carry) jcxz (cx == 0) je, jne (equal) jg, jng (greater) jge, jnge (greater equal) jl, jnl (less) jle, jnle (less or equal) jo, jno (overflow) js, jns (sign) 25

jpe, jpo (parity) jz, jnz (zero) jmp unconditional jump loop/loope/loopne decrement CX, loop if not 0 (with loope/loopne also check zero flag) 26

Conditional Moves/Sets CMOVcc (all of the postfixes of jmps) conditional move lets you do an if (CONDITION) x=y; construct without needing any jump instructions, which hurt performance i.e. cmovc = move if carry set SETcc set byte on condition code 27

Flags lahf / sahf load flags into or out of %ah clc, cld, cmc, stc, std clear, complement or set the various flags 28

Other Misc bound check arrary bounds bswap byte swap (switch endian) int software interrupt. Also single-step for debug cmpxchg compare and exchange, useful for locks cpuid get CPU info rdmsr/rdtsc/rdpmc read model specific reg, 29

timestamp, perf counter xadd xchange and add, useful for locks. LOCK prefix Can use xlate do a table lookup 30

Summary The proceeding was just a summary of integer x86 instructions. There are numerous x86 floating point, SSE, MMX, 3Dnow! and AVX vector instructions, and others such as specific crypto instructions. 31