Introduction to x86 assembly
In this article I'm goin' to provide you with basics how, x86 architecture assembly works, as well a a bit of Intels processor history. So you will better understand how everything works.
|Intel386™ Processor Family
Some time passed after making the ENIAC (the first computer) in 1946. In 1971 Intels first microprocessor 4004 (pic
) was introduced by Intel (designed by Federico Faggin) wich used transistors as the main part, not electric lamps like ENIAC. It wasn't much better than todays calculators. Then in 1972 Intel made 8008 (pic
)wich was used in one of first home microcomputers, the Mark-8. It was the first 8bit microprocessor and it ran at 800kHz (compared to todays 3GHz...). In 1974 the 8080 was created, the first popular microprocessor wich was used in Altairs PCs. In 1978 faster and more advanced chip was introduced by Intel, the 8086 chip It's the starter of x86 processor family.. The same year faster modification was released - the 8088 (pic
). It had most of todays main x86 instructions. 8086 was a 16bit processor! It ran at ~5Mhz. In 1982 the 80286 was introduced by Intel. New opcodes were added, but teh comptability with 8086 software was left. It's one of the reasons, why 286 became so popular. It was capable of addressing 16MB RAM (older chips supported up to 640KB of memory) and frequencies were 6 - 16Mhz. AMD stepped in with 20Mhz version of 80286. Then in 1985 Intel386™, the first 32bit MultiTasking Intel processor was out. It introduced ProtectedMode in order to leave the comptability for Softwere written for 286 and earlier. It introduced many new opcodes, registers and features. It ran at 20-33Mhz frequencies and had several modifications like 386SL and 386DX. In 1989 Intel486™ came out, filled with new features, like FPU, pipelining and more. Graphical Operating Systems started their history then. Linux had graphical support, and you could run early windows on 486. The frequencies for 486 were from 33Mhz to 100Mhz. You could run graphical OSes on 386, but it was still too slow. The 1993 was the year of even more advanced microprocessors, the Pentium® family was started, improving x86 technology. Then Pentium Pro was introduced, with MMX (MiltiMedia eXtension) technology support, allowing real time movies to be displayed faster than ever. MMX allowed faster maths processing on 64bit data. The Pentium II fully supported MMX. Later processors only become faster and new technologies and extensions were introduced, like SSE, SSE2, SSE3, SIMD extensions and so on.
AMD was the only company that was real competitioner to Intel. After Intel released it's 486, 100MHz, AMD released
Am486® wich ran at 120Mhz. Now AMD has fully working 64bit CPU and Windows XP 64Bit Edition is based on AMD Athlon 64 architecture. So it's a myth that AMD is making low class CPU's, just look at benchmarks in Hardware sites.
OK. Now lets see how Assembly language and x86 architecture looks like. Lets start with the main registers
AX - Accumulator Register
BX - Base Register
CX - Counter Register
DX - Data Register
SI - Source Index
DI - Destination Index
BP - Base Pointer
SP - Stack Pointer
These are 16bit registers. To get 32bit registers just add letter 'E ' to register name - EAX, EDI... Using AL, BL, CL and DL, you can access low byto of register and using AH, BH, CH, DH - high byte. There are Segment Registers wich are used to access data in another memory segments, they are:
CS - Code Segment
DS - Data Segment
ES - Extra Segment
SS - Stack Segment
They are used to access memory segments, each segment is 64KB. 386 and later processors have control registers CR0, CR1, CR2, CR3. They are used to set special processors propierties.
Well you have registers... What do you do with them? Good question. To operate values in registers there are special instructions. Today's processors have a lot of them, I'll explain only the main ones here.
MOV dest, src - moves value form src to dest.
mov ax, 15 - put 15 to ax
mov ax, bx - moves value of bx to ax.
mov dx,[bx] - moves value from memory location addressed by bx.
mov dx,[es:bx] - moves memory value, located in es segment at bx offset.
ADD dest, src - Add src to dest
add ax, 5 - incrase ax by 5
add bx, ax - add ax to bx
SUB dest, src - Substract src from dest
sub cx, 7 - substract 7 from cx
sub bx, dx - substract value of dx from bx
MUL val - Multiply AX register by val
mul bx - multiplies AX by BX
This is how instructions look like. Bigger table of instructions can be downloaded here. But there are lots more of instructions.
Using DOS and BIOS functions
DOS and BIOS provides simple functions that ease programmers life. Using BIOS and DOS functions programmer can easily write text on the screen, set video modes, get keyboard data, read disks and floppies and a lot more, and doesn't requere hardware coding experiance. For example seting video mode in BIOS:
mov ah, 0 - Video BIOS 'Set Display Mode' function.
mov al, 13h - Video Mode (you can set al & ah bu hust setting ax 'mov ax,13h')
int 10h - Video BIOS interrupt
And setting Video Mode without BIOS needs setting video cards registers (writting about 9 values). That would take ~20 lines in assembly. DOS uses interrupt 21h. printing text on the screen using DOS (for NASM compiler):
jmp start ; jumps to start
db text "Hello, world!",$ ; text needs to finish with $
mov dx, text ; movs text pointer to dx
mov ah, 9 ; DOS print function
int 21h ; we call it ;)
Assembly is a lot to learn, especially if you want to be good assembler programmer. Read books, tutorials, articles on assembly, read forums wich usually contains a lot of usefull information, try to code, do some research and you will definatly make it. ;) Good luck!