-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdivisibility_by_nine.asm
207 lines (185 loc) · 3.56 KB
/
divisibility_by_nine.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
; Crie um programa que receba como entrada um número N (N < 10^100) e que,
; após a tecla “enter” ser pressionada, seu programa deverá
; informar se o número N digitado é divisível por 9 ou não (basta imprimir “Sim” ou “Não”).
org 0x7c00
jmp 0x0000:main
data:
string times 101 db 0
; calls
putchar:
mov ah, 0x0e
int 10h
ret
getchar:
mov ah, 0x00
int 16h
ret
delchar:
mov al, 0x08 ; backspace
call putchar
mov al, ' '
call putchar
mov al, 0x08 ; backspace
call putchar
ret
endl:
mov al, 0x0a ; line feed
call putchar
mov al, 0x0d ; carriage return
call putchar
ret
; var al, *si;
; lodsb(){
; al = *si;
; si++;
; }
; var al, *di;
; stosb(){
; *di = al;
; di++;
; }
prints: ; mov si, string
.loop:
lodsb ; bota character em al
cmp al, 0
je .endloop
call putchar
jmp .loop
.endloop:
ret
reverse: ; mov si, string
mov di, si
xor cx, cx ; zerar contador
.loop1: ; botar string na stack
lodsb
cmp al, 0
je .endloop1
inc cl
push ax
jmp .loop1
.endloop1:
.loop2: ; remover string da stack
cmp cl, 0
je .endloop2
dec cl
pop ax
stosb
jmp .loop2
.endloop2:
ret
tostring: ; mov ax, int / mov di, string
push di
.loop1:
cmp ax, 0
je .endloop1
xor dx, dx
mov bx, 10
div bx ; ax = 9999 -> ax = 999, dx = 9
xchg ax, dx ; swap ax, dx
add ax, 48 ; 9 + '0' = '9'
stosb
xchg ax, dx
jmp .loop1
.endloop1:
pop si
cmp si, di
jne .done
mov al, 48
stosb
.done:
mov al, 0
stosb
call reverse
ret
gets: ; mov di, string
xor cx, cx ; zerar contador
.loop1:
call getchar
cmp al, 0x08 ; backspace
je .backspace
cmp al, 0x0d ; carriage return
je .done
cmp cl, 100 ; string limit checker
je .loop1
stosb
inc cl
call putchar
jmp .loop1
.backspace:
cmp cl, 0 ; is empty?
je .loop1
dec di
dec cl
mov byte[di], 0
call delchar
jmp .loop1
.done:
mov al, 0
stosb
call endl
ret
stoi: ; mov si, string
xor cx, cx
xor ax, ax
.loop1:
push ax
lodsb
mov cl, al
pop ax
cmp cl, 0 ; check EOF(NULL)
je .endloop1
sub cl, 48 ; '9'-'0' = 9
mov bx, 10
mul bx ; 999*10 = 9990
add ax, cx ; 9990+9 = 9999
jmp .loop1
.endloop1:
ret
; number theory
mod: ; mov ax, num / mov bx, mod
xor dx, dx
div bx
xchg ax, dx
ret
main:
xor ax, ax
mov ds, ax
mov es, ax
mov di, string
call gets
mov si, string
call reverse
mov si, string
mov cx, 1 ; 10^n
xor ax, ax
.loop1:
push ax
lodsb
cmp al, 0
je .endloop1
; prev + (digit * 10^n)
xor ah, ah
sub al, 48 ; '9' - '0' = 9
mul cx ; digit * 10^n
pop bx
add ax, bx
mov bx, 9
call mod ; ax mod bx
push ax ; store result
; (10^(n+1)) mod 9
mov ax, cx
mov bx, 10
mul bx
mov bx, 9
call mod
mov cx, ax
pop ax ; restore result
jmp .loop1
.endloop1:
pop ax ; restore result
mov di, string
call tostring ; convert number mod 9 to string
mov si, string
call prints ; print number mod 9
times 510-($-$$) db 0
dw 0xaa55