Welcome to this beginners guide to Pascal inline assembler! This page is mainly written for those of youthat are familiar to pascal, but not to the inline assembler. Maybe you've read some of those pascalhelps there’s out there, and seen inline being frequently used, but didn't understand anything of it! Ormaybe you've figured a few things out about inline assembler, then see this as a way to master it! The first chapter will take up what assembler is and a little about the hexadecimal numeric format. Don't worry about this yet, you will understand it if you reed this tutorial. The next chapters will be aboutsome more usable options, but I haven't written them yet, so feel free to leave comments to me about what you don't understand and I’ll modify any unclearities. This tutorial won't take up every detailin the processor and explain what it does, I'll leave that bull to the bookwriters. I will mainlyexplain how to code it, and how to code it good! I guess you only read this because you want to optimize yourcode, so I'll teach you how you write the FAST assembler (FASTES is to much to say...). And of cource, thethings that you can't do in pascal is in here to. When you once start to use assembler for real, you'll learnthat it is the ONLY way to stay in total control of you program. But it fast gets messy, so remember to useblank lines and comments alot (that's ALOT, boys and girls!). To you programmers who already got the hungof the basics in assembler can go to the Advanced Section and the Graphics Section directly. ( Those arenot yet written, sorry). Those will discuss how to make your own graphics routines, mainly in mode 13 (320*200*256),and how to speed up your code with assembler.Well, the index:
Chapter one : What is assembler? |
Chapter two : The Basics |
Back to MainPage |
CHAPTER ONE
The processor in a 80X86 computer does not understand anything (surprise surprise), you must communicatewith it in it's own language. Now this happens to be in digits, and actually in only 1:s and 0:s, whichmakes it a pretty bad conversation partner. To tell it what you want it to do, you must translate yourcommands into numbers which means special operations. To memorize all the numbers the processor understandis fairly unsmooth, and look up every instruction in a codetable is pretty uncomfortable too. That's why wehave programming languages! Languages like Pascal and C++ and all other highlevel languages mainly readsa text file containing commands, and transferres it into machine language (the processors language) by it'sown rules. Every command in theese languages convertes into a bunch of processor commands. It is hard tooptimize the program for best performace. Now, the simplest programming language you could possibly think of is naturally one that only translates every command into a predefined number and your processor code is finished! That's assembler (almost). It's far more easy to understand than machine language, but puts you in total control.Since we shall only use inline assembler (so far), I will not tell you how to allocate variables or writing procedures in assembler, since BASM don't accept that. By the way, Borland Inline Assembler will from here on be referred to as BASM.
THE HEXADECIMAL NUMERIC FORMAT
You that know how the hexadecimal numeric format works can skip this part. There's nothing new. And ifyou have trouble understanding, it isn't that important, it just makes you get a better feeling with thenumbers.
As we all know, the normal numeric format is the one that goes 1,2,3,4,5,6,7,8,9,10,12,etc...That's the Decimal numeric system. It goes to nine, and then you get one extra digit in therow to the left. You say the decimal system has the base ten, because there is ten different digits. The Hexadecimal isn't that different. Only that it has the base sixteen, it goes to fifteen and then updatesthe digit to the left. To make this work you need new one-letter-digits for 10,11,12,13,14 and 15. This waseasily done by using the letters A,B,C,D,E and F. So in hexadecimal you count: 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,etc...Study this table and try to understand.
Decimal | Hex | Decimal | Hex | Decimal | Hex | ||
---|---|---|---|---|---|---|---|
1 | 1 | 16 | 10 | 31 | 1F | ||
2 | 2 | 17 | 11 | 32 | 20 | ||
3 | 3 | 18 | 12 | 33 | 21 | ||
4 | 4 | 19 | 13 | 34 | 22 | ||
5 | 5 | 20 | 14 | 35 | 23 | ||
6 | 6 | 21 | 15 | 36 | 24 | ||
7 | 7 | 22 | 16 | 37 | 25 | ||
8 | 8 | 23 | 17 | 38 | 26 | ||
9 | 9 | 24 | 18 | 39 | 27 | ||
10 | A | 25 | 19 | 40 | 28 | ||
11 | B | 26 | 1A | 41 | 29 | ||
12 | C | 27 | 1B | 42 | 2A | ||
13 | D | 28 | 1C | 43 | 2B | ||
14 | E | 29 | 1D | 44 | 2C | ||
15 | F | 30 | 1E | 45 | 2D |
CHAPTER TWO
The processor uses the memory for most operations, like most operations in pascal do. That's the manipulating ofvariables we all are so used to! But updating the memory all the time takes time. By using the processors own "memory" we speed things up. The processor has four "varibles" to use; the registers. There's more thanthat, but we begin here. Since BP still don't support 32bit assembler code, the registers are 16bit, meaning they can take a value between 0 and 65535 (a word). The four registers is called AX,BX,CX and DX. That seems logical but it actually means something! AX is the Accumulator. You use it when you multiply or divide, but you can use it only to store numbers in to, which is what we will begin with. The BX is the Base register,it is used to everything, it have a advanced function but you never use it... CX is the Counter register.To do loops (for-like stuff) you use it to count down. Finaly DX is the Data register. It is used for temporary memory movement, like the remainder after a division. All theese registers is 16 bit. You canaddress the upper 8 bit by typing AH,BH,CH & DH and the lower by AL,BL,CL & DL (H for high and L for low).And if BASM had supported 32 bit the you would have reffered the whole registers as EAX,EBX,ECX and EDX but now you will never do that... Of course you can refer to them all in small letters. Another importand thingis to remember that you don't won't to use real type variables with assembler. You can do it, but it unessecary and slow. You might have noticed that using decimal variables slows down your pascalprogramalot. In this tutorial I will teach you how to use fixpoint to use decimals fast.
The BASM in pascal is used by typing first ASM
and then writingthe code. If you use BP 7.0 you will notice the text going green. You end the assembler code part by typingEND;
So if you write a assembler part itcould look like:
As you see you usaly have one command per line. Most commands are two, three or four letters. They usalyhave a number of attributes, typed after the command and separated by colons(,).You can write semicolonsafter each row, but it's not neccesary. What this little program do you will understand soon. asm
end; mov
int
push
pop
mov
AX,$13
$10
$A000
ES
AX,43
Now we are ready for the first commands. In assembler the command mov
is probably the most used. What it does is it moves something from one location to another. Actually it copies it, leaving the sorce unmodefied. The syntax is: mov destination,source
.mov var,255
mov ax,variable
.mov 134,ax
.
Two other often used commands are add
and sub
. They add and subtracts. the suntaxes are:add destination,source
sub destination,source
.mov ax,213
.
sub ax,97
We first put the original valuein ax and then subracts it with 97. Make sure you understand this, because it's essential!
In the beginning, there was but a few commands... The four most basic commands there is is AND
, OR
, XOR
andNOT
. They are the simplest commands a processor can do, and they are as fast as it can be. Theese operations are thewery stone on wich all electonics was built. When the first "electronic calculator" was build, this was what itcould do. To fully understand what they do you must fully understand the binary numeric format. If you know what theydo the syntax is as follows:
and destination,source |
or destination,source |
xor destination,source |
not destination |
xor ax,ax
puts ax to zero VERY fast. Of course it workswith all registers. mul
anddiv
. They multiply and divide. The syntax is mul source |
div source |
tal
and another in the variable tal2
and wish to dividethem. The result is to be put in the variable result
and the remainder in the variable remind
. That could look like this:asm
mov ax,tal
div tal2
mov result,ax
mov remind,dx
end; { puts the value in tal into AX }
{ divides the value in AX by tal2 }
{ the result was put in AX, and is now moved to result }
{ the reminder was in DX and is now moved to remind }
This pascal source does the same thing as the assembler do, but the assembler is faster.result:=tal div tal2; |
ProcedurePutPixel(xpos,ypos:word;color:byte);Assembler;
Asm
mov ax,ypos
mov bx,xpos
xchg ah,al
add bx,ax
shr ax,2
add bx,ax
push $a000
pop es
mov dl,color
mov es:[bx],dl
End;