Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.

Slides:



Advertisements
Similar presentations
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Advertisements

COMP 2003: Assembly Language and Digital Logic
Instruction Set Architecture Classification According to the type of internal storage in a processor the basic types are Stack Accumulator General Purpose.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Accessing parameters from the stack and calling functions.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
X86_64 programming Tutorial #1 CPSC 261. X86_64 An extension of the IA32 (often called x86 – originated in the Intel 8086 processor) instruction set to.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
Instruction Set Architecture
Overview of Instruction Set Architectures
Credits and Disclaimers
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Morgan Kaufmann Publishers
Microprocessor and Assembly Language
A Closer Look at Instruction Set Architectures
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Computer Organization and Assembly Language (COAL)
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Assembly Language for Intel-Based Computers, 4th Edition
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Discussion Section – 11/3/2012
Chapter 4: Instructions
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Data Transfers, Addressing, and Arithmetic
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah –
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Micro Controllers & Embedded System Design
Instruction encoding The ISA defines Format = Encoding
Instruction encoding The ISA defines Format = Encoding
EECE.3170 Microprocessor Systems Design I
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Instruction encoding The ISA defines Format = Encoding
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Computer Organization and Assembly Language
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail

All materials is licensed under a Creative Commons “Share Alike” license. http://creativecommons.org/licenses/by-sa/3.0/ Attribution condition: You must indicate that derivative work "Is derived from Xeno Kovah's ‘Intro x86-64’ class, available at http://OpenSecurityTraining.info/IntroX86-64.html" Attribution condition: You must indicate that derivative work "Is derived from Xeno Kovah's 'Intro x86-64’ class, available at http://OpenSecurityTraining.info/IntroX86-64.html”

SingleLocalVariable.c Adding a single local variable func: 0000000140001000 sub rsp,18h 0000000140001004 mov dword ptr [rsp],0BEEFh 000000014000100B mov eax,dword ptr [rsp] 000000014000100E add rsp,18h 0000000140001012 ret main: 0000000140001020 sub rsp,28h 0000000140001024 call func (0140001000h) 0000000140001029 add rsp,28h 000000014000102D ret //SingleLocalVariable.c: int func(){ int i = 0xbeef; return i; } int main(){ return func();

Don’t forget to change the next project to the StartUp project as you’re moving between labs!

Based on the asm, we can infer the stack looks like this at line 000000014000100B in func() 00000000`0012FEB8 return address = 00000001`40001029 … undef 00000000`0012FE78 00000000`0012FE70 undef`0000BEEF Because the asm only wrote a “dword ptr” (4 bytes) worth of memory at this location, so the top 4 bytes are undefined 0x18 bytes RSP

SingleLocalVariable.c takeaways Local variables lead to an allocation of space on the stack, within the function where the variable is scoped to In VS (when optimization is turned off), there is an over-allocation of space for local variables 0x18 reserved for only 0x4 (int) worth of data func: sub rsp,18h mov dword ptr [rsp],0BEEFh mov eax,dword ptr [rsp] add rsp,18h ret main: sub rsp,28h call func (0140001000h) add rsp,28h //SingleLocalVariable.c: int func(){ int i = 0xbeef; return i; } int main(){ return func();

Adding and accessing an array local variable ArrayLocalVariable.c Adding and accessing an array local variable main: 0000000140001000 sub rsp,28h 0000000140001004 mov dword ptr [rsp],100Dh 000000014000100B mov qword ptr [rsp+8],0D00Dh 0000000140001014 mov eax,2 0000000140001019 imul rax,rax,1 000000014000101D movzx ecx,word ptr [rsp] 0000000140001021 mov word ptr [rsp+rax+10h],cx 0000000140001026 mov eax,2 000000014000102B imul rax,rax,1 000000014000102F movsx eax,word ptr [rsp+rax+10h] 0000000140001034 movsx ecx,word ptr [rsp+8] 0000000140001039 add eax,ecx 000000014000103B mov ecx,2 0000000140001040 imul rcx,rcx,4 0000000140001044 mov word ptr [rsp+rcx+10h],ax 0000000140001049 mov eax,2 000000014000104E imul rax,rax,4 0000000140001052 movsx eax,word ptr [rsp+rax+10h] 0000000140001057 add rsp,28h 000000014000105B ret //ArrayLocalVariable.c: short main(){ int a; short b[6]; long long c; a = 0x100d; c = 0xd00d; b[1] = (short)a; b[4] = b[1] + (short)c; return b[4]; }

9 IMUL - Signed Multiply FYI, Visual Studio seems to have a predilection for imul over mul (unsigned multiply). You’ll see it showing up in places you expect mul. I haven’t been able to get it to generate the latter for simple examples. Three forms. One, two, or three operands imul r/mX edx:eax = eax * r/mX imul reg, r/mX reg = reg * r/mX imul reg, r/mX, immediate reg = r/mX * immediate Three operands? Possibly the only “basic” instruction (meaning non-added-on-instruction-set(MMX,AVX,AEX,VMX,etc)) of it’s kind? (see link in notes) http://www.microsoft.com/msj/0698/hood0698.aspx - “There's even a form of the IMUL instruction that takes three operands. To my knowledge, this is the only instruction in the Intel opcode set with this distinction.” I found that quote while trying to find a way to make visual studio naturally emit a MUL instruction. Also, while things containing an r/mX can encode a mnemonic which looks like is has more operands, the information is still contained in a the normal combo of one or two bytes Book p. 218

IMUL - examples initial result Book p. 218 operation imul ecx edx eax r/mX(ecx) 0x0 0x44000000 0x4 rax r/mX(rcx) 0x20 0x4 rax r/mX(rcx) 0x20 0x4 initial operation imul ecx imul rax, rcx imul rax, rcx, 0x6 edx eax r/mX(ecx) 0x1 0x10000000 0x4 rax r/mX(rcx) 0x80 0x4 rax r/mX(rcx) 0x18 0x4 result Book p. 218

MOVZX - Move with zero extend MOVSX - Move with sign extend 10 MOVZX - Move with zero extend MOVSX - Move with sign extend 11 Used to move small values (from smaller types) into larger registers (holding larger types) Support same r->r, r->m, m->r, i->m, i->r forms as normal MOV “Zero extend” means the CPU unconditionally fills the high order bits of the larger register with zeros “Sign extend” means the CPU fills the high order bits of the destination larger register with whatever the sign bit is set to on the small value Book p. 169-172

MOVZX/MOVSX - examples mov eax, 0xF00DFACE movzx rbx, eax now rbx = 0x00000000F00DFACE movsx rbx, eax now rbx = 0xFFFFFFFFF00DFACE, because the sign bit (most significant bit) of 0xF00DFACE is 1

Based on the asm, we can infer the stack looks like this at line 0000000140001049 of main(): 00000000`0012FEB8 return address = 000000014000131d … undef 00000000`0012FEAC 00000000`0012FEAA short b[5] = undef 00000000`0012FEA8 short b[4] = maths! 00000000`0012FEA6 short b[3] = undef 00000000`0012FEA4 short b[2] = undef 00000000`0012FEA2 short b[1] = maths! 00000000`0012FEA0 short b[0] = undef 00000000`0012FE98 long long c = 0xd00d 00000000`0012FE90 int a = 0x100d 0x28 bytes I’m cheating here and not using only 0x8-sized entries, for clarity of array indices’ address I’m cheating here and not using only 0x8-sized entries, for clarity I’m cheating here and not using only 0x8-sized entries, for clarity Note how there’s wasted space by storing a 4 byte value (“int a”) in an 8 byte space RSP

ArrayLocalVariable.c takeaways Local variables need not be stored on the stack in the same order they are defined in the high level language (In VS unoptimized code) Array access is typically done by multiplying the size of the array element (2 bytes for a short in this case), times the index that is desired to be access (indices 1 and 4 in this case) Moving a small value to a large register will result in a zero extend. Addition using signed values could result in a sign extend, if the arithmetic is done in a larger register main: sub rsp,28h mov dword ptr [rsp],100Dh mov qword ptr [rsp+8],0D00Dh mov eax,2 imul rax,rax,1 movzx ecx,word ptr [rsp] mov word ptr [rsp+rax+10h],cx movsx eax,word ptr [rsp+rax+10h] movsx ecx,word ptr [rsp+8] add eax,ecx mov ecx,2 imul rcx,rcx,4 mov word ptr [rsp+rcx+10h],ax imul rax,rax,4 add rsp,28h ret //ArrayLocalVariable.c: short main(){ int a; short b[6]; long long c; a = 0x100d; c = 0xd00d; b[1] = (short)a; b[4] = b[1] + (short)c; return b[4]; }

StructLocalVariable.c Accessing a struct local variable main: 0000000140001000 sub rsp,28h 0000000140001004 mov dword ptr [rsp],100Dh 000000014000100B mov qword ptr [rsp+10h],0D00Dh 0000000140001014 mov eax,2 0000000140001019 imul rax,rax,1 000000014000101D movzx ecx,word ptr [rsp] 0000000140001021 mov word ptr [rsp+rax+4],cx 0000000140001026 mov eax,2 000000014000102B imul rax,rax,1 000000014000102F movsx rax,word ptr [rsp+rax+4] 0000000140001035 add rax,qword ptr [rsp+10h] 000000014000103A mov ecx,2 000000014000103F imul rcx,rcx,4 0000000140001043 mov word ptr [rsp+rcx+4],ax 0000000140001048 mov eax,2 000000014000104D imul rax,rax,4 0000000140001051 movzx eax,word ptr [rsp+rax+4] 0000000140001056 add rsp,28h 000000014000105A ret //StructLocalVariable.c: typedef struct mystruct{ int a; short b[6]; long long c; } mystruct_t; short main(){ mystruct_t foo; foo.a = 0x100d; foo.c = 0xd00d; foo.b[1] = foo.a; foo.b[4] = foo.b[1] + foo.c; return foo.b[4]; }

Based on the asm, we can infer the stack looks like this: 00000000`0012FEB8 return address = 000000014000131d ... undef 00000000`0012FEA8 00000000`0012FEA0 long long c = 0xd00d 00000000`0012FE9E short b[5] = undef 00000000`0012FE9C short b[4] = undef 00000000`0012FE9A short b[3] = undef 00000000`0012FE98 short b[2] = undef 00000000`0012FE96 short b[1] = maths! 00000000`0012FE94 short b[0] = undef 00000000`0012FE90 int a = 0x100d 0x28 bytes Note how there’s no wasted space this time since the “int a” 4 byte value is next to the 6 short 2 byte values, which all added up just happen to be 4 shy of 16 bytes total. Hence why c is accessed with “[rsp+10h]” I’m cheating here and not using only 0x8-sized entries, for clarity of array indices’ address I’m cheating here and not using only 0x8-sized entries, for clarity I’m cheating here and not using only 0x8-sized entries, for clarity RSP

StructLocalVariable.c Fields in a struct must be stored in the same order they are defined in the high level language. And they will appear with the first field at the lowest address, and all subsequent fields higher. //StructLocalVariable.c: typedef struct mystruct{ int a; short b[6]; long long c; } mystruct_t; short main(){ mystruct_t foo; foo.a = 0x100d; foo.c = 0xd00d; foo.b[1] = foo.a; foo.b[4] = foo.b[1] + foo.c; return foo.b[4]; } main: sub rsp,28h mov dword ptr [rsp],100Dh mov qword ptr [rsp+10h],0D00Dh mov eax,2 imul rax,rax,1 movzx ecx,word ptr [rsp] mov word ptr [rsp+rax+4],cx movsx rax,word ptr [rsp+rax+4] add rax,qword ptr [rsp+10h] mov ecx,2 imul rcx,rcx,4 mov word ptr [rsp+rcx+4],ax imul rax,rax,4 movzx eax,word ptr [rsp+rax+4] add rsp,28h ret

Instructions we now know (11) NOP PUSH/POP CALL/RET MOV ADD/SUB IMUL MOVZX/MOVSX