X86 architecture tutorial pdf


















This tutorial will address the age old question of why to bother understanding the basics of Assembly Language. This tutorial will address how to create our first real Assembly Program by moving immediate data. This tutorial will address how to debug our first real Assembly Program by moving immediate data.

This tutorial will address how to hack our first real Assembly Program by moving immediate data. This tutorial will address how to create our second real Assembly Program by moving between registers. This tutorial will address how to debug our second real Assembly Program by moving between registers.

This tutorial will address how to hack our second real Assembly Program by moving between registers. This tutorial will address how to create our third real Assembly Program by moving between memory and registers.

This tutorial will address how to debug our third real Assembly Program by moving between memory and registers. This tutorial will address how to hack our third real Assembly Program by moving between memory and registers. This tutorial will address how to create our fourth real Assembly Program by moving between registers and memory.

This tutorial will address how to debug our fourth real Assembly Program by moving between registers and memory. This tutorial will address how to hack our fourth real Assembly Program by moving between registers and memory. This tutorial will address how to create our fifth real Assembly Program by indirect addressing with registers. This tutorial will address how to debug our fifth real Assembly Program by indirect addressing with registers. This tutorial will address how to hack our fifth real Assembly Program by indirect addressing with registers.

This tutorial will discuss number systems such as binary, decimal and hexadecimal in detail. Apache License, Version 2. Skip to content. Star 0. Branches Tags. Could not load branches. Could not load tags. This branch is up to date with master. This branch is not ahead of the upstream master. The two massively popular architectures IA and x are so common, they are described in a single set of manuals.

The following notes briefly summarize the latter architecture only. The following diagram is taken directly from Chapter 3 in this volume:.

General Purpose Registers These are 64 bits wide and used for integer arithmetic and logic, and to hold both data and pointers to memory. The registers are called R Also: You can access the lower order bits of each register using the names R0D Backward compatibilty!

The x grew out of a bit processor family created in the s. You can access the lower order bits of each register using the names R0W You can access the lower order 8-bits of each register using the names R0B XMM Registers These are bits wide. Being able to read and write code in low-level assembly language is a powerful skill to have. It enables you to write faster code, use machine features unavailable in C, and reverse-engineer compiled code.

But starting out can be a daunting task. The official documentation manuals from Intel are well over a thousand pages long. Twenty years of continual evolution with backward compatibility have produced a landscape with clashing design principles from different eras, deprecated features occupying space, layers upon layers of mode switches, and an exception to every pattern.

In this tutorial, I will help you gain a solid understanding of the x86 ISA from basic principles. If you want to make use of this knowledge, you should simultaneously refer to another tutorial that shows you how to write and compile a simple function, and also have a list of CPU instructions open for referencing. My tutorial will start out in familiar territory and slowly add complexity in manageable steps — unlike other documentation that tend to lay out the information all at once.

You do not need to know how CPUs work internally or have prior exposure to assembly language. This is most easily done on Linux harder but possible on Windows.

Here is a sample function in assembly language:. Save it in a file called my-asm. At the very least, being able to compile code gives you a way to verify that your assembly programs are syntactically correct. The underlying concepts are still the same in both cases, but the notation is a bit different. An x86 CPU has eight bit general-purpose registers.

Other CPU architectures would simply name them r0 , r1 , Each register can hold any bit integer value. The x86 architecture actually has over a hundred registers, but we will only cover specific ones when needed. As a first approximation, a CPU executes a list of instructions sequentially, one by one, in the order listed in the source code.

Later on, we will see how the code path can go non-linearly, covering concepts like if-then, loops, and function calls. There are actually eight bit and eight 8-bit registers that are subparts of the eight bit general-purpose registers. These features come from the bit era of x86 CPUs, but still have some occasional use in bit mode.

Whenever the value of a bit or 8-bit register is modified, the upper bits belonging to the full bit register will remain unchanged. The most basic x86 arithmetic instructions operate on two bit registers.

The first operand acts as a source, and the second operand acts as both a source and destination. Many instructions fit this important schema — for example:. The bit shifting and rotation instructions take a bit register for the value to be shifted, and the fixed 8-bit register cl for the shift count.

Many arithmetic instructions can take an immediate value as the first operand. The immediate value is fixed not variable , and is coded into the instruction itself.

For example:. Now is a good time to talk about one principle in assembly programming: Not every desirable operation is directly expressible in one instruction.

In typical programming languages that most people use, many constructs are composable and adaptable to different situations, and arithmetic can be nested. In assembly language however, you can only write what the instruction set allows. To illustrate with examples:. When performing bit shifting, the shift count must be either a hard-coded immediate value or the register cl. It cannot be any other register. If the shift count was in another register, then the value needs to be copied to cl first.

There is a bit register named eflags which is implicitly read or written in many instructions. In other words, its value plays a role in the instruction execution, but the register is not mentioned in the assembly code.

Arithmetic instructions such as addl usually update eflags based on the computed result. Some instructions directly affect a single flag bit, such as cld clearing the direction flag DF. Comparison instructions affect eflags without changing any general-purpose registers. Most of the time, the instruction after a comparison is a conditional jump covered later. So far, we know that some flag bits are related to arithmetic operations.

Other flag bits are concerned with how the CPU behaves — such as whether to accept hardware interrupts, virtual mode, and other system management stuff that is mostly of concern to OS developers, not to application developers.

For the most part, the eflags register is largely ignorable. The system flags are definitely ignorable, and the arithmetic flags can be forgotten except for comparisons and bigint arithmetic operations.



0コメント

  • 1000 / 1000