Why learn asm?
Assembly is seen by many as being too much work to do most tasks. This although partly true assembly is the one of the fastest langs around and is a low level language so it lets you get down and dirty with your machine. Asm also is a great learning experience and teaches you how your computer works.
Ok lets get started!
CODE.model small.stack.dataMessage db "z0mg ASM!!$" .codestart:mov dx,OFFSET Message mov ax,SEG Message mov ds,ax mov ah,9 int 21h mov ax,4c00h int 21hEND startOk happy? Fin.
tongue.gif alright i guess i can explain it a little bit.
First all asm programs start off with a .model call. in this situation we use the small memory model which means that the data and code segments are separate but both are less than 64k. Ill explain more about memory models in another article.
The .stack call is required for .exe programs which require a call stack to function.
We then move into the .data segment. This is where all of your variables are stored.
CODEMessage db "z0mg ASM!!$"In this example "
message" is your var name. Db stands for declare byte and sets the size of your var. The "{:content:}quot; symbol terminates the string and also stores the string length in the var.
.codeThis segment stores your code! Who would of thought that? tongue.gif
The first thing you'll notice is the
mov command. This copies x into y. It doesn't matter what y is so it can be a value register or var etc however you have to make sure x is able to store the amount of data and is same data type.
Now were going to take a bit of a detour here to talk about registers.
For now were only going to talk about the 16bit registers. Our general purpose registers are:
#note to use their 32bit cousins add an "E" to the front of the name #
axbxcxand
dxNow all of those above registers are 16bits in size however they each are actually 2 registers. ax for example is split into ah and al. However ax al and ah can each be used separately for some tasks.
Now the next thing you'll notice is the
SEG and
OFFSET. During the early computer years they felt no one would ever use more than 1mb so the chip was designed to not allow anything over 1mb. The problem however was that to access 1mb 20bits were needed. Since registers were only 16 bits and they felt using 2(32bits) was impractical they decided to use
SEGMENTS and
OFFSETS to access 20bits using only 1 register.
EX:
OFFSET=SEG x 16
SEG=Offset / 16( the bottom 4 bits are lost sad.gif )
1 register has the offset and 1 has the segment if you put them together you have a 20bit address biggrin.gif .
EX2:
CODE
SEGMENT 0010010000010000---- OFFSET ----0100100000100010 20-bit Address 00101000100100100010
DS stores the segment and SI stores the offset. Note any general register can be used to store an offset except for the segment registers which are CS, DS, ES, SS.
^^ I hope you understand all that it took me a while to figure out.
The mov ds,ax points or segment and offset to create our 20bit register which stores our string.
Next we move 9 into ah then use int 21. Int cmds are interrupts are like functions and int 21 is the most common as it calls dos. When we use int 21 it searches ah for the function and in this case its 9 which is print string.
The final part mov ax,4c00h then int21 returns us to DOS. Ill fully explain it in the next tutorial.
Ok now you know how it works so lets compile it. I prefer to use tasm so thats how im going to explain it. Save the file as test.asm # or wtf you want.asm
CODE
C:PerlTTASM5BIN>tasm testTurbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland International
Assembling file: test.ASMError messages: NoneWarning messages: NonePasses: 1Remaining memory: 453kC:PerlTTASM5BIN>tlink testTurbo Link Version 7.1.30.1. Copyright (c) 1987, 1996 Borland International
Well folks you have just learned the basics "
hello world" style program and how it works in x86 asm. In the next tut ill cover input control and maybe even some graphical work. If you have any questions feel free to ask away.