汇编学习7

汇编计算器练习

今天我用汇编语言搞了一个超级丐版的计算器小程序 🤓,算是对自己这段时间学习成果的一个小小检验。说实话,整个过程还挺刺激的 😂,尤其是在那堆汇编指令里找路的时候,感觉自己就像是在迷宫里找出口 🌀,满头问号但是心中带着希望 🤔✨。

好消息是,大部分功能居然都还挺顺利的 🎉,啥加减乘除的基本运算竟然没把我逼疯 🤯。坏消息呢?嘿嘿,我在输出结果的部分,碰到了个让人想摔键盘的小问题 😵‍💫。每次我期待着屏幕上弹出一个光鲜亮丽的答案时,结果,那个可恶的运算符号也会大摇大摆地跟着答案一起显示出来,就像是它也想在这场演出里蹭点镜头 😂。我在心里想:“喂,哥们儿,你的戏份已经结束了,快下去吧!” 👋但它偏偏不下。

试想一下,当我想要计算个 “3 + 5” 的时候,程序给我输出了个 “3 + 5 = 8+”。没错,就是这样,那个运算符号像块牛皮糖一样,甩都甩不掉 🤦‍♂️。我感觉这个输出结果在嘲笑我:“嘿,你不是学汇编很厉害吗?那你试试搞定我呀?” 😏

估计我在某个代码的角落里,不小心让这个符号爱上了结果 ❤️,所以它们就总是一起出场。现在问题就是,我怎么把它们给拆散?有没有哪位大佬能来帮我康康 🙏,让这个多余的符号从输出中安静地退下?拜托啦,我的计算器可是想要走向丐版巅峰的 🏔️,不想再被这多余的符号给拖累了!

有任何建议都欢迎留言,别让我和这个鬼符号纠缠太久了,真的有点心累啊,哈哈哈 😂😩!

汇编代码

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
207
section .data
prompt1 db 'Enter first num: ', 0
prompt2 db 'Enter second num: ', 0
prompt_op db 'Enter operator(+, -, *, /)', 0
result_msg db 'Result: ', 0
newline db 0xA, 0

section .bss
num1 resb 16
num2 resb 16
num_str resb 16
operator resb 16
result resb 16

section .text
global _start

_start:
mov eax, 4
mov ebx, 1
mov ecx, prompt1
mov edx, 18
int 0x80

mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 16
int 0x80

mov esi, num1
call remove_newline
mov esi, num1
call str_to_int
mov [result], eax

mov eax, 4
mov ebx, 1
mov ecx, prompt2
mov edx, 19
int 0x80

mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 16
int 0x80

mov esi, num2
call remove_newline
mov esi, num2
call str_to_int
push eax

mov eax, 4
mov ebx, 1
mov ecx, prompt_op
mov edx, 27
int 0x80

mov eax, 3
mov ebx, 0
mov ecx, operator
mov edx, 16
int 0x80

mov esi, operator
call remove_newline

mov al, [operator]
cmp al, '+'
je do_addition
cmp al, '-'
je do_subtraction
cmp al, '*'
je do_multiplication
cmp al, '/'
je do_division

jmp end_program

do_addition:
xor eax, eax
pop ebx
mov eax, [result]
add eax, ebx
mov [result], eax
jmp output_result

do_subtraction:
xor eax, eax
pop ebx
mov eax, [result]
sub eax, ebx
mov [result], eax
jmp output_result

do_multiplication:
xor eax, eax
pop ebx
mov eax, [result]
xor edx, edx
mul ebx
mov [result], eax
jmp output_result

do_division:
xor eax, eax
pop ebx
cmp ebx, 0
je de
mov eax, [result]
xor edx, edx
div ebx
mov [result], eax
jmp output_result

de:
jmp end_program

output_result:
mov eax, 4
mov ebx, 1
mov ecx, result_msg
mov edx, 8
int 0x80

mov eax, [result]
call int_to_str

mov eax, 4
mov ebx, 1
mov ecx, edi
mov edx, 8
int 0x80

mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80

end_program:
mov eax, 1
xor ebx, ebx
int 0x80

str_to_int:
xor eax, eax
xor ebx, ebx
str_to_int_loop:
mov bl, [esi]
cmp bl, 0
je str_to_int_end

sub bl, '0'
imul eax, eax, 10
add eax, ebx

inc esi
jmp str_to_int_loop
str_to_int_end:
ret

int_to_str:
mov edi, num_str
add edi, 15
mov byte [edi], 0
dec edi

mov ecx, 10
xor edx, edx

convert_loop:
xor edx, edx
div ecx
add dl, '0'
cmp dl, '0'
jl convert_loop
cmp dl, '9'
jg convert_loop
mov [edi], dl
dec edi
test eax, eax
jnz convert_loop

inc edi
ret

remove_newline:
remove_newline_loop:
mov al, [esi]
cmp al, 0
je remove_newline_end

cmp al, 0xA
jne continue_loop

mov byte [esi], 0
jmp remove_newline_end

continue_loop:
inc esi
jmp remove_newline_loop

remove_newline_end:
ret