Intel X86 Assembly Language & Microarchitecture

3y ago
57 Views
8 Downloads
1.31 MB
91 Pages
Last View : 19d ago
Last Download : 3m ago
Upload by : Luis Wallis
Transcription

Intel x86 AssemblyLanguage &Microarchitecture#x86

Table of ContentsAbout1Chapter 1: Getting started with Intel x86 Assembly Language & Microarchitecture2Remarks2Examples2x86 Assembly Language2x86 Linux Hello World Example3Chapter 2: AssemblersExamples66Microsoft Assembler - MASM6Intel Assembler6AT&T assembler - as7Borland's Turbo Assembler - TASM7GNU assembler - gas7Netwide Assembler - NASM8Yet Another Assembler - YASM9Chapter 3: Calling ConventionsRemarks1010Resources10Examples1032-bit cdecl10Parameters10Return Value11Saved and Clobbered Registers1164-bit System V11Parameters11Return Value11Saved and Clobbered Registers1132-bit stdcallParameters1212

Return Value12Saved and Clobbered Registers1232-bit, cdecl — Dealing with Integers12As parameters (8, 16, 32 bits)12As parameters (64 bits)12As return value1332-bit, cdecl — Dealing with Floating Point14As parameters (float, double)14As parameters (long double)14As return value1564-bit Windows15Parameters15Return Value16Saved and Clobbered Registers16Stack alignment1632-bit, cdecl — Dealing with Structs16Padding16As parameters (pass by reference)17As parameters (pass by value)17As return value17Chapter 4: Control Flow19Examples19Unconditional jumps19Relative near jumps19Absolute indirect near jumps19Absolute far jumps19Absolute indirect far jumps20Missing jumps20Testing conditionsFlags2021

Non-destructive tests21Signed and unsigned tests22Conditional jumps22Synonyms and terminology22Equality22Greater than23Less than24Specific flags24One more conditional jump (extra one)25Test arithmetic relations25Unsigned integers25Signed integers26a label26Synonyms27Signed unsigned companion codes27Chapter 5: Converting decimal strings to integers28Remarks28Examples28IA-32 assembly, GAS, cdecl calling convention28MS-DOS, TASM/MASM function to read a 16-bit unsigned integer29Read a 16-bit unsigned integer from input.29Return values30Usage30Code30NASM porting32MS-DOS, TASM/MASM function to print a 16-bit number in binary, quaternary, octal, hex32Print a number in binary, quaternary, octal, hexadecimal and a general power of two32Parameters33Usage33Code34Data35

NASM porting35Extending the function35MS-DOS, TASM/MASM, function to print a 16-bit number in decimal36Print a 16-bit unsigned number in decimal36Parameters36Usage36Code37NASM porting38Chapter 6: Data Manipulation39Syntax39Remarks39Examples39Using MOV to manipulate valuesChapter 7: Multiprocessor management3941Parameters41Remarks41Examples43Wake up all the processorsChapter 8: eroing a register50Moving Carry flag into a register50Background50Use 'sbb'51Pros51Cons51Test a register for 051Background51Use test51

Pros52Cons52Linux system calls with less bloat52Multiply by 3 or 553Background53Use lea53Pros53Cons53Chapter 9: Paging - Virtual Addressing and MemoryExamplesIntroductionHistory54545454The first computers54Multi-user, ns54Segmentation55Problems55Paging55Virtual addressing55Hardware and OS support55Paging features55Multiprocessing56Sparse Data56Virtual Memory56Paging decisions57How big should a Page be?57How to optimise the usage of the Page Tables?5780386 Paging58High Level Design58

Page Entry59Page Directory Base Register (PDBR)59Page Faults5980486 Paging60Pentium Paging60Address layout60Directory Entry layout61Physical Address Extension (PAE)61Introduction61More RAM61Design61Page Size Extension (PSE)62PSE-32 (and PSE-40)Chapter 10: Real vs Protected modesExamples626464Real Mode64Protected Mode65Introduction65Design65Segment Register65Global / Local65Descriptor Table65Descriptor66True protection at last!66Errors66Switching into Protected Mode67Unreal mode68Chapter 11: Register FundamentalsExamples16-bit Registers717171

