Introduction to linking external ASM files into BP

What is BASM?

BASM is the built-in assembler in Borland Pascal. It allows you to write 8086/8087 and 80286/80287 assembler code in your pascal programs. For example, to init Mode-X (which is easier in assembly) one could do:
procedure modex; assembler;
consst CRTParams:array[1..10] of 
  word=($00d06,$03e07,$04109,$0ea10,$0ac11,$0df12,$00014,$0e715,$00616,$0e317);
asm
	mov	dx,03c4h
	mov	ax,0604h
	out	dx,ax    ;{disable chain4 mode}
	mov	ax,0100h
	out	dx,ax
	mov	dx,03c2h
	mov	al,0e3h
	out	dx,al

	mov	dx,03c4h
	mov	ax,0300h
	out	dx,ax

	mov	dx,03d4h
	mov	al,11h
	out	dx,al
	inc	dx
	in	al,dx
	and	al,7fh
	out	dx,al
	dec	dx
	mov	si,offset CRTParams
	mov	cx,10
@s:	mov	ax,ds:[si]
	add	si,2
	out	dx,ax
	dec	cx
	jnz	@s
end;

Why use TASM instead of BASM?

There are several reasons why we prefer putting our assembler procedures in an external file. They include: The first two are relatively intuitive. The last two, perhaps, are not. When a programmer becomes fluent in assembler, he begins to realize that there are some things that are much easier to do in assembly than in Pascal. Also, speed is an issue. With a few simple assembler procedures, he can improve a program's performance. An example is the move() procedure. By using a MOVSD/MOVSB combination to move memory, you can get an [optimum] factor of four improvement in speed. When a lot of these optimized procedures build up, it is easier to read your main program if a person don't have to read through several pages of assembly code. Portability is another issue. Although most of you do not code for other platforms, some of you may in the future. There is a very nice Borland Pascal compatible compiler for the Amiga. And there will be one [eventually] for the BeBox. By moving most of the hardware specific code out of your program (and units) a majority of the work is in re-creating the assembler portion of your code on a foreign platform.

Examples

Your assembly file must have certain headers for Borland to be able to recognize it. Here is an example skeleton .ASM file:
; file: skel.asm

.model tpascal
.386

; variables go here

.CODE
LOCALS      ; optional

; procedures go here

CODE ENDS
     END

Why MASM syntax instead of Ideal?

Tom Swan, the excellent author of such books as Mastering Turbo Pascal and Mastering Turbo Assembler, uses the Ideal syntax for TASM in his books. Ideal mode has several advantages over MASM, such as removing some of the 'quirkiness' of MASM. However, most of the differences are purely reflexive, and the only difference you need to take note of is the order the keywords appear in. For MASM, it is <name> <identifier>, and for Ideal it is <identifier> <name>. For example, using Ideal syntax you would declare a procedure as:
proc MyProcedure
; ...
MyProcedure endp
Whereas in MASM you would declare it as:
MyProcedure proc
; ...
endp MyProcedure

Near versus Far procedures

Because Borland Pascal is a 16-bit compiler, there can be both near and far procedures. This is only a minor inconvenience. Basically it comes down to this:
  • Near Procedures are used when you are linking an external assembly file directly into your program.
  • Far Procedures are used when you are linking an external assembly file into a unit, or the procedures in your program are declared as far.
If you don't want to worry about which your procedures should be declared near or far, just declare all of them as far.

Conversions

An assembly module can access global variables from your main program or unit by using the EXTRN keyword your assembly file.
These variables:
type
 Trec=record
       x,y:word
      end;
var
a : Byte;
b : Word;
c : Shortint;
d : Integer;
e : Real;
f : Single;
g : Double;
h : Extended;
i : Comp;
j : Pointer;
k : boolean;
l : ptr48;
m : array[0..15] of pointer;
n : TRec;
o : array[byte] of TRec;
p : ^Trec;
would convert to:
TRec struc
 x dw ?
 y dw ?
TRec ends

EXTRN A : Byte
EXTRN B : Word
EXTRN C : Byte
EXTRN D : Word
EXTRN E : FWord
EXTRN F : DWord
EXTRN G : QWord
EXTRN H : Tbyte
EXTRN I : QWord
EXTRN J : Dword
EXTRN K : Byte
EXTRN L : PWord
EXTRN M[16] : Dword
EXTRN N : TRec
EXTRN O[256] : Trec
EXTRN P : Dword

More Examples

Click on the parchment at the bottom of this page to download examples of how to declare procedures, functions, and how to use parameters.
-----------------------------
Previous pageUp one levelNext pageDownloadable stuff