8086 Programming
8086 Programming
Variables: declaration/definition
Assignment: assigning values to variables
Input/Output: Displaying messages
Displaying variable values
Control flow: if-then
Loops
Subprograms: Definition and Usage
Variables
For the moment we will skip details of variable declaration and
simply use the 8086 registers as the variables in our programs.
Registers have predefined names and do not need to be
declared.
Assignment
x = 42 ;
y = 24;
z = x + y;
mov x, 42
mov y, 24
add z, x
add z, y
Example:
mov bx, 2
mov cx, bx
Example:
inc ax ; add 1 to ax
; ax now contains 9
The dec instruction like inc takes one operand and subtracts 1
from it. This is also a frequent operation in programming.
Exercises:
a = b + c –d
z = x + y + w – v +u
mov ax, 0
Label_X: add ax, 2
add bx, 3
cmp ax, 10
jl Label_X
Introduction to 8086 Assembly Language Programming, Joe Carthy, UCD 7
The above loop continues while the value of ax is less than 10.
The cmp instruction compares ax to 0 and records the result.
The jl instruction uses this result to determine whether to jump
to the point indicated by Label_X.
Input/Output
The 8086 provides the instructions in for input and out for
output. These instructions are quite complicated to use, so we
usually use the operating system to do I/O for us instead.
For I/O and some other operations, the number used is 21h.
This means that you must also specify which I/O operation
(e.g. read a character, display a character) you wish to carry
out. This is done by placing a specific number in a register.
A Complete Program
We are now in a position to write a complete 8086 program.
You must use an editor to enter the program into a file. The
process of using the editor (editing) is a basic form of word
processing. This skill has no relevance to programming.
H:\> prog1
a
H:\>
.model small
.stack 100h
.code
start:
mov dl, ‘a’ ; store ascii code of ‘a’ in dl
The first three lines of the program are comments to give the
name of the file containing the program, explain its purpose,
give the name of the author and the date the program was
written.
The first two are directives, .model and .stack. They are
concerned with how your program will be stored in memory
and how large a stack it requires. The third directive, .code,
indicates where the program instructions (i.e. the program
code) begin.
Introduction to 8086 Assembly Language Programming, Joe Carthy, UCD 14
For the moment, suffice it to say that you need to start all
assembly languages programs in a particular format (not
necessarily that given above. Your program must also finish in
a particular format, the end directive indicates where your
program finishes.
You must also specify where your program starts, i.e. which is
the first instruction to be executed. This is the purpose of the
label, start.