Notes:7132-bit registers728-bit Registers72Segment Registers73Segmentation73Original Segment Registers73Segment Size?73More Segment Registers!7464-bit registers74Flags register75Condition Codes75Accessing FLAGS directly76Other Flags7680286 Flags7780386 Flags7780486 Flags77Pentium Flags78Chapter 12: System Call MechanismsExamplesBIOS calls797979How to interact with the BIOS79Using BIOS calls with function select79Examples79How to write a character to the display:79How to read a character from the keyboard (blocking):79How to read one or more sectors from an external drive (using CHS addressing):80How to read the system RTC (Real Time Clock):80How to read the system time from the RTC:80How to read the system date from the RTC:81How to get size of contiguous low memory:81How to reboot the computer:81

Error handling81References81Credits82

AboutYou can share this PDF with anyone you feel could benefit from it, downloaded the latest versionfrom: intel-x86-assembly-language---microarchitectureIt is an unofficial and free Intel x86 Assembly Language & Microarchitecture ebook created foreducational purposes. All the content is extracted from Stack Overflow Documentation, which iswritten by many hardworking individuals at Stack Overflow. It is neither affiliated with StackOverflow nor official Intel x86 Assembly Language & Microarchitecture.The content is released under Creative Commons BY-SA, and the list of contributors to eachchapter are provided in the credits section at the end of this book. Images may be copyright oftheir respective owners unless otherwise specified. All trademarks and registered trademarks arethe property of their respective company owners.Use the content presented in this book at your own risk; it is not guaranteed to be correct noraccurate, please send your feedback and corrections to info@zzzprojects.comhttps://riptutorial.com/1

Chapter 1: Getting started with Intel x86Assembly Language & MicroarchitectureRemarksThis section provides an overview of what x86 is, and why a developer might want to use it.It should also mention any large subjects within x86, and link out to the related topics. Since theDocumentation for x86 is new, you may need to create initial versions of those related topics.Examplesx86 Assembly LanguageThe family of x86 assembly languages represents decades of advances on the original Intel 8086architecture. In addition to there being several different dialects based on the assembler used,additional processor instructions, registers and other features have been added over the yearswhile still remaining backwards compatible to the 16-bit assembly used in the 1980s.The first step to working with x86 assembly is to determine what the goal is. If you are seeking towrite code within an operating system, for example, you will want to additionally determinewhether you will choose to use a stand-alone assembler or built-in inline assembly features of ahigher level language such as C. If you wish to code down on the "bare metal" without anoperating system, you simply need to install the assembler of your choice and understand how tocreate binary code that can be turned into flash memory, bootable image or otherwise be loadedinto memory at the appropriate location to begin execution.A very popular assembler that is well supported on a number of platforms is NASM (NetwideAssembler), which can be obtained from http://nasm.us/. On the NASM site you can proceed todownload the latest release build for your platform.WindowsBoth 32-bit and 64-bit versions of NASM are available for Windows. NASM comes with aconvenient installer that can be used on your Windows host to install the assembler automatically.LinuxIt may well be that NASM is already installed on your version of Linux. To check, execute:nasm -vIf the command is not found, you will need to perform an install. Unless you are doing somethingthat requires bleeding edge NASM features, the best path is to use your built-in packagemanagement tool for your Linux distribution to install NASM. For example, under Debian-derivedhttps://riptutorial.com/2

systems such as Ubuntu and others, execute the following from a command prompt:sudo apt-get install nasmFor RPM based systems, you might try:sudo yum install nasmMac OS XRecent versions of OS X (including Yosemite and El Capitan) come with an older version of NASMpre-installed. For example, El Capitan has version 0.98.40 installed. While this will likely work foralmost all normal purposes, it is actually quite old. At this writing, NASM version 2.11 is releasedand 2.12 has a number of release candidates available.You can obtain the NASM source code from the above link, but unless you have a specific need toinstall from source, it is far simpler to download the binary package from the OS X releasedirectory and unzip it.Once unzipped, it is strongly recommended that you not overwrite the system-installed version ofNASM. Instead, you might install it into /usr/local: sudo su user's password entered to become root # cd /usr/local/bin# cp path/to/unzipped/nasm/files/nasm ./# exitAt this point, NASM is in /usr/local/bin, but it is not in your path. You should now add thefollowing line to the end of your profile: echo 'export PATH /usr/local/bin: PATH' /.bash profileThis will prepend /usr/local/bin to your path. Executing nasmnow display the proper, newer, version.-vat the command prompt shouldx86 Linux Hello World ExampleThis is a basic Hello World program in NASM assembly for 32-bit x86 Linux, using system callsdirectly (without any libc function calls). It's a lot to take in, but over time it will becomeunderstandable. Lines starting with a semicolon(;) are comments.If you don't already know low-level Unix systems programming, you might want to just writefunctions in asm and call them from C or C programs. Then you can just worry about learninghow to handle registers and memory, without also learning the POSIX system-call API and the ABIfor using it.This makes two system calls: write(2) and exit(2) (not the exit(3) libc wrapper that flushes stdiohttps://riptutorial.com/3

