Code For Program That Will Read A Decimal Number and Print It On The Next Line Using Procedures For DECIMAL Input and Output in Assembly Language

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Code for Program that will read a decimal number and

print it on the next line using procedures for DECIMAL


input and output in Assembly Language
.MODEL SMALL
.STACK 100H

.DATA
PROMPT_1 DB 'Enter a Decimal number (-32767 to 32767) : $'
PROMPT_2 DB 0DH,0AH,'The given Decimal number is : $'
ILLEGAL DB 0DH,0AH,'Illegal character. Try again : $'

.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX

LEA DX, PROMPT_1 ; load and display the string PROMPT_1


MOV AH, 9
INT 21H

CALL DECIMAL_INPUT ; call the procedure DECIMAL_INPUT

LEA DX, PROMPT_2 ; load and display the string PROMPT_2


MOV AH, 9
INT 21H

CALL DECIMAL_OUTPUT ; call the procedure DECIMAL_OUTPUT

MOV AH, 4CH ; return control to DOS


INT 21H
MAIN ENDP

;***********************************************************************
***;

;***********************************************************************
***;
;------------------------- Procedure Definitions
------------------------;

;***********************************************************************
***;

;***********************************************************************
***;

;***********************************************************************
***;
;--------------------------- DECIMALY_INPUT
------------------------------;

;***********************************************************************
***;

DECIMAL_INPUT PROC
; this procedure will read a number indecimal form
; input : none
; output : store binary number in BX
; uses : MAIN

JMP @READ ; jump to label @READ

@ERROR: ; jump label


LEA DX, ILLEGAL ; load and display the string ILLEGAL
MOV AH, 9
INT 21H

@READ: ; jump label


XOR BX, BX ; clear BX
XOR CX, CX ; clear CX

MOV AH, 1 ; set input function


INT 21H ; read a character

CMP AL, "-" ; compare AL with "-"


JE @MINUS ; jump to label @MINUS if AL="-"

CMP AL, "+" ; compare AL with "+"


JE @PLUS ; jump to label @PLUS if AL="+"
JMP @INPUT ; jump to label @INPUT

@MINUS: ; jump label


MOV CX, 1 ; set CX=1

@PLUS: ; jump label


INT 21H ; read a character
CMP AL, 0DH ; compare AL with CR
JE @END ; jump to label @END if AL=CR

@INPUT: ; jump label


CMP AL, 30H ; compare AL with 0
JL @ERROR ; jump to label @ERROR if AL<0

CMP AL, 39H ; compare AL with 9


JG @ERROR ; jump to label @ERROR if AL>9

AND AX, 000FH ; convert ascii to decimal code

PUSH AX ; push AX onto the STACK

MOV AX, 10 ; set AX=10


MUL BX ; set AX=AX*BX
MOV BX, AX ; set BX=AX

POP AX ; pop a value from STACK into AX


ADD BX, AX ; set BX=AX+BX

MOV AH, 1 ; set input function


INT 21H ; read a character

CMP AL, 0DH ; compare AL with CR


JNE @INPUT ; jump to label if AL!=CR

@END: ; jump label

OR CX, CX ; check CX is 0 or not


JE @EXIT ; jump to label @EXIT if CX=0
NEG BX ; negate BX

@EXIT: ; jump label

RET ; return control to the calling


procedure
DECIMAL_INPUT ENDP

;***********************************************************************
***;
;--------------------------- DECIMAL_OUTPUT
-----------------------------;

;***********************************************************************
***;

DECIMAL_OUTPUT PROC
; this procedure will display a decimal number
; input : BX
; output : none
; uses : MAIN

CMP BX, 0 ; compare BX with 0


JGE @START ; jump to label @START if BX>=0
MOV AH, 2 ; set output function
MOV DL, "-" ; set DL='-'
INT 21H ; print the character

NEG BX ; take 2's complement of BX

@START: ; jump label

MOV AX, BX ; set AX=BX


XOR CX, CX ; clear CX
MOV BX, 10 ; set BX=10

@REPEAT: ; loop label


XOR DX, DX ; clear DX
DIV BX ; divide AX by BX
PUSH DX ; push DX onto the STACK
INC CX ; increment CX
OR AX, AX ; take OR of Ax with AX
JNE @REPEAT ; jump to label @REPEAT if ZF=0
MOV AH, 2 ; set output function

@DISPLAY: ; loop label


POP DX ; pop a value from STACK to DX
OR DL, 30H ; convert decimal to ascii code
INT 21H ; print a character
LOOP @DISPLAY ; jump to label @DISPLAY if CX!=0

RET ; return control to the calling


procedure
DECIMAL_OUTPUT ENDP

;***********************************************************************
***;
;----------------------------------------------------------------------
----;

;***********************************************************************
***;

END MAIN

You might also like