-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhappy_number_check.asm
More file actions
187 lines (156 loc) · 4.75 KB
/
Copy pathhappy_number_check.asm
File metadata and controls
187 lines (156 loc) · 4.75 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
; =============================================================================
; TITLE: Happy Numbers
; DESCRIPTION: Repeatedly replaces a number with the sum of the squares of its
; digits, and detects the cycle that shows it will never reach one.
; 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
SAMPLES DW 19, 20, 23, 4
HOWMANY EQU 4
SEP DB ' is $'
M_HAPPY DB 'happy', 0DH, 0AH, '$'
M_SAD DB 'not happy', 0DH, 0AH, '$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
START:
MOV AX, @DATA
MOV DS, AX
LEA SI, SAMPLES
MOV CX, HOWMANY
EACH_SAMPLE:
MOV BX, [SI]
PUSH CX
PUSH SI
MOV AX, BX
CALL PRINT_DECIMAL
LEA DX, SEP
MOV AH, 09H
INT 21H
; -------------------------------------------------------------------------
; A NUMBER IS UNHAPPY WHEN IT FALLS INTO A CYCLE, AND EVERY SUCH CYCLE
; PASSES THROUGH FOUR. TESTING FOR FOUR IS THEREFORE ENOUGH, AND NEEDS NO
; RECORD OF WHAT HAS BEEN SEEN BEFORE.
; -------------------------------------------------------------------------
HAPPY_LOOP:
CMP BX, 1
JE IS_HAPPY
CMP BX, 4
JE IS_SAD
CALL SUM_SQUARED_DIGITS ; BX in, BX out
JMP HAPPY_LOOP
IS_HAPPY:
LEA DX, M_HAPPY
JMP REPORT
IS_SAD:
LEA DX, M_SAD
REPORT:
MOV AH, 09H
INT 21H
POP SI
POP CX
ADD SI, 2
LOOP EACH_SAMPLE
MOV AH, 4CH
INT 21H
; -----------------------------------------------------------------------------
; SUM_SQUARED_DIGITS
;
; Replaces BX with the sum of the squares of its decimal digits.
; -----------------------------------------------------------------------------
SUM_SQUARED_DIGITS PROC
PUSH AX
PUSH CX
PUSH DX
PUSH DI
XOR DI, DI
MOV AX, BX
MOV CX, 10
SSD_LOOP:
XOR DX, DX
DIV CX ; DX = the lowest digit
PUSH AX ; The quotient, for the next pass
MOV AX, DX
MUL DX ; The digit squared
ADD DI, AX
POP AX
OR AX, AX
JNZ SSD_LOOP
MOV BX, DI
POP DI
POP DX
POP CX
POP AX
RET
SUM_SQUARED_DIGITS ENDP
; -----------------------------------------------------------------------------
; PRINT_DECIMAL
;
; Prints the unsigned value in AX as decimal, with no leading zeros.
; Every register it touches is restored, so a caller can rely on it.
; -----------------------------------------------------------------------------
PRINT_DECIMAL PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
XOR CX, CX ; How many digits have been stacked
MOV BX, 10
PD_DIVIDE:
XOR DX, DX ; DX:AX is the dividend, so clear DX
DIV BX ; AX = quotient, DX = this digit
PUSH DX ; Digits arrive lowest first
INC CX
OR AX, AX
JNZ PD_DIVIDE ; Keep going until the quotient is zero
PD_EMIT:
POP DX ; Unstacking reverses them into order
ADD DL, '0'
MOV AH, 02H
INT 21H
LOOP PD_EMIT
POP DX
POP CX
POP BX
POP AX
RET
PRINT_DECIMAL ENDP
; -----------------------------------------------------------------------------
; NEWLINE
;
; Moves to the start of the next line. DOS needs both characters: the return
; moves the cursor to column zero and the feed moves it down a line.
; -----------------------------------------------------------------------------
NEWLINE PROC
PUSH AX
PUSH DX
MOV DL, 0DH
MOV AH, 02H
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
POP DX
POP AX
RET
NEWLINE ENDP
END START
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. WHY FOUR IS THE ONLY TEST NEEDED:
; - Every unhappy number eventually enters the single cycle 4, 16, 37,
; - 58, 89, 145, 42, 20 and back to 4. Meeting any member is enough,
; - and four is the smallest.
; 2. THE PROCEDURE RETURNS IN BX:
; - BX is deliberately not saved, since it carries both the argument
; - in and the answer out.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =