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

Slides:



Advertisements
Similar presentations
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Advertisements

Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
What Linux does with IDE? Introduction to Pentium features for trapping reads/writes to memory-locations and i/o-ports.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
CS2422 Assembly Language & System Programming November 7, 2006.
High-Level Language Interface Chapter 17 S. Dandamudi.
Ithaca College 1 Machine-Level Programming X: inline Assembly Comp 21000: Introduction to Computer Systems & Assembly Lang On-Line resources* * See see.
6.828: PC hardware and x86 Frans Kaashoek
1 GCC Inline Assembly (asm) Example 1 : assignment System oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/spSession 14 – 26 May 2015 Ref: B. Hirsbrunner:
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Other Processors. Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Embedding Assembly Code in C Programs תרגול 7 שילוב קוד אסמבלי בקוד C.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Ithaca College 1 Machine-Level Programming XI: inline Assembly Comp 21000: Introduction to Computer Systems & Assembly Lang On-Line resources* * See see.
Instruction Set Architecture
CHAPTER NINE.
Homework Reading Lab with your assigned section starts next week
Assembly language.
Credits and Disclaimers
ECE 3430 – Intro to Microcomputer Systems
Credits and Disclaimers
Chapter 13 Inline Code.
Other Processors.
Additional Assembly Programming Concepts
Chapter 13 Inline Code.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
High-Level Language Interface
Low level Programming.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Assembler, Compiler, Interpreter
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.
Assembly Language Programming I: Introduction
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.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Lecture 30 (based on slides by R. Bodik)
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.
MIPS Procedure Calls CSE 378 – Section 3.
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.
Shift & Rotate 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 –
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.
Shift & Rotate Instructions)
Assembler, Compiler, Interpreter
Tonga Institute of Higher Education IT 141: Information Systems
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Tonga Institute of Higher Education IT 141: Information Systems
Credits and Disclaimers
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
An Introduction to the ARM CORTEX M0+ Instructions
Low level Programming.
The ‘asm’ construct An introduction to the GNU C/C++ compiler’s obscure syntax for doing inline assembly language.
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”

Inline assembly Inline assembly is a way to include assembly directly in a C/C++ file. However, the syntax will differ between compilers and assemblers. There are times when you actually have to code asm in order to do something from a C/C++ file. Very common in OS or driver design, because there are many aspects of hardware which can only be accessed with special instructions In crypto you might want access to the “rol/ror - rotate left/right” instructions which don’t have corresponding C syntax like shifts do Or maybe you just want full control over the code being generated for optimization purposes Keep in mind the compiler may still optimize your inline asm Also it’s a great way to simply experiment with instructions Though getting the syntax right for the desired instructions is sometimes annoying

GCC inline assembly Book starting on p. 365 GCC syntax - AT&T syntax asm(“instructions separated by \n”); DO need a semicolon after close parentheses int myVar = 0xdeadbeef; asm("movl -0x4(%rbp), %eax\n" "cmp $0xdeadbeef,%eax\n" "je myLabel\n" "xor %eax, %eax\n" "myLabel: movw $0xFFFF, %cx\n" "and %ecx, %eax”); http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html http://www.cs.virginia.edu/~clc5q/gcc-inline- asm.pdf 6 Book starting on p. 365

GCC inline assembly 2 Syntax using C variables (aka “extended asm”): asm ( assembler template : output operands /* optional */ : input operands /* optional */ : list of clobbered registers /* optional */ ); int myVar; //value into C variable from register asm ("movl %%eax, %0" : "=r" (myVar) ); //value into register from C variable asm ("movl %0, %%eax" : : "r" (myVar) ); 7

.byte Once you learn about opcodes later on, you can even specify exactly the instructions you want to use by using the “.byte” keyword, to place specific bytes into the code. Those bytes can then be interpreted as instructions or data This is sometimes useful if you can’t figure out the inline asm syntax for the instruction you want to use, but you know its opcodes (either from seeing them elsewhere, or by reading the manual) Examples: asm(“.byte 0x55”); is “push %rbp” asm(".byte 0x48 ; .byte 0x89 ; .byte 0xE5"); is “mov %rsp, %rbp” _emit syntax: http://msdn.microsoft.com/en- us/library/1b80826t(VS.80).aspx 8

VisualStudio assembly Visual Studio does not support inline assembly for x64 code If you must run hand-written assembly, you must write write assembly functions in a separate asm file, use an assembler to assemble it, and then link it with your C code Will not go into detail here, but instructions can be found here: http://www.codeproject.com/Articles/271627/Assembly-Programming- with-Visual-Studio 4

VisualStudio assembly 2 Certain assembly instructions can be inserted into C code by using VS compiler intrinsics These look like C functions calls, but the compiler substitutes them with literal assembly instructions Examples: __writeeflags __stosX (rep stos) __movsX (rep movs) __cpuid _rotrX (ror) Many, many more: http://msdn.microsoft.com/en-us/library/hh977022.aspx 5

Visual Studio Inline Assembly for 32-bit code Bonus Slides Visual Studio Inline Assembly for 32-bit code

VisualStudio inline assembly VisualStudio syntax - intel-syntax __asm{ instructions separated by \n}; That’s two underscores at the beginning Don’t even need a semicolon after it, but I put them there since it makes the auto-indent work correctly __asm{ mov eax, [esp+0x4] cmp eax, 0xdeadbeef je myLabel xor eax, eax myLabel: mov bl, al }; 4

VisualStudio inline assembly 2 Syntax using C variables is the same, just put the variable in place of a register name for instance. (The assembler will substitute the correct address for the variable.) http://msdn.microsoft.com/en-us/library/4ks26t93(VS.80).aspx int myVar; //value into C variable from register __asm {mov myVar, eax}; //value into register from C variable __asm {mov eax, myVar}; 5

_emit and .byte Once you learn about opcodes later on, you can even specify exactly the instructions you want to use by using the “_emit” or “.byte” keywords, to place specific bytes into the code. Those bytes can then be interpreted as instructions or data This is sometimes useful if you can’t figure out the inline asm syntax for the instruction you want to use, but you know its opcodes (either from seeing them elsewhere, or by reading the manual) Examples: __asm{_emit 0x55} is __asm{push ebp} __asm{_emit 0x89};__asm{_emit 0xE5} is __asm{mov ebp, esp} asm(“.byte 0x55”); is asm(“push %ebp”); asm(".byte 0x89 ; .byte 0xE5"); is asm(“mov %esp, %ebp”); _emit syntax: http://msdn.microsoft.com/en- us/library/1b80826t(VS.80).aspx 8