Uploaded by VI VICTORVICH

Assignment 7 2.docx.docx

advertisement
Matthew Weshner
Assignment 7
Write an assembly program to create a calculator that repeatedly takes a
basic math operator (+, -, *, /), and two numbers all from user input, and
have it print the result of the mathematical operation. The mathematical
operations should be wrapped inside of procedures.
The two input numbers should be passed to the procedure either using
registers or stack (your choice), the return result should be returned and
printed in the caller procedure.
The program should stop when user enters e
Note : You need to submit the source code and output
; Calculator
;
; 11/06/2020
.586
.MODEL FLAT
INCLUDE
io.h
; header file for input/output
.STACK 4096
.DATA
num1
num2
prompt1
pNum1
pNum2
result
string
sum
DWORD ?
DWORD ?
BYTE "Enter the mathmatical operator (+, -, *, /)", 0
BYTE "Enter first number", 0
BYTE "Enter second number", 0
BYTE "Result is", 0
BYTE 40 DUP (?)
BYTE 11 DUP (?), 0
.CODE
_MainProc PROC
startLoop:
input
prompt1, string, 2
character
;get user input and take only the first
cmp
string, '+' ;compare user input to '+'
je
addition
;
i
f
u
s
e
r
i
n
p
u
t
w
a
s
'
+
'
t
h
e
n
j
u
m
p
t
o
a
d
d
i
t
i
o
n
cmp
je
string, '-' ;see above
subtraction
cmp
je
string, '*'
multiply
This study source was downloaded by 100000854915659 from CourseHero.com on 10-22-2022 13:00:32 GMT -05:00
https://www.coursehero.com/file/76054305/Assignment-7-2docx/
Matthew Weshner
cmp
je
string, '/'
divide
cmp
je
string, 'e'
endLoop
;allows the user to end the program by entering 'e'
;if no valid option is entered, loop back to the
startLoop
start
jmp
addition:
input
atod
mov
pNum1, string, 40
;prompt the user for the first number
string
num1, eax
;store first number as num1
input
atod
mov
pNum2, string, 40
string
num2, eax
mov
eax, num1
mov
add
dtoa
ebx, num2
eax, ebx
sum, eax
jmp
subtraction
:
input
atod
mov
display
;load num1 and num2 into registers to prepare for
math
;adding num1 to num2
;store results as sum
;jump to display, where the program displays the
result
pNum1, string, 40
string
num1, eax
input
atod
mov
pNum2, string, 40
string
num2, eax
mov
mov
sub
dtoa
eax,
ebx,
eax,
sum,
jmp
display
num1
num2
ebx
eax
;same code as addition except subtraction
multiply:
input
atod
mov
pNum1, string, 40
string
num1, eax
input
atod
mov
pNum2, string, 40
string
num2, eax
mov
mov
mul
dtoa
eax, num1
ebx, num2
ebx
sum, eax
jmp
display
divide:
input
;prompt for second number
;same code as addition except multiplication
pNum1, string, 40
This study source was downloaded by 100000854915659 from CourseHero.com on 10-22-2022 13:00:32 GMT -05:00
https://www.coursehero.com/file/76054305/Assignment-7-2docx/
Matthew Weshner
atod
mov
string
num1, eax
input
atod
mov
pNum2, string, 40
string
num2, eax
mov
mov
ebx, num1
eax, num2
cdq
idiv
dtoa
ebx
sum, eax
jmp
display
display:
output
jmp
endLoop:
mov
ret
;convert double to quad because im using the big
register
;signed division for negative numbers
result, sum ;output the result of math
startLoop
eax, 0
_MainProc ENDP
END
; end of source code
This study source was downloaded by 100000854915659 from CourseHero.com on 10-22-2022 13:00:32 GMT -05:00
https://www.coursehero.com/file/76054305/Assignment-7-2docx/
Matthew Weshner
This study source was downloaded by 100000854915659 from CourseHero.com on 10-22-2022 13:00:32 GMT -05:00
https://www.coursehero.com/file/76054305/Assignment-7-2docx/
Powered by TCPDF (www.tcpdf.org)
Download