# Program that will read a decimal number and print it on the next line using procedures for DECIMAL input and output

``` .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 in decimal form
; input : none
; output : store binary number in BX
; uses : MAIN

@ERROR:                        ; jump label
LEA DX, ILLEGAL                ; load and display the string ILLEGAL
MOV AH, 9
INT 21H

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 \"-\"

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

@MINUS:                        ; jump label
MOV CX, 1                      ; set CX=1

@PLUS:                         ; jump label
INT 21H                        ; read a character
CMP AL, 0DH                    ; compare AL with CR

@INPUT:                        ; jump label
CMP AL, 30H                  ; compare AL with 0

CMP AL, 39H                  ; compare AL with 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

@END:                          ; jump label

OR CX, CX                      ; check CX is 0 or not
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
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

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