ASM in QBasic


Before I continue I must say something. ASM is a low-level language, and that is all. For you to get the best result you must use it with a high-level language! C is a high level language and so is BASIC. You should use ASM only when you need low-level support or stuff that your high-level language can't do! Why am I saying this? Well I don't want people thinking ASM is all they need! Imagine how long and frustrating making a 32,000 line ASM Game, where you can just use BASIC to make the same 32,000 line game in about 100 lines!!! Don't get frustrated by ASM's boundaries to what you can do in a specific time, ASM is made for power-programming not general programming! Of course you can make relitavly large programs with it provided you need speed, and small file size. There is little point there though, C gives you enough speed (and so does QB in some ways).

Lesson no.5 ASM:Diffrent approach


Your now comfortable with normal way assembly works. But most assemblers have a better system. Here's an example, try to read it carefully:
.STACK 100h            ;Stack will be explained later
                        ;it is beyond the scope of this
                        ;tutorial

.CODE                  ;This is the code segment

jmp start               ;Jump to "start" no matter
                        ;what.

message db 'Hello world!', 0d, 0a, '$'

start:
mov dx,OFFSET message   ;Get the message pointer
mov ax,cs               ;put code segment in ax
mov ds,ax               ;put code segment in data segment
mov ax,0900h            ;Use function 9
int 21                  ;DOS service 21 (explained later)
mov ax,4c00h            ;Returns to DOS function
int 21                  ;DOS Exit
        
What did that do? Run it and see. Ah there we go it printed out "Hello World". But how the heck did that happen? Simple if you follow what I say.

First by saying "jmp start" we are jumping to the label called "start". Labels behave much like they do in QB as the do in asm. Except, as you'll find out, you can do a bit more with them. Now that we are in the start, we notice we skiped a line of code up there.

It says:

message db 'Hello world!', 0d, 0a, '$'
        
First the "message" part is actually a label. But that's a neat way of writing it. db means a integer or a string to put it in simple terms. Then we have the text the 0d 0a (Enter). At the end we have a $. This is actually to terminate the string.

So by saying that we get a label called message pointing to a string that say "Hello world".

Ok lets go back to buisness, after the start label there is a command that says:

mov dx,OFFSET message
        
What's offset you say? before you continue I suggest reading the segments and offsets tutorial that I've included. Done? About time =) Now that we know what segments and offsets are you should now understand that what we are doing is locating the offset of the data block after the label message, then puting that value in DX. Why DX? We will get to that shortly.

After that we have this pretty peice of code:

mov ax,cs
mov ds,ax
        
Firstly, CS is a pointer to the segment of the program's code thats running at the momment. DS is the pointer to the DATA segment. In the case of our little program, both the Data Segment and the Code segments are the same, thus we need to make the value of the data segment, the same value of the code segment. Why not just say MOV DS,CS? Well some smart ass jerk making the computer chipset model decided that MOV DS,CS is a illegal command combination (OK OK that's not the real reason but to tell you the truth I don't know ;) ) Ofcourse there is a way out of this chocking limit.

First mov the value of CS to AX, then mov the value of AX to DS!

mov ax,cs       ;mov value of cs to ax
mov ds,ax       ;mov value of ax to ds, therefor moving cs to ds.
        
Aha! After we done that we see this weird part:
mov ax,0900h            ;Use function 9
int 21                  ;DOS service 21 (explained later)
        
This simply says to use function 9 of the DOS service int. Remeber when we turned on the mouse? Well This int prints out a string.

First you put the string's offset in DX

mov dx,OFFSET message   ;Get the message pointer
        
Then you put the string's segment in DS
mov ax,cs               ;put code segment in ax
mov ds,ax               ;put code segment in data segment
        
Then you call INT 21 with 900 in AX
mov ax,0900h            ;Use function 9
int 21                  ;DOS service 21 (explained later)
        
Now we must bravely exit out of our little homemade program. There is an int perfect for this. int 21 function 4C!
mov ax,4c00h            ;Returns to DOS function
int 21                  ;DOS Exit
        
If you don't get all this, read it agian, still don't get it, read it agian, still don't get it? Email me at abionnnn@geocities.com =)

Next month we look at more registers more commands and more asm. Don't miss out =)



© Copyright 1999-2000 The QB Times.
This tutorial was taken from The QB Times with permission from the webmaster(s) of the site. You may not take this article and place it on any other website.