Tuesday, March 29, 2011

CISS 360 Programming Assignment 3

; Title: CISS 360 Programming Assignment 3
;------------------------------------------------------------
; Description: This program searches a double word entered by
; a user in the list of DWORDs array of 14 elements and returns
; a position of the array element found in the array or else
; if not found, the return -1 and informs user that the
; element searched is not found!!
; It used stack and procedure to neatly perform the operation
; by saving the register to be used by the main proc as EAX only!!
;------------------------------------------------------------
; Programmer: Prajwal Shrestha
; Professor: Ronald J. Hart
;------------------------------------------------------------
; Date/Time: Wednesday, February 16, 2011 10:20 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 "SEARCH (Enter number): ", 0
prompt2 BYTE "FOUND (Position): ", 0
prompt3 BYTE "Element NOT FOUND!", 0
array DWORD 1, 3, 7, 5, 8, 10, 11, 15, 17, 18, 77, 55, 25, 22
total_element DWORD 14
user_input DWORD ?
newline DWORD 10

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

_MainProc PROC

rerun_main:


put_ch newline ; starts the newline for display puropses
put_str prompt1 ; asks user to Search the number
get_i user_input ; DWORD "user_input" has wat the user enters
put_ch newline ; starts the newline for display puropses

lea eax, array ; loading the effective address, ie. the first address of the array "array" into register eax
push eax ; 1st ELEMENT pushed: pushing eax to procedure that has the effective address for the array we have to search for user input

push total_element ; 2nd ELEMENT pushed: pushing total elements in the array to procedure so that we can perform calculations there

mov eax, user_input ; moving user input element to eax so that we can push it to stack for procedure
push eax ; 3rd ELEMENT pushed: pushing value to be searched into the stack procedure

call search_number ; calling search_number procedure
add esp, 12 ; restoring stack pointer back

cmp eax, -1 ; is the value is eax regsister -1?
je value_not_found ; if true, we know and goto value_not_found:
put_str prompt2 ; display found message
put_i eax ; displays the position of the array element found

jmp end_cmp ; goto end_cmp:

value_not_found:
put_i eax ; display the value of eax is -1 as the element is not found
put_ch newline ; starts the newline for display puropses
put_str prompt3 ; display message to inform element is not found
put_ch newline ; starts the newline for display puropses

end_cmp:
put_ch newline ; starts the newline for display puropses

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

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

search_number PROC

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

;; push registers used by this proc
push esi ; save esi
push ebx ; save ebx
push edx ; save edx

push ecx ; save ecx

mov esi, [ebp + 16] ; 1st pushed from main: the address of array from the main is inserted into the esi proc
mov ebx, [ebp + 12] ; 2nd pushed from main: the number of elements in the array passed from the main to ebx proc
mov edx, [ebp + 8] ; 3rd pushed from main: user input being passed from main to the edx proc

;; proc code
mov ecx, 1 ; accumulator register being used as counter by setting it to 1

search_loop:
mov eax, [esi] ; the first element of the array being moved to eax that was pointed by esi
cmp eax, edx ; eax = edx?
je search_found ; if true, goto search_found:

inc ecx ; increasing counter, ecx by one
add esi, 4 ; now moving to the next element of the array by incresing 4 to the frist element
cmp ecx, ebx ; ecx = ebx?
jng search_loop ; if ecx = ebx is true then goto search_not_found:

search_not_found:
mov eax, -1 ; displaying -1 if the searched element in the array is not found
jmp end_search_loop ; since we don't want our instruction to go to next line we force it to goto end_search_loop

search_found:
mov eax, ecx ; moving the value of counter "ecx" to eax so that we get the position of the array element found

end_search_loop:

;; pop registers used by this proc
pop esi ; restore esi
pop ebx ; restore ebx
pop edx ; restore edx
pop ecx ; restore ecx
pop ebp ; restore base pointer
ret

search_number ENDP

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

stat_flags MACRO

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

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

END ; end of source code

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

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

Thursday, March 17, 2011

This program determines whether or not a given string is a palindrome. - By Professor Ronald J. Hart

; Title: CISS 360 Demo Program
;------------------------------------------------------------
; Description: This program determines whether or not a given
; string is a palindrome.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Sunday, January 25, 2011 4:05 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 *
;****************************************************

prompt BYTE "Enter a string: ", 0
input_str BYTE 256 DUP(0)
palin_yes BYTE "Input string is a palindrome",0
palin_no BYTE "Input string is not a palindrome",0
no_input_str BYTE "String not entered",0
one_byte_str BYTE "String has only one byte",0

