Qbasicnews.com

Full Version: ASM Functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I've learned ASM (TASM and NASM) in a short period of time, but there are still some things I don't know.

1. Can someone tell me how I make procedures or functions in ASM. E.g. I have this code:
Code:
mov ax, 0A000h
mov es, ax
mov di, 02h
mov ax, 0001h
stosb
How can I make of this tiny program a sub procedure, which can, with ARGUMENTS X and Y and Colour set a pixel on screen (Mode 13h).

2. What is the difference between PROC and MACRO

3. How can I make a subprocedure (see 1) return a value (so it's an equivalent of the QB FUNCTION).
1. Like this:

Code:
nameofproc   proc X:word,Y:word

        ; Put stuff here
        ret

nameofproc   endp

2. A proc is something that generates an explicit "call" instruction every time it is invoked. A macro is inserted into the source file at compile time for every time it is called. So:

Code:
mymacro    macro
                  mov ax,5
                  add ax,bx
                  endm
mymacro
add ax,ax
mymacro

compiles to:
Code:
mov ax,5
add ax,bx
add ax,ax
mov ax,5
add ax,bx

3. You usually return stuff in AX. You can't return stuff in the sense of "value = hello()", so by convention assembly programmers just shove the return value in AL/AX/EAX/FP0.

Assembly is limited in that you can only use very basic instructions and you cannot have more than one calculation on one line (unless you use 386 addressing tricks).
I know, but about 3.

What should I do then?

Code:
ret ax
or something like that?
You just go:

mov ax, returnvalue ; whatever this is
ret

You can return things in other registers and memory locations if you want to.
er. Ok. Please tell me 8)
Begin thinking in low level. There are no return values. Just registers. You have a set of registers. If you modify them in a procedure, once you exit it the registers remain modified, so, everything you stuff in the registers or in a memory position remains there when you "ret". That's what someone42 meant.
First of all, use masm. It's superior.
Now let me explain the different parts.

.model - tells the assembler what memory model to use. QB uses medium.
basic - tells the assembler which order the arguments are on the stack and the sort of return it should make. use basic for QB
.code - creates a segment called code, here your program code is stored

proc - A new routine
public - it should be callable outside of this file
uses es bx di - Preserve es, bx and di
\ - continues on the next line
dst:far ptr - the first argument on the stack, it's a far pointer (4 bytes)
endp - end of routine
end - required at the end of the file


Code:
.model medium, basic            
            .386
            .code
            
mypset      proc    public uses es bx di,\
                    dst:far ptr, x:word, y:word, col:word
                    
            les     di, dst
            
            mov     bx, y
            mul     bx, 320
            add     bx, x
            
            mov     ax, col
            mov     es:[di+bx], al
            
            ret
mypset      endp

mypget      proc    public uses es bx di,\
                    src:far ptr, x:word, y:word
                    
            les     di, dst
            
            mov     bx, y
            mul     bx, 320
            add     bx, x
            
            xor     ax, ax
            mov     al, es:[di+bx]          
            
            ret
mypget      endp
            
            end
Oh right, and about returning values. It depends on the language really. But here's how QB does it

integers are returned in ax
longs are returned in ax:dx
For single and double qb pushes a near ptr on the stack as the first argument. However, being as crappy as it happens to be it forgets about this in the IDE. So for it to work in the IDE as well, you need to return that near pointer in ax.
Thankx! Smile
While you're learning ASM, I would try to get the book "Assembly Language for Intel-based computers, 4th edition, by Kip Irvine." It's a recent book (published in 2003). It teaches MASM and it's excellent. I've learnt all my ASM from it. It's actually supposed to be a college textbook, but it's great to teach yourself with it.
It's long, too.
Pages: 1 2