buffers and so on). (Technically, exit() calls sys exit group, not sys exit, but that only matters ina multi-threaded process.) See also syscalls(2) for documentation about system calls in general,and the difference between making them directly vs. using the libc wrapper functions.In summary, system calls are made by placing the args in the appropriate registers, and thesystem call number in eax, then running an int 0x80 instruction. See also What are the returnvalues of system calls in Assembly? for more explanation of how the asm syscall interface isdocumented with mostly C syntax.The syscall call numbers for the 32-bit ABI are in /usr/include/i386-linux-gnu/asm/unistd 32.h(same contents in /usr/include/x86 64-linux-gnu/asm/unistd 32.h).will ultimately include the right file, so you could run echo '#include sys/syscall.h ' gcc -E - -dM less to see the macro defs (see this answer for more aboutfinding constants for asm in C headers)#include sys/syscall.h section .text; Executable code goes in the .text sectionglobal start; The linker looks for this symbol to set the process entry point,so execution start here;;;a name followed by a colon defines a symbol. The global start directive modifies it soit's a global symbol, not just one that we can CALL or JMP to from inside the asm.;;; note that start isn't really a "function". You can't return from it, and the kernelpasses argc, argv, and env differently than main() would expect.start:;;; write(1, msg, len);; Start by moving the arguments into registers, where the kernel will look for themmovedx,len; 3rd arg goes in edx: buffer lengthmovecx,msg; 2nd arg goes in ecx: pointer to the buffer;Set output to stdout (goes to your terminal, or wherever you redirect or pipe)movebx,1; 1st arg goes in ebx: Unix file descriptor. 1 stdout, which isnormally connected to the terminal.moveax,4; system call number (from SYS write / NR write from unistd 32.h).int0x80; generate an interrupt, activating the kernel's system-callhandling code. 64-bit code uses a different instruction, different registers, and differentcall numbers.;; eax return value, all other registers unchanged.;;;Second, exit the process. There's nothing to return to, so we can't use a retinstruction (like we could if this was main() or any function with a caller);;; If we don't exit, execution continues into whatever bytes are next in the memory page,;;; typically leading to a segmentation fault because the padding 00 00 decodes to add[eax],al.;;; exit(0);xorebx,ebx; first arg exit status 0. (will be truncated to 8 bits).Zeroing registers is a special case on x86, and mov ebx,0 would be less efficient.;; leaving out the zeroing of ebx would mean we exit(1), i.e. with anerror status, since ebx still holds 1 from earlier.moveax,1; put NR exit into eaxint0x80;Execute the Linux functionsection.rodata; Section for read-only constants;; msg is a label, and in this context doesn't need to be msg:.separate line.https://riptutorial.com/It could be on a4

