Skip to content

Instantly share code, notes, and snippets.

@addisonhuddy
Last active August 29, 2015 14:07
Show Gist options
  • Save addisonhuddy/7b241270fabbe8b78077 to your computer and use it in GitHub Desktop.
Save addisonhuddy/7b241270fabbe8b78077 to your computer and use it in GitHub Desktop.
CS 271 Assignement 01
TITLE Assignment 01 (assignment01.asm)
; Author: Addison Huddy
; CS271 / Program 01 Date: Oct 7 2014
; Description: taking in two numbers from the user and calculating
; sum, difference, product, integer quotient, and remainder
INCLUDE Irvine32.inc
; (insert constant definitions here)
.data
welcomeMsg BYTE "Welcome to basic math with Addison Huddy",0
instructionsMsg BYTE "I will ask for two numbers and make some basic calculations",0
num1Msg BYTE "Enter in a number: ",0
num2Msg BYTE "Enter another number equal to or less than the first number: ",0
goodbyeMsg BYTE "Thanks and goodnight ",0
differenceMsg BYTE "The difference is ",0
sumMsg BYTE "The sum is ",0
productMsg BYTE "The product is ",0
quotientMsg BYTE "The quotient is ",0
remainderMsg BYTE "The remainder is ",0
quitMsg BYTE "Want to do some more math (type 1 to quit, 0 to contine): ",0
preciseMsg BYTE "The more precise quotient is ",0
hundred REAL8 100.0
; section to write data that the program doesn't know yet
.data?
num1 DWORD ? ;first integer to be entered by user
num2 DWORD ? ;first integer to be entered by user
sum DWORD ? ;sum num1 and num2
difference DWORD ? ;difference between num1 minus num2
product DWORD ? ;num1 times num2
quotient DWORD ? ;num1/num2
remainder DWORD ? ;the remaining amount of num1
quit DWORD ? ;if 1 is entered then the program quits
.code
main PROC
; welcome message
mov edx, OFFSET welcomeMsg
call WriteString
call CrLf
mov edx, OFFSET instructionsMsg
call WriteString
call CrLf
; large loop to allow the user to enter in two more numbers if they choose
.repeat
LGetNumbers:
; get number 1
mov edx, OFFSET num1Msg
call WriteString
call ReadInt
mov num1, eax
; get number 2
mov edx, OFFSET num2Msg
call WriteString
call ReadInt
mov num2, eax
; need to compare num1 and num2. Jumps to LGetNumbers if num2 is greater than num1
mov eax, num1
mov ebx, num2
cmp eax, ebx
jb LGetNumbers
; calculate sum
mov eax, num1
mov ebx, num2
add eax, ebx
mov sum, eax
; calculate difference
mov eax, num1
mov ebx, num2
sub eax, ebx
mov difference, eax
; calculate product
mov eax, num1
mov ebx, num2
mul ebx
mov product, eax
; calculate quotient
mov eax, num1
cdq ; changing eax to a qword (8 byte integer)
mov ebx, num2
div ebx
mov quotient, eax
mov remainder, edx ; the remainder is in edx after calling div. Tried is with ebx, and that is just num2 still
; display sum to the user
mov edx, OFFSET sumMsg
call WriteString
mov eax, sum
call WriteDec
call CrLf
; display difference to the user
mov edx, OFFSET differenceMsg
call WriteString
mov eax, difference
call WriteDec
call CrLf
; display product to the user
mov edx, OFFSET productMsg
call WriteString
mov eax, product
call WriteDec
call CrLf
; display quotient and remainder
mov edx, OFFSET quotientMsg
call WriteString
mov eax, quotient
call WriteDec
call CrLf
mov edx, OFFSET remainderMsg
call WriteString
mov eax, remainder
call WriteDec
call CrLf
; calculate and display complex floating-point quotient. To get the right percision: *100 -> integer -> /100
mov edx, OFFSET preciseMsg
call WriteString
fld num1
fdiv num2
fmul hundred
frndint
fdiv hundred
call WriteFloat
call CrLf
; display quit message
mov edx, OFFSET quitMsg
call WriteString
call ReadInt
mov quit, eax
.until quit == 1
; say goodbye to the user
mov edx, OFFSET goodbyeMsg
call WriteString
call CrLf
exit ; exit to operating system
main ENDP
; (insert additional procedures here)
END main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment