Assembly
At this point, we must differentiate between two types of I/O, character I/O and number I/O. We have seen how characters are input and output.
Numeric I/O
To input and output integers using assembly is slightly more complicated, so to keep things simple we will use I/O procedures which will work for inputting and outputting unsigned numbers (i.e. positive only) on a standard ASCII terminal or display.
A procedure or subroutine is a portion of a program that can be referenced, or called, from many different places within a program without repeating the instructions each time the procedure is used (similar to a function in C ).
indec is a procedure which inputs an unsigned decimal number from the keyboard.
The number is entered as one or more decimal digits terminated by a carriage return (RETURN) key. The number must be represented by two bytes and therefore must be between 0 and 65,535. The number is returned in the ax register.
outdec is a second procedure which outputs an unsigned decimal to the terminal. The number is taken from the ax register. It is output without a carriage return or a line feed.
Simply, the input procedure (indec) inputs the number as a sequence of characters, convert this string into a numeric value, and returns the value. Similarly, the output procedure (outdec) outputs the number by converting the numeric value into a sequence of characters.
These subroutines are not 8086 machine instructions, rather they are just 8086 assembly language programs which were written for this course.You will have to include them in any of your programs that need to use them in. They are both available on the common drive.
To use them in your program, use the call instruction. The mnemonic call is an assembly language instruction which is part of the 8086 set and must be followed by the identifier that represents the procedure’s name which is being called.
e.g.
; the following program inputs two numbers and outputs the sum
; Authors: RC/RJ Feb. 1998
title prog3.asm
.model small
.stack 100h
.data
.code
extrn outdec : proc
extrn indec : proc
callindec; inputs an unsigned number, returns its value in ax
movbx, ax; stores first number in bx
callindec; inputs second number, it is put in ax
addax, bx; sums the two numbers, result in ax
calloutdec; outputs to the screen the integer in ax
mov ah,4ch
int 21h; terminate program
end
Points to note
1.The procedures indec and outdec if run on their own will “hang” the system since they do not terminate correctly and hand back control to DOS.
They are both intended for use within another assembly program.
2.To make an executable file of prog3.asm above the following steps must be carried out:
(a)Make the .obj files for indec ,outdec and prog3 by using
tasm indec /zi
tasm outdec /zi
tasm prog3 /zi
(b)Now link the three files into one .exe file by the command:
tlink prog3 indec outdec /v
The .exe file will have the name prog3.exe
The full procedure can be visualised as follows:
3.The extern directive is simply telling the assembler that the following procedure is not to be found in the current program but will be found external to it.
Now, when it is called, the assembler will look for this procedure in the other modules which have been compiled with the current one.
4.The line
add ax, bx
simply adds the value in bx to the value in ax and the result is put in ax .
12/03/2018Lect0798.doc - Input/OutputPage: 1