msgdb;; db Data Bytes: assemble some literal bytes into the output file.'Hello, world!',0xa; ASCII string constant plus a newline (0x10);; No terminating zero byte is needed, because we're using write(), which takesa buffer length instead of an implicit-length string.;; To make this a C string that we could pass to puts or strlen, we'd need aterminating 0 byte. (e.g. ".", 0x10, 0)lenequ - msg; Define an assemble-time constant (not stored by itself in theoutput file, but will appear as an immediate operand in insns that use it); Calculate len string length. subtract the address of the start; of the string from the current position ( );; equivalently, we could have put a str end: label after the string and donelen equstr end - strOn Linux, you can save this file as Hello.asm and build a 32-bit executable from it with thesecommands:nasm -felf32 Hello.asmFdwarf for debug symbols and warningsgcc -nostdlib -m32 Hello.o -o Hellostatic binary# assemble as 32-bit code.Add -Worphan-labels -g -# link without CRT startup code or libc, making aSee this answer for more details on building assembly into 32 or 64-bit static or dynamically linkedLinux executables, for NASM/YASM syntax or GNU AT&T syntax with GNU as directives. (Keypoint: make sure to use -m32 or equivalent when building 32-bit code on a 64-bit host, or you willhave confusing problems at run-time.)You can trace it's execution with strace to see the system calls it makes: strace ./Helloexecve("./Hello", ["./Hello"], [/* 72 vars */]) 0[ Process PID 4019 runs in 32 bit mode. ]write(1, "Hello, world!\n", 14Hello, world!) 14exit(0) ? exited with 0 The trace on stderr and the regular output on stdout are both going to the terminal here, so theyinterfere in the line with the write system call. Redirect or trace to a file if you care. Notice how thislets us easily see the syscall return values without having to add code to print them, and is actuallyeven easier than using a regular debugger (like gdb) for this.The x86-64 version of this program would be extremely similar, passing the same args to thesame system calls, just in different registers. And using the syscall instruction instead of int 0x80.Read Getting started with Intel x86 Assembly Language & Microarchitecture oarchitecturehttps://riptutorial.com/5

Chapter 2: AssemblersExamplesMicrosoft Assembler - MASMGiven that the 8086/8088 was used in the IBM PC, and the Operating System on that was mostoften from Microsoft, Microsoft's assembler MASM was the de facto standard for many years. Itfollowed Intel's syntax closely, but permitted some convenient but "loose" syntax that (in hindsight)only caused confusion and errors in code.A perfect example is as follows:MaxSizeSymbolEQUDW160x1234; Define a constant; Define a 16-bit WORD called Symbol to hold 0x1234MOVMOVMOVAX, 10; AX now holds 10BX, MaxSize ; BX now holds 16CX, Symbol ; ?Does the last MOV instruction put the contents of Symbol into CX, or the address of Symbol into CX?Does CX end up with 0x1234 or 0x0102 (or whatever)? It turns out that CX ends up with 0x1234 - if youwant the address, you need to use the OFFSET specifierMOVMOVAX, [Symbol]; Contents of SymbolCX, OFFSET Symbol ; Address of SymbolIntel AssemblerIntel wrote the specification of the 8086 assembly language, a derivative of the earlier 8080, 8008and 4004 processors. As such, the assembler they wrote followed their own syntax precisely.However, this assembler wasn't used very widely.Intel defined their opcodes to have either zero, one or two operands. The two-operand instructionswere defined to be in the dest, source order, which was different from other assemblers at the time.But some instructions used implicit registers as operands - you just had to know what they were.Intel also used the concept of "prefix" opcodes - one opcode would affect the next instruction.; Zero operand examplesNOP; No parametersCBW; Convert byte in AL into word in AXMOVSB; Move byte pointed to by DS:SI to byte pointed to by ES:DI; SI and DI are incremented or decremented according to D bit; Prefix examplesREPMOVSB; Move number of bytes in CX from DS:SI to ES:DI; SI and DI are incremented or decremented according to D bit; One operand exampleshttps://riptutorial.com/6

NOTMULAXCX; Replace AX with its one's complement; Multiply AX by CX and put 32-bit result in DX:AX; Two operand examplesMOVAL, [0x1234] ; Copy the contents of memory location DS:0x1234 into AL registerIntel also broke a convention used by other assemblers: for each opcode, a different mnemonicwas invented. This required subtly- or distinctly-different names for similar operations: e.g. LDM for"Load from Memory" and LDI for "Load Immediate". Intel used the one mnemonic MOV - andexpected the assembler to work out which opcode to use from context. That caused many pitfallsand errors for programmers in the future when the assembler couldn't intuit what the programmeractually wanted.AT&T assembler - asAlthough the 8086 was most used in IBM PCs along with Microsoft, there were a number of othercomputers and Operating Systems that used it too: most notably Unix. That was a product ofAT&T, and it already had Unix running on a number of other architectures. Those architecturesused more conventional assembly syntax - especially that two-operand instructions specified themin source, dest order.So AT&T assembler conventions overrode the conventions dictated by Intel, and a whole newdialect was introduced for the x86 range: Register names were prefixed by %:%al, %bx etc. Immediate values were prefied by : 4 Operands were in source, dest order Opcodes included their operand sizes:movw 4, %ax ; Move word 4 into AXBorland's Turbo Assembler - TASMBorland started out with a Pascal compiler that they called "Turbo Pascal". This was followed bycompilers for other languages: C/C , Prolog and Fortran. They also produced an assemblercalled "Turbo Assembler", which, following Microsoft's naming convention, they called "TASM".TASM tried to fix some of the problems of writing code using MASM (see above), by providing amore strict interpretation of the source code under a specified IDEAL mode. By default it assumedMASM mode, so it could assemble MASM source directly - but then Borland found that they had tobe bug-for-bug compatible with MASM's more "quirky" idiosyncracies - so they also added a QUIRKSmode.Since TASM was (much) cheaper than MASM, it had a large user base - but not many peopleused IDEAL mode, despite its touted advantages.GNU assembler - gashttps://riptutorial.com/7

