Assembly Programming
Assembly Programming
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. The ah register is
used to pass this information.
3. We request MS-DOS to carry out the I/O operation using the int
instruction. This means that we interrupt our program and transfer
control to the MS-DOS subprogram that we have specified using
the ah register.
8086 version:
2. We call MS-DOS to carry out the I/O operation using the int
instruction as for character output.
C version:
8086 Version:
mov ah, 1h ; keyboard input subprogram
int 21h ; character input
; character is stored in al
mov c, al ; copy character from al to c
C version:
c = getchar() ;
putchar( c ) ;
8086 version:
mov ah, 1h ; keyboard input subprogram
int 21h ; read character into al
If you have syntax errors, you will get error messages at this point.
You then have to edit your program, correct them and repeat the
above command, otherwise proceed to the link command,
pressing Return in response to prompts for file names from masm
or link.
To execute the program, simply enter the program name and press
the Return key:
C> prog1
a
C>
.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 directives, .model and .stack 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.
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.
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.
This same label is also used by the end directive. When a program
has finished, we return to the operating system.
Time-saving Tip
Since your programs will start and finish using the same format,
you can save yourself time entering this code for each program.
You create a template program called for example,
template.asm, which contains the standard code to start and
You then edit prog2.asm and enter your code in the appropriate
place.
Example 3.9: The following template could be used for our first
programs:
.model small
.stack 100h
.code
start:
.model small
.stack 100h
.code
start:
mov dl, al
end start
Assuming you enter the letter ‘B’ at the keyboard when you
execute the program, the output will appear as follows:
C> prog2
BB
Note: In this example we must save the character entered (we save
it in bl) so that we can use ax for the display subprogram number.
C> io4
? x
x
.model small
.stack 100h
.code
start:
; display ?
mov dl, ‘?’ ; copy ? to dl
mov ah, 2h ; display subprogram
int 21h ; display ?
;display Return
mov dl, 13d ; dl = CR
mov ah, 2h ; display subprogram
int 21h ; display CR
;display Line-feed
mov dl, 10d ; dl = LF
mov ah, 2h ; display subprogram
int 21h ; display LF
.model small
.stack 100h
.code
start:
mov dl,‘?’
mov ah,2h
int 21h
mov ah,1h
int 21h
mov bl,al
mov dl,13d
mov ah,2h
int 21h
mov dl,10d
mov ah,2h
int 21h
mov dl,bl
mov ah,2h
int 21h
mov ax,4c00h
int 21h
end start
Which program is easier to read and understand ?
The first version uses the method of high level languages and
simply encloses the string in quotes. This is the preferred
method.
String Output
MS-DOS provides subprogram number 9h to display strings
which are terminated by the ‘$’ character. In order to use it
we must:
.model small
.stack 100h
.data
message db ‘Hello World‘, 13, 10, ‘$‘
.code
start:
mov ax, @data
mov ds, ax
end start
Exercises
C> sub
Enter a character: x
You entered: x
.model small
.stack 100h
.data
prompt db ‘Enter a character: $‘
msgout db ‘You entered: $‘
.code
start:
mov ax, @data
mov ds, ax
CR equ 13d
LF equ 10d
MAX equ 1000d
MIN equ 0
.model small
.stack 100h
.data
CR equ 13d
LF equ 10d
.code
start:
mov ax, @data
mov ds, ax
end start