Search:

Submit Article

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

Posted By: Easy Tutor     Category: Assembly Language     Views: 23426

## 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

@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

RET                            ; return control to the calling procedure
DECIMAL_OUTPUT ENDP

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

END MAIN

;**************************************************************************;
;**************************************************************************;
;------------------------------  THE END  ---------------------------------;
;**************************************************************************;
;**************************************************************************;
```
Share:

 Easy Tutor author of Program that will read a decimal number and print it on the next line using procedures for DECIMAL input and output is from United States. Easy Tutor says Hello Friends,I am Free Lance Tutor, who helped student in completing their homework. I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] comI have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada. I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.Thanks,Happy Programming :) View All Articles