-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdisplay_decimal.asm
More file actions
93 lines (79 loc) · 2.94 KB
/
display_decimal.asm
File metadata and controls
93 lines (79 loc) · 2.94 KB
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
; =============================================================================
; TITLE: Display Decimal Representation
; DESCRIPTION: Converts a 16-bit integer into its Decimal (Base 10) ASCII
; string using repeated division logic.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
; -----------------------------------------------------------------------------
; DATA SEGMENT
; -----------------------------------------------------------------------------
.DATA
TEST_VAL DW 12345 ; Example Value
MSG_OUT DB "Decimal Output: $"
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; --- Step 1: Initialize DS ---
MOV AX, @DATA
MOV DS, AX
; --- Step 2: Display Header ---
LEA DX, MSG_OUT
MOV AH, 09H
INT 21H
; --- Step 3: Print Decimal ---
MOV AX, TEST_VAL
CALL PRINT_DEC_PROC
; --- Step 4: Exit ---
MOV AH, 4CH
INT 21H
MAIN ENDP
; -----------------------------------------------------------------------------
; PROCEDURE: PRINT_DEC_PROC
; Input: AX (Value to print)
; -----------------------------------------------------------------------------
PRINT_DEC_PROC PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV CX, 0 ; Digit Counter
MOV BX, 10 ; Divisor
L_DIV_LOOP:
XOR DX, DX ; Clear High Word for Division
DIV BX ; AX = Quotient, DX = Remainder (0-9)
PUSH DX ; Push Remainder to Stack
INC CX ; Count Digits
CMP AX, 0 ; Quotient 0?
JNE L_DIV_LOOP ; No, continue dividing
L_PRINT_LOOP:
POP DX ; Pop Last Remainder (Most Significant Digit)
ADD DL, '0' ; ASCII Conversion
MOV AH, 02H
INT 21H
LOOP L_PRINT_LOOP
POP DX
POP CX
POP BX
POP AX
RET
PRINT_DEC_PROC ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES & ARCHITECTURAL INSIGHTS
; =============================================================================
; 1. DECIMAL CONVERSION ALGORITHM:
; - Number modulo 10 gives the last digit.
; - Number divided by 10 removes the last digit.
; - Repeat until number is 0.
;
; 2. STACK USAGE:
; Division extracts digits in reverse order (ones, then tens...). Pushing
; them onto the stack and popping them allows printing in the correct
; order (LIFO).
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =