Monday, March 26, 2012

Searching Arrays in Assembly Language


TITLE Program Template (ArraySerach.asm)

; Program Description: This program searches for an element in an array
; Author: Akhtar Jamil
; Date Created: 21/3/2012
; Last Modification Date:

INCLUDE Irvine32.inc

; (insert symbol definitions here)

.data
arrElements BYTE 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
msgOptions BYTE "Please select an option:",0dh,0ah
msgOption1 BYTE "Press 1 for searching",0dh,0ah
msgOption2 BYTE "Press 2 for exiting",0
msgNumberEntry BYTE "Please enter a number to search: ",0
msgError BYTE "Error: Please press either 1 or 2",0
msgFound BYTE "Entered element is found, ie: ",0
msgNotFound BYTE "Entered element is NOT found in array",0
sprtLine BYTE "--------------------------------------",0
.code
main PROC
globalLoop:

 MOV EDX,OFFSET msgOptions
 CALL WRITESTRING
 CALL CRLF
 CALL READINT
 CMP AL,1
 JE searchLabel
 CMP AL,2
 JE exitLabel
 CALL CLRSCR
 MOV EDX,OFFSET msgError  ; Tell user that wrong option selected
 CALL WRITESTRING
 CALL CRLF
 JMP globalLoop ; jump back to main options
 
searchLabel:
  MOV EDX,OFFSET msgNumberEntry
  CALL WRITESTRING
  CALL READINT
  MOV ESI, OFFSET arrElements
  MOV ECX,15
loop1:
CMP AL, [ESI]
JE found
INC ESI
LOOP  loop1     ; Loop through all elements of array to search for element

  MOV EDX,OFFSET msgNotFound
  CALL CLRSCR
  CALL WRITESTRING
  CALL CRLF
  JMP globalLoop
 
  found:
  CALL CLRSCR
  MOV EDX,OFFSET msgFound
  CALL WRITESTRING
  CALL WRITEINT
  CALL CRLF
  MOV EDX,OFFSET sprtLine
  CALL WRITESTRING
  CALL CRLF
 
JMP globalLoop

exitLabel:
 EXIT ; exit to operating system
main ENDP

; (insert additional procedures here)

END main

No comments:

Post a Comment