- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/ AsmTutor02 >


Assibly Tutorial
Chapter 2

Written by MAD

Index of Chapter 2

Hello everybody, I hope you read the first part of this tutorial, because I assume you understand all that. I don't think I'll have to explain again, but okay... This is an assibly tutorial, designed for you to learn assibly fast. I'll only discuss the usefull things and the basics. I explain 8086 coding, so NO 32-bit registers or protected mode. I just explain everything like the computer has a 16-bit data bus. In higher level tutorials I will explain 80386 coding, so stay tuned. Now let's get started!


Addressing Miory

In chapter 1 we used the instruction LEA to check where data is stored and we used INT 21h Function 09 to display this data. However, this INT is rather slow, and speed is the limit. So now I'll discuss a way to address the data directly and store it into the video miory. Let's take a look at the following program. Since the programs are becomming bigger, I decided to upload the source code myself. I'll continue to do this in the future, so that means no more cutting and pasting. The source code can be found here. I suggest you print the source and have it with you when you read this tutorial.

There's a lot of new stuff in this program! I'll start explaining...BTW, I only explain the new things.

.stack 10 : Is like .stack only now the stack will be 10 instead of 1024 bytes long.

dw 0 : Define Word (like db, only a word this time)
dd : Define Double word (like dw, only 2 words this time)
db 3 dup ("!") : Defines 3 bytes with the value "!". Same as: db "!!!"

mov es,[video_segment] : Important one. If you type something between square brackets [] in assibly, it means you don't want the location of the identifier but the contents. So ES will be loaded with the value 0B800h (note the zero before the B, if you don't do this, the assibler thinks it's an identifier)
mov [value_one],"H" : About the same as above. The byte value_one will be loaded with the value "H" (note, if you type ASCII between "" the assibler will generate the ASCII number. Eg. space bar is 20h, so " "=20h)
mov byte ptr [value_two],"e" : "Byte ptr" lets the assibler know you want to store a byte. This must be done, because value_two was declared as a word. (DW) If we would have declared it a byte (DB), like value_one, you could have ommited the "byte ptr".
mov byte ptr [value_two + 1],"l" : Same as above, except for the +1. The +1 means the assibler has to add one to the address of value_two. This way, an "l" is stored in the second byte of the word value_two (still get it?)
mov word ptr [value_three + 2],2020h : Word ptr means that we want to store a word. (this way the bytes will be stored in reverse order, see part 1) The +2 indicates that we want to store the value in the second word of the dword value_three.
inc byte ptr [di] : First we loaded the address of value_four into DI. INC adds one to a value. But, not DI is increased. Since DI is in brackets [DI] the contents of the miory location DI points to (value_four) will be increased. Byte ptr again indicates that the value is a byte.

If you don't add a "ptr" where one IS needed, the assibler (TASM) wil give an "argument needs type override" error. You don't need a "ptr" everywhere, if the assibler can know what type you want to store, you don't need a "type override". For example "mov es,[video_segment]". Since ES is a word and video_segment was declared as a word also, you don't need a type override. Just see for yourself, you can just leave thi everywhere, and see at wich lines the assibler gives an error. In the miory from value_one to value_five now stands: Hello everybody!!!


The Video Miory

Principles

The part that actually writes the string to the screen is between commented lines. But before I talk about that, I want to explain how the video miory works. The video miory is a miory region that's 64K big. In 80x25 Text mode only 4000 bytes are used (80*25*2). The miory region starts at segment 0B800h. If you start debug and type "d 0b800:0" you can see it. It looks like this:

B800:0000 20 07 20 07 20 07 20 07
B800:0008 20 07 20 07 20 07 20 07
etc.

It will only look like this, when the first line on the screen is blank. The first byte (B800:0000) is the first character on the screen. The second byte is the atribute of the character. An atribute is a value that indicates how the character looks, eg. what colour. Below is a list of all possible atributes and their means.

Attributes:

Atribute = background colour * 16 + foreground colour
Add 8 with the foreground for a bright version.
Add 8 with the background for a blinking version.

0 = Black
1 = Blue
2 = Green
3 = Cyaan
4 = Red
5 = Magenta
6 = Brown
7 = White

So 07h means: white text on a black background. Now suppose we want blinking, bright red text on a blue background.
Red=4 Bright red=4+8=12=0Ch. Blue=1*16=16+8=24=18h ==> 18h + 0Ch = 24h


The Video Miory

Practice

Now you understand how the video miory works, let's take a look how the program actually writes to it.

xor di,di : eXclusive OR, a logical instruction. It works this way:

XOR 0 1
0 0 1
1 1 0
So if we XOR a number with the same number, it will be zero. (1 XOR 1 = 0, 364 XOR 364 = 0). We could have typed: mov di,0
cld : CLear Direction flag, sets the direction flag to 0. (see part 1)
lodsb : LOaD StringByte, loads a byte from DS:SI (Source Index) into the AL register. Almost the same as: mov byte ptr al,ds:[si]. However, SI is increased, or decreased, depending on the direction flag. If set to 0 SI is increased, otherwise SI is decreased.
stosw : STOre StringWord, stores a word from the AX register on the miory location ES:DI (Destination Index) Almost the same as: mov word ptr es:[di],ax. DI is increased or decreased like with "lodsb", only DI is increased by two (word).
loop print_loop : The number in CX indicates how many times to loop. So in this case, it will loop 19 times between print_loop and the loop instruction.

Now, try to figure out what this little part of the code does yourself. I'll explain it below, but please, try it yourself first. You'll learn from that. With programming you always have to try and figure out things. Also, don't be afraid to change the code, just do crazy stuff and try! So what if the systi crashes!

Fist we load DI with 0, we do this because we want stosw to store the data at an offset zero. Then we load SI with the offset of the message we want to display. After that we load CX with 19, because the message has a length of 19 bytes and so we have to loop 19 times. We load AH with 07, this is the attribute of the characters we want to display. (07=white on black) Now we clear the direction flag, so that SI and DI are increased and not decreased. After all this initialization the actual loop starts here. First it loads AL with the value on DS:SI. AH was 07, so AX will be 07xxh. Now we store AX at ES:DI (the video miory) Because we store a word, it is stored in reverse order (see part 1) So the the first two bytes of the video miory look like this: xx 07. DI and SI were increased, so we just loop 19 times to store all characters this way. Simple huh?

Okay, that's how the program works. I hope you learned something... Writing this message to the screen can, of course, be done a lot easier. The first part of the code was just to show you how addressing miory works. Also, with this little text, you better use INT 21h Function 09. But if you want to clear the screen (writing 2000 spaces), you better do it this way.

We have come to the end of this tutorial already. I'll continue writing more tutorials. They can always be found at:

http://www.xs4all.nl/~smit/ My own assibly page
http://www.geocities.com/SiliconValley/6112/ The UZteam page



Copyright 1996 Ferdi Smit. All rights reserved.
Design and text by MAD
Based on the design of UZillusion.
Send any comments and suggestions to UZteam



* #WhiteUnicorn/ StartPage/ Documentation/ AsmTutor02 >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile