-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment1.asm
238 lines (183 loc) · 6.81 KB
/
assignment1.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
TITLE Assignment 1 (assignment1.asm)
;Author: Wyatt Whiting
;Course / Project ID: Project 1, CS271 Date: 10/02/20
;Description: displays title and instructions. Takes user input. Does some arithemetic and displays terminating message.
INCLUDE c:/Irvine/Irvine32.inc
.data
;numeric data.
;using DWORD becuase ReadInt interprets input as 32-bit value
;and we don't need to worry about negative values
usrInput1 DWORD ? ;first user input
usrInput2 DWORD ? ;second user input
sum DWORD ? ;sum of numbers
diff DWORD ? ;difference of numbers
prod DWORD ? ;product of numbers
quot DWORD ? ;quotient of numbers
remainder DWORD ? ;remainder of numbers
str1 \
BYTE "This string is quite long!",0
;strings and output literals
intro BYTE "Assignment 1 - CS271 - Fall 2020 - Wyatt D. Whiting", 0
instruct BYTE "Enter two integers when prompted. Note, your second input must neither exceed the first or be 0. ",0
prompt1 BYTE "Please enter your first integer: ",0
prompt2 BYTE "Please enter your second integer: ",0
farewell BYTE "Thank you for grading my program. I hope I earn a good score!",0
errorMsgZero BYTE "ERROR: the second value you entered is zero, which will cause a division by zero error. Please try again.",0
errorMsgVals BYTE "ERROR: the second value you entered is greater than the first. Please try again.",0
repeatQues BYTE "Would you like to run the program again? Enter 1 for yes, anything else for no: ",0
challenges BYTE "I have implemented challenges 1 and 2",0
;math symbols, etc...
plus BYTE " + ",0
minus BYTE " - ",0
times BYTE " * ",0
divide BYTE " / ",0
remain BYTE ", remainder ",0
equals BYTE " = ",0
.code
main PROC
;start with printing program introduction
;print assignment information and such
mov edx, OFFSET intro ;load intro's OFFSET into edx register
call WriteString ;print string
call CrlF ;carriage return
mov edx, OFFSET challenges ;load challenge's OFFSET into edx
call WriteString ;print string
call CrlF ;carriage return
mov edx, OFFSET instruct ;load instruct's OFFSET into edx
call WriteString ;print string
call CrlF ;carriage return
;prompt input from user and store data into declared variables
;this routine will also check the data is acceptable for the rest
;of the program logic
getUserInput:
call CrlF ;carriage return for formatting
;clear registers
;probably not necessary but w/e
mov eax, 0 ;reset eax
mov ebx, 0 ;reset ebx
;print prompt for first user input
mov edx, OFFSET prompt1 ;load prompt1's OFFSET into edx register
call WriteString ;print string
;take and store first user input
call ReadInt ;stores 32-bit value in eax
mov usrInput1, eax ;copy eax to usrInput1
;print prompt for second user input
mov edx, OFFSET prompt2 ;load prompt2's OFFSET into edx register
call WriteString ;print string
;take and store second user input
call ReadInt ;stores 32-bit value in eax
mov usrInput2, eax ;copy eax to usrInput2
call CrlF ;carriage return for formatting
;check the sizes of inputs are acceptable
;start by checking usrInput2 != 0
cmp usrInput2, 0 ;compare usrInput2 to 0
je errorZero ;jump to errorZero section if they are equal
;checking if usrInput1 < usrInput2
mov eax, usrInput1 ;copy usrInput1 into eax
mov ebx, usrInput2 ;copy usrInput2 into ebx
cmp eax, ebx ;compare inputs
jl errorVals ;jump if usrInput1 < usrInput2
jmp math ;if no errors are found, go ahead with calculations
;program label for errors
errorZero:
mov edx, OFFSET errorMsgZero ;copy errorMsgVals's OFFSET to edx
call WriteString ;print the message
call CrlF ;carriage return
jmp getUserInput ;jump back to user input prompting
errorVals:
mov eax, 0 ;reset eax
mov ebx, 0 ;reset ebx
mov edx, OFFSET errorMsgVals ;copy errorMsgVals's OFFSET to edx
call WriteString ;print the message
call CrlF ;carriage return
jmp getUserInput ;jump back to user input prompting
;control flow label for math calculations
;usrInputs must be verified before jump
math:
;addition
mov eax, usrInput1 ;load first input into eax
add eax, usrInput2 ;add usrInput2 to eax
mov sum, eax ;store in sum
;subtraction
mov eax, usrInput1 ;load first input into eax
sub eax, usrInput2 ;now eax = usrInput1 - usrInput2
mov diff, eax ;store difference
;multiplication
mov eax, usrInput1 ;load first input into eax
mov ebx, usrInput2 ;load second input into ebx
mul ebx ;multiply by usrInput2
mov prod, eax ;store product
;division
mov eax, usrInput1 ;load first input into eax
mov ebx, usrInput2 ;load second user input into ebx
div ebx ;perform divsion
mov quot, eax ;store quotient
mov remainder, edx ;store remainder
;print all the equations
;show sum equation
mov eax, usrInput1 ;a
call WriteDec
mov edx, OFFSET plus ;+
call WriteString
mov eax, usrInput2 ;b
call WriteDec
mov edx, OFFSET equals ;=
call WriteString
mov eax, sum ;c
call WriteDec
call CrlF
;show difference equation
mov eax, usrInput1 ;a
call WriteDec
mov edx, OFFSET minus ;-
call WriteString
mov eax, usrInput2 ;b
call WriteDec
mov edx, OFFSET equals ;=
call WriteString
mov eax, diff ;c
call WriteDec
call CrlF
;show product equation
mov eax, usrInput1 ;a
call WriteDec
mov edx, OFFSET times ;*
call WriteString
mov eax, usrInput2 ;b
call WriteDec
mov edx, OFFSET equals ;=
call WriteString
mov eax, prod ;c
call WriteDec
call CrlF
;show division equation
mov eax, usrInput1 ;a
call WriteDec
mov edx, OFFSET divide ;/
call WriteString
mov eax, usrInput2 ;b
call WriteDec
mov edx, OFFSET equals ;=
call WriteString
mov eax, quot ;c
call WriteDec
mov edx, OFFSET remain ;remainder
call WriteString
mov eax, remainder ;r
call WriteDec
call CrlF ;carriage return
call CrlF
;ask if use wants to repeat the process, take input
mov edx, OFFSET repeatQues ;load repeatQues' OFFSET into edx
call WriteString ;print the string
call ReadInt ;stores 32-bit value in eax
cmp eax, 1 ;compare stored input to decimal 1
je getUserInput ;if 1 was entered, return to user input
;print farewell
call CrlF ;carriage return
mov edx, OFFSET farewell ;load farewell's OFFSET to edx
call WriteString ;print string
call CrlF ;carriage return
exit
main ENDP
END main