Intel IA-32 Assembly Tutorial
Assembly language is the oldest programming language, and of all languages, it bears the closest resemblance to the native language of a computer. It provides direct access to a computer's hardware, making it necessary to understand a great deal about a computer's architecture and operating system.
Assembly is not a portable language meaning that it can only run on machines that it was written to run on.
What is an assembler? An assembler is a program that converts source-code programs from assembly language into machine language. A companion program called a linker combines individual object files by an assembler into a single executable program.
A single C/C++ statement like this:
x = 2 + 3 * 4; //multiply 3 and 4 then add 2
Expands to this in assembly:
mov eax,4 ;move 4 to the EAX register mov ebx,3 ;move 3 to the EBX register mul ebx ;multiply EAX by EBX add eax,2 ;add 2 to the EAX register which holds 12 now mov x,eax ;move EAX to x variable
A smart C/C++ compiler might produce this:
mov eax,3 ;move 3 to the EAX register shl eax,2 ;left shift EAX by two bits add eax,2 ;add 2 to the EAX register which holds 12 now mov x,eax ;move EAX to x variable
Virtual Machine Concept
The virtual machine concept can be used to explain how hardware and software is related. Each layer of the hardware (digital logic and microarchitecture) and software (instruction set architecture to high-level languages) can each be seen as a virtual machine. They each run on top of each other making it simplier but less efficient.
|INSTRUCTION SET ARCHITECTURE|
It would be too duanting a task for a programmer to write a program using the instruction set because this would be all numeric. In the early days, crude operating systems were written this way. Also, the first assembly language program was written this way. Examples of high-level languages are C++, C#, Java, Visual Basic, etc.
IA-32 Processor Architecture
The IA-32 processors began with the Intel 386DX and are still produced today even though some are 64-bit processors with IA-32 compatibility. Since the 386DX, processor basics have not changed very much.
There are three modes of operation: protected mode, real-address mode and system management mode. Protected mode is the native state of the processor where all instructions are available. In this mode, programs are given separate memory areas and the processor can detect when a program tries to access memory outside its own area.
Program Execution Registers
Registers are inside the CPU and are very high speed storage locations (faster than memory). There are ten 32-bit general purpose registers: EAX, EBX, ECX, EDX, EBP, ESP, ESI, EDI, EFLAGS and EIP. There are six 16-bit segment registers: CS, SS, DS, ES, FS and GS. EAX, EBX, ECX, EDX, EBP, ESP, ESI and EDI divide into smaller registers.
- SI, DI, BP and SP are only used for programs running in real-address mode.
- EAX is used by multiplication and division instructions.
- ECX is used as a loop counter by the CPU.
- ESP addresses data on the stack which is a system memory structure.
- ESI and EDI are reserved for high-speed memory transfer instructions.
- EBP is used by high-level languages to reference function parameters and local variables on the stack.
EIP contains the address of the next instruction to be executed.