Tuesday, March 29, 2011

CISS 360 Programming Assignment 2

; Title: CISS 360 Programming Assignment 2
;------------------------------------------------------------
; Description: This program asks user to enter a number (n) and
; it computes the sum of 1^2 + 2^2 + 3^2 +....+ n^2 and displays
; the sum.
;------------------------------------------------------------
; Programmer: Prajwal Shrestha
; Professor: Ronald J. Hart
;------------------------------------------------------------
; Date/Time: Tuesday, February 01, 2011 8:11 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 signed integer value in parameter)
; put_u DWORD/Reg (Displays unsigned integer value in parameter)
; put_fp REAL4/Reg (Displays float value in parameter)
; get_i DWORD (Loads signed integer entered at keyboard into parameter)
; get_u DWORD (Loads unsigned 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 *
;****************************************************
prompt1 BYTE "Enter integer, n: ",0
prompt2 BYTE ", ",0
prompt3 BYTE " ",0
n DWORD 0
sum DWORD 0
value DWORD 0
sum_str BYTE "Sum of integer, n^2!: ",0
newline DWORD 10

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

_MainProc PROC ; start of main program

rerun_main:
mov sum, 0 ; moving 0 into sum so that when we rerun the instuctions then it won't have previous sum!
put_ch newline ; for the newline
put_ch newline ; for the newline
put_str prompt1 ; prompt for the integer value
get_i n ; user input saved into n DWORD memory location.
mov ecx, n ; moving the value of n into accumulator register ecx
mov eax, 1 ; moving 1 into eax

loop_it:
mov value, eax ; move eax into value
mov ebx, eax ; move value of eax into ebx
imul ebx, value ; multiply value with ebx and put it in ebx
add sum, ebx ; adding ebx to sum and putting it in sum
put_i sum ; prints out the sum
cmp ecx, 1 ; ecx = 1 ?
je indenting ; if yes, then goto indenting
put_str prompt2 ; indenting the output only

indenting:
put_str prompt3 ; special display out for last element in the sum for indenting only!

inc eax ; adding eax by 1
loop loop_it ; loops back to loop_it untill ecx = 0

put_ch newline ; for the newline
put_ch newline ; for the newline
put_str sum_str ; displaying the message declared in "sum_str"
put_i sum ; outputting the sum value onto screen
put_ch newline ; for the newline

main_done:

done ; display completion/do-again message
cmp eax, 0 ; EAX = 0?
je rerun_main ; yes, go run program again
xor eax, eax ; exit with return code 0
ret

_MainProc ENDP

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

proc_name PROC

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.