-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorksheet8-2_Success.asm
More file actions
142 lines (112 loc) · 2.77 KB
/
Worksheet8-2_Success.asm
File metadata and controls
142 lines (112 loc) · 2.77 KB
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
TITLE ASM 32-bit Template
; Description: Worksheet 8, number 2
; Author: Amanda Steidl
; Date: Nov. 8, 2014
INCLUDE Irvine32.inc
.data
outMsg BYTE "Your fraction reduces to: ",0
welcomeMsg BYTE "Welcome to the Fraction Reducer!" , 0
MsgInput1 BYTE "Please input the numerator positive integer(at most a 16-bit): ", 0
userInput1 WORD ?
MsgInput2 BYTE "Please input the denominator at most positive integer(at most a 16-bit): ", 0
userInput2 WORD ?
denError BYTE "ERROR: You cannot divide by 0! Answer is undefined!",0
numError BYTE "Zero divided by any number is 0" , 0
.code
main PROC
Call ClrScr
MOV EDX, OFFSET welcomeMsg
Call WriteString
Call Crlf
MOV EDX, OFFSET MsgInput1
Call WriteString
Call ReadDec
MOV userInput1, AX
MOV EDX, OFFSET MsgInput2
Call WriteString
Call ReadDec
MOV userInput2, AX
MOV BX, AX ;BX now has second input
MOV AX, userInput1 ;AX now has first input
;----------------------------
;"Idiot-Proofing" comparisons
;----------------------------
;Check if the numerator is 0... answer will be zero
CMP AX, 0
JE displayNumError
;Check if the denominator is 0... answer is undefined
CMP BX, 0
JNE compareValue
;"ERROR" messages
MOV EDX, OFFSET denError
Call WriteString
Call Crlf
JMP exit_
displayNumError:
MOV EDX, OFFSET numError
Call WriteString
Call Crlf
JMP exit_
;----------------------------
;Set-Up Proc
;----------------------------
compareValue:
MOV EDX, OFFSET outMsg
CMP AX, BX
JAE startProcInput2
MOVZX ECX, AX
JMP proc_
startProcInput2:
MOVZX ECX, BX
proc_:
Call reduceFraction
exit_:
EXIT
main ENDP
;----------------------------------------------------------------------------
;Description: Reduces fraction if needed. Outputs.
;Recieves: Counter for loop in ECX, AX contains user input1, BX contains user input 2
; Offset outMsg EDX
;Returns: nothing
;----------------------------------------------------------------------------
reduceFraction PROC
PUSH EDI
PUSH ESI
PUSH EDX
L1:
MOV DX, 0
;AX currently has userInput1
PUSH EAX ;will save input1
DIV CX
CMP DX, 0
MOV SI, AX ;saves the quotient of first in SI
POP EAX
JNE endLoop
MOV DX, 0
PUSH EAX ;will save input1
MOV AX, BX
DIV CX
CMP DX, 0
MOV DI, AX ;saves the quotient of second in DI
POP EAX ;returns value input1 into AX
JE output
;if not equal, move correct output
MOV SI, AX ;input value 1
MOV DI, BX ;input value 2
endLoop:
LOOP L1
output:
POP EDX
Call WriteString
MOV AX, SI
Call WriteDec
MOV AL, 2Fh
Call WriteChar
MOV AX, DI
Call WriteDec
Call Crlf
POP ESI
POP EDI
RET
reduceFraction ENDP
end main