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

No comments:

Post a Comment

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