-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_number.asm
More file actions
170 lines (127 loc) · 2.38 KB
/
input_number.asm
File metadata and controls
170 lines (127 loc) · 2.38 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
%macro pushReg 0
push eax
push ebx
push ecx
push edx
%endmacro
%macro popReg 0
pop edx
pop ecx
pop ebx
pop eax
%endmacro
%macro print 2
push eax
push ebx
push ecx
push edx
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 0x80
pop edx
pop ecx
pop ebx
pop eax
%endmacro
%macro exit 1
mov eax, 1
mov ebx, %1
int 0x80
%endmacro
%macro scan 2
pushReg
mov eax, 3
mov ebx, 0
mov ecx, %1
mov edx, %2
int 0x80
popReg
%endmacro
%macro ascii_to_int 2
pushReg
push esi
push edi
mov esi, %1
mov edi, %2
xor eax, eax
%xdefine cnt %[counter]
inc dword [counter]
.iterate%[cnt]:
mov cl, byte [esi]
cmp cl, 0xa
je .iterate_end%[cnt]
cmp cl, 0x0
je .iterate_end%[cnt]
cmp cl, '0'
jl .invalid_input%[cnt]
cmp cl, '9'
jg .invalid_input%[cnt]
sub cl, '0'
mov ebx, 10
mul ebx
add eax, ecx
inc esi
jmp .iterate%[cnt]
.invalid_input%[cnt]:
print err_msg, err_msg_len
print newline, newline_len
exit 1
.iterate_end%[cnt]:
add dword [edi], eax
push edi
push esi
popReg
%endmacro
%macro int_to_ascii 3
pushReg
push esi
push edi
; number %1, buffer %2
mov edi, %2 + 9
mov byte [edi], 0
mov eax, [%1]
mov ebx, 10
%xdefine iterator_label iterate%3
iterator_label:
xor edx, edx
div ebx
add dl, '0'
dec edi
mov byte [edi], dl
test eax, eax
jnz iterator_label
mov edx, %2 + 10
sub edx, edi
print edi, edx
pop edi
pop esi
popReg
%endmacro
section .data
msg db "Enter Any Number: "
msg_len equ $-msg
err_msg db "You didn't enter valid number."
err_msg_len equ $-err_msg
newline db 0xa
newline_len equ 1
section .bss
ipbuffer resd 10
opbuffer resd 10
op resd 1
counter resd 1
section .text
global _start
_start:
mov dword [op], 0
mov dword [counter], 0
print msg, msg_len
scan ipbuffer, 10
ascii_to_int ipbuffer, op
add dword [op], 1
int_to_ascii op, opbuffer, 10
; print opbuffer, 10
; print op, 1
print newline, newline_len
exit 0