When the GNU project needed an assembler for the x86 family, they went with the AT&T version(and its syntax) that was associated with Unix rather than the Intel/Microsoft version.Netwide Assembler - NASMNASM is by far the most ported assembler for the x86 architecture - it's available for practicallyevery Operating System based on the x86 (even being includ

Chapter 1: Getting started with Intel x86 Assembly Language & Microarchitecture 2 Remarks 2 Examples 2 x86 Assembly Language 2 x86 Linux Hello World Example 3 Chapter 2: Assemblers 6 Examples 6 Microsoft Assembler - MASM 6 Intel Assembler 6 AT&T assembler - as 7 Borland's Turbo Assembler - TASM 7 GNU assembler - gas 7 Netwide Assembler - NASM 8

Related Documents:

Netburst microarchitecture Intel announces plans to discontinue the Pentium 4 line and switch over to the Core microarchitecture 2007 - Intel finishes switching to the Core series microarchitecture which removed Hyper Threading 2008 - Intel releases the i7 based on the Nehalem microarchitecture which reintroduces Hyper Threading

Intel x86 Assembly Fundamentals Comppgz ygguter Organization and Assembly Languages Yung-Yu Chuang with slides by Kip Irvine . x86 Assembly Languagex86 Assembly Language F

Intel C Compiler Intel Fortran Compiler Intel Distribution for Python* Intel Math Kernel Library Intel Integrated Performance Primitives Intel Threading Building Blocks Intel Data Analytics Acceleration Library Included in Composer Edition SCALE Intel MPI Library Intel Trace Analyze

x86 Assembly Language Introduction. x86 Assembly Why Learn Assembly? Assembly is the most primitive tool in the programmers toolbox. Entire software projects can be written without ever once looking at a single line of assembly code. So the question arises: why learn assembly?

android-x86.org Android-x86 status update from lead developer Chih-Wei Huang . Virtualbox and VMware Player supported. 26-28 Sept. - A Coruña android-x86.org oreo-x86 features . marshmallow-x86 3.7 FORCE_AMDGPU cflag to fix function prototypes (maurossi)

Document Number: 337029 -009 Intel RealSenseTM Product Family D400 Series Datasheet Intel RealSense Vision Processor D4, Intel RealSense Vision Processor D4 Board, Intel RealSense Vision Processor D4 Board V2, Intel RealSense Vision Processor D4 Board V3, Intel RealSense Depth Module D400, Intel RealSense Depth Module D410, Intel

Lenovo recommends Windows 8 Pro. SPECIFICATIONS PrOCESSOr OPErATING SySTEM I/O (INPUT/OUTPUT) POrTS Mini-Tower / Small Form Factor (SFF) Intel Core i7-4770S 65W Intel Core i7-4770 84W Intel Core i5-4430S 65W Intel Core i5-4430 84W Intel Core i5-4570S 65W Intel Core i5-4570 84W Intel Core i5-4670S 65W Intel Core i5-4670 84W Intel Core i3-4330 65W

Here are a few suggested references for this course, [2,19,22]. The latter two references are downloadable if you are logging into MathSci net through your UCSD account. For a proof that all p{ variation paths have some extension to a rough path see, [21] and also see [9, Theorem 9.12 and Remark 9.13]. For other perspectives on the the theory, see [6] and also see Gubinelli [10,11] Also see .