newline DWORD 10

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

_MainProc PROC

put_str prompt ; prompt user for input string
get_str input_str ; get the string
put_ch newline ; line space on display
mov ecx, 0 ; byte counter
lea edi, input_str ; store starting address of string

;; loop to count characters in string and to get address of last byte

loop_it:

cmp BYTE PTR [edi], 0 ; string terminator?
je stop_scan ; yes, end of scan
inc ecx ; count character
inc edi ; point at next character of string
jmp loop_it ; go check the next character

stop_scan:

cmp ecx, 0 ; bytes in input string?
je no_input ; no, go display appropriate message
cmp ecx, 1 ; yes, only one byte?
je one_byte ; yes, go display appropriate message
lea esi, input_str ; no, determine if input string is a palindrome
dec edi ; backup last address

next_bytes:

cmp esi, edi ; start address >= ending address?
jge yes_palin ; yes, input string is palindrome
mov bl, BYTE PTR [edi] ; set up to compare characters
cmp BYTE PTR [esi], bl ; characters equal?
jne no_palin ; no, not a palindrome
inc esi ; increment to point at next byte
dec edi ; decrement to point at preceding byte
jmp next_bytes ; go check next two bytes

yes_palin:

put_str palin_yes ; display input string is a palindrome
jmp main_done ; go finish program

no_palin:

put_str palin_no ; display input string is not a palindrome
jmp main_done ; go finish program

no_input:

put_str no_input_str ; tell user no string was input
jmp main_done ; go finish program

one_byte:

put_str one_byte_str ; tell user a one-character string was input

main_done:

put_ch newline ; display newline...
put_ch newline ; and another

done ; display "completion" message
xor eax, eax ; exit with return code 0
ret

_MainProc ENDP

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

END ; end of source code

This program demonstrates how a proc is called and how a proc accesses parameters passed to it. - By Professor Ronald J. Hart

; Title: CISS 360 Proc Demo 1
;------------------------------------------------------------
; Description: This program demonstrates how a proc is called
; and how a proc accesses parameters passed to it.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Thursday, January 27, 2011 4:05 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 *
;****************************************************
x DWORD ?
y DWORD ?
Sum DWORD ?

prompt_1 BYTE "Enter first integer: ",0
prompt_2 BYTE "Enter second integer: ",0
result BYTE "Sum is ",0

newline DWORD 10


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

_MainProc PROC



rerun_main:

put_str prompt_1 ; prompt for 1st integer value
get_i x ; get 1st integer value
put_str prompt_2 ; prompt for 2nd integer value
get_i y ; get 2nd integer value

push y ; push 2nd parameter
push x ; push 1st parameter

call Adder ; invocation: Adder (x, y)

add esp, 8 ; clean up stack
mov Sum, eax ; save sum (i.e., value "returned" by proc)
put_str result ; display result label
put_i Sum ; display sum
put_ch newline ; display newline
put_ch newline ; display another newline

done ; display completion message
xor eax, eax ; exit with return code 0
ret


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

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

Adder PROC ; sample procedure

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

;; push registers used by this proc N/A

;; proc code inserted here
mov eax, 0 ; initialize accumulator
add eax, DWORD PTR [ebp+8] ; add 1st parameter
add eax, DWORD PTR [ebp+12] ; add 2nd parameter

;; pop registers used by this proc N/A

pop ebp ; restore base pointer
ret

Adder ENDP

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

stat_flags MACRO

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

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

END ; end of source code

This program displays the the sum, difference, product, and quotient (with remainder) of two input integers. - By Professor Ronald J. Hart

; Title: CISS 360 Demo Program
;------------------------------------------------------------
; Description: This program displays the the sum, difference,
; product, and quotient (with remainder) of two
; input integers.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Tuesday, January 13, 2011 4: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 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 *
;****************************************************

prompt1 BYTE "Enter 1st integer: ",0
prompt2 BYTE "Enter 2nd integer: ",0

sum_str BYTE "Sum = ",0
diff_str BYTE "Difference = ",0
prod_str BYTE "Product = ",0
quot_str BYTE "Quotient = ",0
rem_str BYTE "Remainder = ",0

value1 DWORD ?
value2 DWORD ?
newline DWORD 10

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

_MainProc PROC ; start of main program

;;-------------------------------------------------------
;; Prompt for input integer values
;;-------------------------------------------------------
put_str prompt1 ; prompt for first integer value
get_i value1 ; get first integer value
put_ch newline ; output newline
put_ch newline ; output another newline
put_str prompt2 ; prompt for second integer value
get_i value2 ; get second integer value
put_ch newline ; output newline
put_ch newline ; output another newline
;;-------------------------------------------------------
;; Addition
;;-------------------------------------------------------
mov eax, value1 ; load EAX with first integer value
add eax, value2 ; add second integer value to EAX
put_str sum_str ; display "sum" string
put_i eax ; display sum of two integer values
put_ch newline ; output newline
put_ch newline ; output another newline
;;-------------------------------------------------------
;; Subtraction
;;-------------------------------------------------------
mov eax, value1 ; load EAX with 1st integer value
sub eax, value2 ; EAX = EAX - 2nd integer value
put_str diff_str ; display "difference" string
put_i eax ; display difference of two integer values
put_ch newline ; output newline
put_ch newline ; output another newline
;;-------------------------------------------------------
;; Multiplication
;;-------------------------------------------------------
mov eax, value1 ; load EAX with 1st integer value
imul eax, value2 ; EAX = EAX * 2nd integer value
put_str prod_str ; display "product" string
put_i eax ; display product of two integer values
put_ch newline ; output newline
put_ch newline ; output another newline
;;-------------------------------------------------------
;; Division
;;-------------------------------------------------------
mov eax, value1 ; dividend to EAX
cdq ; prepare EDX for division
idiv value2 ; divide EDX:EAX by divisor (value2)
put_str quot_str ; display "quotient" string
put_i eax ; display quotient of two integer values
put_ch newline ; output newline
put_str rem_str ; display "remainder" string
put_i edx ; display remainder after division of two integer values
;;-------------------------------------------------------
put_ch newline ; output newline
put_ch newline ; output newline
done ; macro to display "done" message and get any key
xor eax, eax ; exit with return code 0
ret ; return

_MainProc ENDP ; end of main program

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

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_flag MACRO

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

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

END ; end of source code

This program prompts for and obtains two integer values and then computes and displays the sum of the two input values By Professor Ronald J. Hart

; Title: CISS 360 Program Demo 2
;------------------------------------------------------------
; Description: This program prompts for and obtains two
; integer values and then computes and displays
; the sum of the two input values.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Thursday, January 13, 2011 4:07 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 *
;****************************************************

prompt1 BYTE "Enter 1st integer: ",0
prompt2 BYTE "Enter 2nd integer: ",0

result_str BYTE "Sum = ",0

value1 DWORD ?
value2 DWORD ?
newline DWORD 10

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

_MainProc PROC ; start of main program

put_str prompt1 ; prompt for first integer value
get_i value1 ; get first integer value
put_ch newline ; output newline
put_ch newline ; output another newline
put_str prompt2 ; prompt for second integer value
get_i value2 ; get second integer value
put_ch newline ; output newline
put_ch newline ; output another newline

mov eax, value1 ; load EAX with first integer value
add eax, value2 ; add second integer value to EAX

put_str result_str ; display "result" string
put_i eax ; display sum of two integer values

done ; macro to display "done" message and get any key
xor eax, eax ; exit with return code 0
ret ; return

_MainProc ENDP ; end of main program

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

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

Displays the alphabet forward and backwards and then displays a welcome message - By Professor Ronald J. Hart

; Title: CISS 360 Demo Program
;------------------------------------------------------------
; Description: This program displays the alphabet forwards
; and backwards and then displays a welcome
; message.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Tuesday, January 11, 2011 3:54 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 *
;****************************************************

asterisks BYTE "* * * * * * * * * * * * *",10,13,0

welcome BYTE "* Welcome to CISS 360! *",10,13,0

newline DWORD 10

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

_MainProc PROC ; start of main program

mov al, 'a' ; start with lowercase 'a'
mov ecx, 26 ; loop counter (num letter in alphabet)

do_forward:

put_ch eax ; display current character
inc eax ; add one to character value
loop do_forward ; go display next character
put_ch newline ; done, display newline
put_ch newline ; display another newline
dec eax ; decrement character value (now 'z')
mov ecx, 26 ; re-init loop counter

do_backward:

put_ch eax ; display current character
dec eax ; decrement character value
loop do_backward ; go display next character
put_ch newline ; done, display newline
put_ch newline ; display another newline
put_str asterisks ; display string of asterisks
put_str welcome ; display welcome message
put_str asterisks ; display another line of asterisks
put_ch newline ; display newline

done ; macro to display "done" message and get any key
xor eax, eax ; exit with return code 0
ret ; return

_MainProc ENDP ; end of main program

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

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

get_i routine given by Professor Ronald J. Hart

; Title: CISS 360 Programming Assignment
;------------------------------------------------------------
; Description: This program reveals the code necessary to
; convert a numeric string entered at the key-
; board to an integer value (essentially it's
; an implementation of the "get_i" function).
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Sunday, February 13, 2011 2:48 PM
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h ; header file for console input/output
.STACK 4096 ; reserve 4096-byte (1024 DWORD) stack

.DATA ;****************************************************
;* Data Section *
;****************************************************

prompt BYTE 'Enter a positive or negative integer: ',0
result BYTE 'You entered: ',0

newline DWORD 10

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

_MainProc PROC

put_str prompt ; prompt user for an integer value
call my_get_i ; invoke conversion routine
put_str result ; display label for result
put_i eax ; display integer value
put_ch newline ; display newline
put_ch newline ; and another

main_done:

done ; display "end-of_program" message
xor eax, eax ; exit with return code 0
ret

_MainProc ENDP

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

my_get_i PROC

push ebp ; save base pointer
mov ebp, esp ; establish stack frame
push ebx ; save registers used by this proc
push edx
xor edx, edx ; accumulate integer value in EDX
mov ebx, 1 ; EBX indicates sign (1 means positive, -1 means negative)
next_char:
get_ch ; input character to EAX
cmp al, ' ' ; al = space?
je next_char ; yes, bypass and get next character
cmp al, 9 ; no, al = tab?
je next_char ; yes, bypass and get next character
cmp al, '-' ; no, al = minus sign?
je negative ; yes, go set "negative" flag
cmp al, '+' ; no, al = plus sign?
je next_byte ; yes, go get next byte of input
cmp al, 10 ; no al = newline?
je fini ; end of input string, finish up
jmp check_digit ; go check byte for a digit
negative:
neg ebx ; make EBX negative (indicates negative value entered)
next_byte:
get_ch ; input next character to EAX
check_digit:
cmp al, 48 ; AL < ascii zero? jl fini ; yes, go finish up cmp al, 57 ; no, AL > ascii nine?
jg fini ; yes, go finish up
sub al, 48 ; no, convert ascii digit to integer
imul edx, 10 ; increase current value by power of ten
add edx, eax ; add in current integer byte
jmp next_byte ; go process next input character
fini:
mov eax, edx ; returned integer value in EAX
imul ebx ; set sign of result
pop edx ; restore registers used by this proc
pop ebx
pop ebp ; restore base pointer
ret

my_get_i ENDP

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

END ; end of source code

Proc Demo by Ronald J. Hart

; Title: CISS 360 Proc Demo 1
;------------------------------------------------------------
; Description: This program demonstrates how a proc is called
; and how a proc accesses parameters passed to it.
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Thursday, January 27, 2011 4:05 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 *
;****************************************************

x DWORD ?
y DWORD ?
Sum DWORD ?

prompt_1 BYTE "Enter first integer: ",0
prompt_2 BYTE "Enter second integer: ",0
result BYTE "Sum is ",0

newline DWORD 10

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

_MainProc PROC

put_str prompt_1 ; prompt for 1st integer value
get_i x ; get 1st integer value
put_str prompt_2 ; prompt for 2nd integer value
get_i y ; get 2nd integer value

push y ; push 2nd parameter
push x ; push 1st parameter

call Adder ; invocation: Adder (x, y)

add esp, 8 ; clean up stack
mov Sum, eax ; save sum (i.e., value "returned" by proc)
put_str result ; display result label
put_i Sum ; display sum
put_ch newline ; display newline
put_ch newline ; display another newline

done ; display completion message
xor eax, eax ; exit with return code 0
ret

_MainProc ENDP

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

Adder PROC ; sample procedure

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

;; push registers used by this proc N/A

;; proc code inserted here
mov eax, 0 ; initialize accumulator
add eax, DWORD PTR [ebp+8] ; add 1st parameter
add eax, DWORD PTR [ebp+12] ; add 2nd parameter

;; pop registers used by this proc N/A

pop ebp ; restore base pointer
ret

Adder ENDP

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

END ; end of source code