-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_output.asm
executable file
·152 lines (122 loc) · 3.43 KB
/
decimal_output.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
; Filename: DecimalOutput.asm
; Description: This procedure takes a number in AX and display its
; decimal equivalent
;---------- Algorithm ----------
; If the number is negative,
; print a (-) sign
; negate the number
; Convert the number in AX in decimal in stack
;do
; Divide AX by 10, save remainder in Stack
; count the remainder
; until AX != 0
; Pop the decimal numbers in stack
; convert them to ascii
; display the converted digit
;----------- Algorithm -----------
DEFINE_DEC_OUTPUT MACRO
JMP DECOUTPUT
;------------ Data section ----------
DO_Color db ?
DO_col db ? ; current col position
DO_row db ? ; current row position
;----------- Program begins --------
DECOUTPUT PROC
;This procedure take a number in AX and display it's decimal in next line
; Input: AX = number, DX = row, col ,BX = page/color
; Output: None
; Uses : AX, BX, CX, DX
; Destroys: Nothing, at the end all are same as before except cursor position
;save BX and DX and CX and Color
PUSH DX
PUSH BX
PUSH CX
; also save the original number
PUSH AX
; save row and column position
MOV DO_row , DH
MOV DO_col , DL
; save the system color profile
MOV DO_Color, BL
; clear CX for use as counter
XOR CX, CX
; Check if AX is negative
OR AX, AX
; if not negative , jump to conversion
JNS DO_Convert
; else , print a negative (-) sign
; first save the number
push ax
MOV AH, 0EH
MOV AL, '-'
INT 10H
POP AX
; two's complement it, to get the positive
NEG AX
; Convert the number in AX to decimal and put in stack
DO_Convert:
; Divide AX by 10, save remainder in AH in stack
; clear high word for dividend
XOR DX, DX
; save the color profile before using Bx as divisor
PUSH BX
; load divisor
MOV BX ,10
; DX = remainder, AX = qoutient
DIV BX
; restore the color profile after using it as divisor
POP BX
; save the remainder
PUSH DX
; increase digit counter
INC CX
; if AX = 0 , stop conversion
CMP AX, 0
; and go to display it
JE DO_Display
; else repeat until AX = 0
JMP DO_Convert
DO_Display:
; go to new line to print it
; restore the row and col
MOV DH, DO_row
MOV DL, DO_col
; go at new line lef
INC DH
MOV DL, 0
; move cursor function
MOV AH, 2
int 10h
DO_Display_loop:
; Display the digits in stack
POP AX
; convert to ascii
OR AL, 30H
; display it with attribute
; attributed character display function
MOV AH, 09H
; AL has the character
; BH is current page
MOV BL, DO_Color
; we will use cx, which holding the digit count, so save it
push cx
; only one times
MOV CX, 1
; call interrupt
INT 10H
; restore cx and decrease it 1
pop cx
INC DL
; cursor move function
MOV AH, 2
INT 10H
; loop until all digit is displayed
LOOP DO_Display_loop
DO_EXIT:
POP AX
POP CX
POP BX
POP DX
RET
DECOUTPUT ENDP
DEFINE_DEC_OUTPUT ENDM