Library of common functions: emu8086.inc

8086 assembler tutorial for beginners (part 5)

To make programming easier there are some common functions that can be included in your program. To make your program use functions defined in other file you should use the INCLUDE directive followed by a file name. Compiler automatically searches for the file in the same folder where the source file is located, and if it cannot find the file there - it searches in Inc folder.

Currently you may not be able to fully understand the contents of the emu8086.inc (located in Inc folder), but it's OK, since you only need to understand what it can do.

To use any of the functions in emu8086.inc you should have the following line in the beginning of your source file:

include 'emu8086.inc'


emu8086.inc defines the following macros:

To use any of the above macros simply type its name somewhere in your code, and if required parameters, for example:

include emu8086.inc

ORG    100h

PRINT 'Hello World!'

GOTOXY 10, 5

PUTC 65           ; 65 - is an ASCII code for 'A'
PUTC 'B'

RET               ; return to operating system.
END               ; directive to stop the compiler.


When compiler process your source code it searches the emu8086.inc file for declarations of the macros and replaces the macro names with real code. Generally macros are relatively small parts of code, frequent use of a macro may make your executable too big (procedures are better for size optimization).



emu8086.inc also defines the following procedures:


To use any of the above procedures you should first declare the function in the bottom of your file (but before the END directive), and then use CALL instruction followed by a procedure name. For example:

; demonstrate scan_num, print_num, pthis
;----------------------------------------
include 'emu8086.inc'
ORG    100h

LEA    SI, msg1       ; ask for the number
CALL   print_string   ;
CALL   scan_num       ; get number in CX.

MOV    AX, CX         ; copy the number to AX.

; print the following string:
CALL   pthis
DB  13, 10, 'You have entered: ', 0

CALL   print_num      ; print number in AX.

RET                   ; return to operating system.

; data
msg1   DB  'Enter the number: ', 0

; macros to define procs
DEFINE_SCAN_NUM
DEFINE_PRINT_STRING
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS  ; required for print_num.
DEFINE_PTHIS

END                   ; directive to stop the compiler.


First, the compiler processes the declarations (these are just regular the macros that are expanded to procedures). When compiler gets to CALL instruction it replaces the procedure name with the address of the code where the procedure is declared. When CALL instruction is executed control is transferred to procedure. This is quite useful, since even if you call the same procedure 100 times in your code you will still have relatively small executable size. Seems complicated, isn't it? That's ok, with the time you will learn more, currently it's required that you understand the basic principle.



<<< previous part <<<      >>> Next Part >>>