-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.asm
154 lines (116 loc) · 1.66 KB
/
calculator.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
segment .data
input1 db 'Enter a number',0xA,0xD
len1 equ $- input1
input2 db 'Enter a second number',0xA,0xD
len2 equ $- input2
choice db 'add(0), subtract(1), divide(2) or multiply(3)',0xA,0xD
lenC equ $- choice
output db 'output is ',0xA,0xD
len3 equ $- output
segment .bss
num1 resb 2
num2 resb 2
c resb 2
res resb 2
section .text
extern printFunction
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, input1
mov edx, len1
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 4
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, input2
mov edx, len2
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 4
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, choice
mov edx, lenC
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, c
mov edx, 2
int 0x80
mov ah, [c]
sub ah, '0'
cmp ah, 0
JE sum_values
cmp ah, 1
JE sub_values
cmp ah, 2
JE divide_values
cmp ah, 3
JE multiply_values
int 0x80
sum_values:
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
add al, bl
add al, '0'
mov eax, 4
mov ebx, 1
mov ecx, res
mov edx, 2
int 0x80
jmp exit
sub_values:
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
sub al, bl
add al, '0'
mov [res], al
push res
call printf
jmp exit
divide_values:
mov ah, 0
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
div bl
add ax, '0'
mov [res], ax
mov eax, 4
mov ebx, 1
mov ecx, res
mov edx, 2
int 0x80
jmp exit
multiply_values:
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
mul bl
add al, '0'
mov [res], al
mov eax, 4
mov ebx, 1
mov ecx, res
mov edx, 2
int 0x80
jmp exit
exit:
mov eax, 1
mov ebx, 0
int 0x80