-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.asm
210 lines (162 loc) · 3.54 KB
/
calc.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
207
208
209
210
; ---------------------------------------------------------
; Programa: calculator.asm
; Autor: Jorge Macias
; Descripci??n: Two number calculator in 16-bit DOS assembly.
; Example of a program that displays
; the message "Hello world!" on screen.
; ---------------------------------------------------------
.model small
.stack 100h
.data
menu_message db 'Choose an option:', 0ah, '1. Add', 0ah, '2. Subtract', 0ah, '3. Multiply', 0ah, '4. Divide', 0ah, '5. Exit', 0ah, '$'
first_number db 0
second_number db 0
result db 0
message_first_number db 10, 13, 'Enter first number: $'
message_second_number db 10, 13, 'Enter second number: $'
message_result db 10, 13, 'Result is: $'
invalid_choice db 10, 13, 'Invalid option! choose another'
menu_option db ?
.code
start:
; Code section begins here
; To understand syscalls and services to achieve stuff
; you can read it here: https://en.wikipedia.org/wiki/DOS_API#DOS_INT_21h_services
; Configure data segments
mov ax, @data
mov ds, ax
; Print menu to the screen
mov ah, 9
lea dx, menu_message
int 21h
; Menu option input
mov ah, 1 ; Character input to the ah register
int 21h ; Make the DOS syscall
sub al, '0'
mov menu_option, al
; Go to a different operation depending of the integer selected
; use cmp -> compare to make an 'if' statement
cmp menu_option, 1
je sum
cmp menu_option, 2
je res
cmp menu_option, 3
je mult
cmp menu_option, 4
je divi
jmp invalid_option
sum:
mov ah, 9
lea dx, message_first_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov first_number, al
mov ah, 9
lea dx, message_second_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov second_number, al
mov al, first_number
add al, second_number
add al, 30h
mov result, al
mov ah, 9
lea dx, message_result
int 21h
mov ah, 2
mov dl, result
int 21h
jmp exit
res:
mov ah, 9
lea dx, message_first_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov first_number, al
mov ah, 9
lea dx, message_second_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov second_number, al
mov al, first_number
mov bl, second_number
mov al, first_number
sub al, second_number
add al, 30h
mov result, al
mov ah, 9
lea dx, message_result
int 21h
mov ah, 2
mov dl, result
int 21h
jmp exit
mult:
mov ah, 9
lea dx, message_first_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov first_number, al
mov ah, 9
lea dx, message_second_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov second_number, al
mov al, first_number
mul al, second_number
add al, 30h
mov result, al
mov ah, 9
lea dx, message_result
int 21h
mov ah, 2
mov dl, result
int 21h
jmp start
divi:
mov ah, 9
lea dx, message_first_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov first_number, al
mov ah, 9
lea dx, message_second_number
int 21h
mov ah, 1
int 21h
sub al, 30h
mov second_number, al
mov al, first_number
div al, second_number
add al, 30h
mov result, al
mov ah, 9
lea dx, message_result
int 21h
mov ah, 2
mov dl, result
int 21h
invalid_option:
mov ah, 9
lea dx, invalid_choice
int 21h
jmp exit
; Exit from the program
exit:
mov ah, 4ch ; Function to finish the program
int 21h ; Make DOS syscall
end start