Tuesday, March 29, 2011

CISS 360 Programming Assignment 1

; Title: CISS 360 Programming Assignment 1
;------------------------------------------------------------
; Description: This program asks the user; pennies, nickles,
; dimes, quarters, 50-cent pieces, dollars and
; puts it in appropriate 32-bit DWORDS. It
; calculates the total number of pennies so
; that we could use the idiv division operation
; to put the quotient ie. dollars in to eax
; register and then remainder ie. cents in to
; edx register of the CPU. It also calcuates
; the total number of coins on the process
; by making use of the same eax register to give
; the output in 32-bit memory slot called numCoins.
;------------------------------------------------------------
; Programmer: Prajwal Shrestha
; Professor: Ronald J. Hart
;------------------------------------------------------------
; Date/Time: Friday, January 21, 2011 12:58 PM
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h ; header file for console input/output
.STACK 4096 ; reserve 4096-byte (1024 DWORD) stack
;------------------------------------------------------------
; Input/Output Facility
;------------------------------------------------------------
;
; Macro Operand
;
; put_ch DWORD/Reg (Displays character in parameter)
;
; get_ch None (Loads character from keyboard into EAX)
;
; put_str BYTE (Displays string beginning at given address)
;
; put_i DWORD/Reg (Displays integer value in parameter)
;
; put_fp REAL4/Reg (Displays float value in parameter)
;
; get_i DWORD (Loads integer entered at keyboard into parameter)
;
; get_fp REAL4 (Loads float entered at keyboard into parameter)
;
; get_str BYTE (Loads string entered at keyboard into parameter)
;
;------------------------------------------------------------

.DATA ;****************************************************
;* Data Section *
;****************************************************
; defining constants
NICKLE EQU 5
DIME EQU 10
QUARTER EQU 25
HALF EQU 50
DOLLAR EQU 100

; prompt characters for user information
prompt1 BYTE "Enter Number of Pennies: ",0
prompt2 BYTE "Enter Number of Nickles: ",0
prompt3 BYTE "Enter Number of Dimes: ",0
prompt4 BYTE "Enter Number of Quarters: ",0
prompt5 BYTE "Enter Number of 50 Cent pieces: ",0
prompt6 BYTE "Enter Number of Dollars: ",0

; output characters for the users
displayInfo BYTE "||Coin Information||",0
displayInfo1 BYTE "--------------------",0
numCoin_str BYTE "Total Number of Coins = ",0
numPennies_str BYTE "Total Number of Pennies = ",0
valueDollar_str BYTE "Total Dollar Value = ",0
valueCent_str BYTE "Total Cent Value = ",0

; 32-bit memory locations user input
pennies DWORD ?
nickles DWORD ?
dimes DWORD ?
quarters DWORD ?
fiftyCentPieces DWORD ?
dollars DWORD ?

; 32-bit memory locations for calculations
totalDollar DWORD ?
totalCent DWORD ?
divisor DWORD 100
numCoins DWORD ?
numPennies DWORD ?

; to create new line
newline DWORD 10

.CODE ;****************************************************
;* Code Section *
;****************************************************

_MainProc PROC ; start of main program

;;-------------------------------------------------------
;; Prompt for input coin values
;;-------------------------------------------------------
put_str prompt1 ; prompt for number of pennies
get_i pennies ; get pennies into pennies DWORD
put_ch newline ; output newline

put_str prompt2 ; prompt for number of nickles
get_i nickles ; get nickles into nickles DWORD
put_ch newline ; output newline

put_str prompt3 ; prompt for number of dimes
get_i dimes ; get dimes into dimes DWORD
put_ch newline ; output newline

put_str prompt4 ; prompt for number of quarters
get_i quarters ; get quarters into quarters DWORD
put_ch newline ; output newline

put_str prompt5 ; prompt for number of 50 Cent pieces
get_i fiftyCentPieces ; get fiftyCentPieces into fiftyCentPieces DWORD
put_ch newline ; output newline

put_str prompt6 ; prompt for number of dollars
get_i dollars ; get dollars into dollars DWORD
put_ch newline ; output newline
;;-------------------------------------------------------
;; Calculations: Addition, multiplicaton
;;-------------------------------------------------------
mov eax, pennies ; load eax with integer pennies value
add numCoins, eax ; add value of eax register to numCoins
add numPennies, eax ; add value of eax register to numPennies

mov eax, nickles ; load eax register with nickles value
add numCoins, eax ; add value of eax to numCoins
imul eax, NICKLE ; eax = eax * NICKLE (5)
add numPennies, eax ; add value of eax register to numPennies

mov eax, dimes ; load eax register with dimes value
add numCoins, eax ; add value of eax to numCoins
imul eax, DIME ; eax = eax * DIME (10)
add numPennies, eax ; add value of eax register to numPennies

mov eax, quarters ; load eax register with quarters value
add numCoins, eax ; add value of eax to numCoins
imul eax, QUARTER ; eax = eax * QUARTER (25)
add numPennies, eax ; add value of eax register to numPennies

mov eax, fiftyCentPieces; load eax register with 50-cents value
add numCoins, eax ; add value of eax to numCoins
imul eax, HALF ; eax = eax * HALF (50)
add numPennies, eax ; add value of eax register to numPennies

mov eax, dollars ; load eax register with dollar value
add numCoins, eax ; add value of eax to numCoins
imul eax, DOLLAR ; eax = eax * DOLLAR (100)
add numPennies, eax ; add value of eax register to numPennies
;;-------------------------------------------------------
;; Calculation: Division
;;-------------------------------------------------------
mov eax, numPennies ; dividend to EAX (it was not necessary but still)
cdq ; prepare EDX for division
idiv divisor ; divide EDX:EAX by divisor
;;-------------------------------------------------------
;; Display Output: number of coins, dollar, cents
;;-------------------------------------------------------
put_ch newline ; output newline
put_str displayInfo1 ; display "--------------------" string
put_ch newline ; output newline
put_str displayInfo ; display "||Coin Information||" string
put_ch newline ; output newline
put_str displayInfo1 ; display "--------------------" string

put_ch newline ; output newline
put_ch newline ; output another newline

put_str numCoin_str ; display "Total Number of Coins = " string
put_i numCoins ; display total number of Coins
put_ch newline ; output newline
put_ch newline ; output another newline

put_str numPennies_str ; display "Total Number of Pennies = " string
put_i numPennies ; display total number of Pennies
put_ch newline ; output newline
put_ch newline ; output another newline

put_str valueDollar_str ; display "Total Number of Dollars = " string
put_i eax ; display total dollars ie. Quotient of two interegs value
put_ch newline ; output newline
put_str valueCent_str ; display "Total Number of Cents = " string
put_i edx ; displays total cents ie. Remainder after division of two integers
put_ch newline ; output newline


main_done:

done
xor eax, eax ; exit with return code 0
ret

_MainProc ENDP

;------------------------------------------------------------

proc_name PROC ; sample procedure

push ebp ; save base pointer
mov ebp, esp ; establish stack frame

;; push registers used by this proc

;; proc code inserted here

;; pop registers used by this proc

pop ebp ; restore base pointer
ret

proc_name ENDP

;----------------------------------------------------------------------

stat_flags MACRO

fstsw ax ; FPU status word to AX
sahf ; condition code bits to flags
ENDM

;----------------------------------------------------------------------

END ; end of source code

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.