-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.asm
203 lines (162 loc) · 4.06 KB
/
source.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
.386
.model flat, stdcall
option casemap:none
includelib msvcrt.lib
printf PROTO C :ptr sbyte, :VARARG
scanf PROTO C :ptr sbyte, :VARARG
strlen PROTO C :ptr sbyte, :VARARG
.data
; Process input numbers in char
input byte "%s", 0
input_question_1 byte "Input the 1st number: ", 0
input_question_2 byte "Input the 2nd number: ", 0
; Output final result in char
output byte "%s * %s = %s", 0ah, 0
; Big integers to calculate (Read in char)
num_char_1 byte 100 dup (0)
num_char_2 byte 100 dup (0)
result_char byte 200 dup (0)
; Each digit: "char to int"
num_int_1 dword 100 dup(0)
num_int_2 dword 100 dup(0)
result_int dword 200 dup(0)
; Length of numbers to multiply
length_1 dword 0
length_2 dword 0
length_res dword 0
; Decimal input numbers
radix dword 10
.code
; Convert each digit to int and push to stack (Reversed)
; Convert "sbyte" string to "dword" int
convert_to_int proc far C num_char :ptr byte, num_int :ptr dword, len :dword
mov ecx, len
mov esi, num_char
L1:
movzx eax, byte ptr [esi]
sub eax, 30h
push eax
inc esi
loop L1
mov ecx, len
mov esi, num_int
L2:
pop eax
mov dword ptr [esi], eax
add esi, 4
loop L2
ret
convert_to_int endp
; Get each digit out of stack to get ready for printing (Reversed)
; Convert "dword" int to "sbyte" string
convert_to_char proc far C uses eax ecx esi
mov ecx, length_res
mov esi, 0
L1:
mov eax, dword ptr result_int[esi * 4]
add eax, 30h
push eax
inc esi
loop L1
mov ecx, length_res
mov esi, 0
L2:
pop eax
mov byte ptr result_char[esi], al
inc esi
loop L2
ret
convert_to_char endp
; Get number 1 length
get_num1_len proc far C uses eax
invoke strlen, offset num_char_1
mov length_1, eax
invoke convert_to_int, offset num_char_1, offset num_int_1, length_1
ret
get_num1_len endp
; Get number 2 length
get_num2_len proc far C uses eax
invoke strlen, offset num_char_2
mov length_2, eax
invoke convert_to_int, offset num_char_2, offset num_int_2, length_2
ret
get_num2_len endp
; Big integer multiply
big_int_multiply proc far C uses eax ebx ecx esi
mov ebx, -1
First_Num_Loop:
inc ebx
cmp ebx, length_1
; Loop end if reaches end of first number
jnb First_Loop_End
xor ecx, ecx
Second_Num_Loop:
xor edx, edx
; Multiply two digits and put result in EAX
mov eax, dword ptr num_int_1[4 * ebx]
mul num_int_2[4 * ecx]
; Put value in EAX in result's position accordingly
mov esi, ecx
add esi, ebx
add result_int[4 * esi], eax
; Loop ends if: ecx reaches end of second number
inc ecx
cmp ecx, length_2
jnb First_Num_Loop
; Else: continue second number loop
jmp Second_Num_Loop
First_Loop_End:
mov ecx, length_1
add ecx, length_2
inc ecx
mov esi, offset length_res
; Get result's length (Value of ECX)
mov [esi], ecx
xor ebx, ebx
Calc_Carry:
; If no carries left, carry loop ends
cmp ebx, ecx
jnb Second_Loop_End
mov eax, result_int[ebx * 4]
; result_int[ptr + 1] = result[ptr] + result[ptr] / 10
xor edx, edx
div radix
add result_int[ebx * 4 + 4], eax
; result_int[ptr] = result[ptr] % 10
mov result_int[ebx * 4], edx
inc ebx
jmp Calc_Carry
; Check for leading zeros:
; (From the end of the result, because result is reversed.)
Second_Loop_End:
mov ecx, length_res
Trim_Zero:
cmp dword ptr result_int[ecx * 4], 0
jnz End_Loop
dec ecx
jmp Trim_Zero
End_Loop:
inc ecx
mov esi, offset length_res
mov [esi], ecx
; Convert result_int (dword) to reversed result_char (byte) for result printing
invoke convert_to_char
ret
big_int_multiply endp
main proc
; Input the first number
invoke printf, offset input_question_1
invoke scanf, offset input, offset num_char_1
; Input the second number
invoke printf, offset input_question_2
invoke scanf, offset input, offset num_char_2
; Get length of input numbers, and convert each digit to dword stack
invoke get_num1_len
invoke get_num2_len
; Multiplication!
invoke big_int_multiply
; Print final result
invoke printf, offset output, offset num_char_1, offset num_char_2, offset result_char
ret
main endp
end main