UMBC CMSC 211

UMBC | CSEE


Numeric I/O

After macros, comes libraries. A library is the same in assembly language as it is in C. We will look at using a library for doing input and output of numeric values. It is not as easy as it looks, and we are not at the stage where we can do it right. Won't be there until chapter 7 and 14 that we will be look at the specifics.

We will use three library calls to do the work for us now. These call handle signed numbers.

To be able to use them, immediately after the .CODE pseudo-op, put:
    .CODE
    EXTRN  GetDec : NEAR, PutDec : NEAR, PutHex : NEAR	
ProcName PROC   etc.
    
Then when we link the program, we must use: link MyProgFile,,,util That is three, exactly three commas. It assumes that the file util.lib is in the current directory.

The alternative form is:

ml MyProgFile util.lib

Example

;; DECTOHEX	.ASM  - A program which requests input of a decimal number from the
;;		keyboard and then prints its hex representation 
;;
;; Must be linked using:
;;	link DECTOHEX,,,UTIL
;;
INCLUDE	PCMAC.INC
		.MODEL	SMALL
		.STACK	100h
		
		.DATA	
CR		EQU    13
LF		EQU    10
Prompt		DB     'Enter decimal number: $'
OutMsg		DB     'The hex equivalent is $'
SaveAX		DW     ?

		.CODE
		EXTERN	GetDec : NEAR , PutHex : NEAR
DecToHex	PROC
		mov	ax, @data
		mov	ds, ax

		_PutStr	Prompt
		call	GetDec;	;ax := decimal number from keyboard
		mov	SaveAX, ax	;Save ax in variable
		_PutStr	OutMsg	
		mov	ax, SaveAX	;Restore AX
		call	_PutHex	;Display input has hex
		_Putch	'H', CR, LF	;Display trailing 'H' to show its a hex 
					;  value and start a new line
		_Exit	0		;Return to DOS

DecToHex	ENDP
		end		DecToHex

Programming Challange

Write a program called Today which displays today's date in the form:

Today is mm/dd/yyyy Use the DOS Get Date call (_GetDate from PCMAC.INC) where after the call, you will find the following:

dh = month (1 to 12)
dl = day (1 to 31)
cx = year (e.g., 1984)
al = day of the week (0 to 6)

Trick of the trade

To make an unsigned byte into a word and display it:
mov  al, dh
mov  ah, 0     ; Extend to a word, assuming it is not signed
call PutDec    ; Show it!
    
Note: There is actually an instruction to do this with signed values for us, but we will look at it in Chapter 4.


UMBC | CSEE