Character Searching in X86 Assembly
Solution
TITLE Project 4 (project4.asm)
; Program that uses the procedure FindChar to search a string for a specific character
; Written By: xxxxxxxxx
; CSC235
; Autumn 2018
INCLUDE Irvine32.inc
.data
promptString BYTE "Enter string to search: ", 0
promptChars BYTE "Enter characters to find: ",0
resultMsg BYTE "Characters found: ", 0
MAX_LENGTH = 100
string BYTE MAX_LENGTH + 1 dup(0) ; place to save the string read from the user
chars BYTE MAX_LENGTH + 1 dup(0) ; place to savve the characters to find
matched BYTE MAX_LENGTH + 1 dup(0) ; place to save the matched characters
.code
main PROC
call ClrScr ; clear the screen
mov edx,OFFSET promptString ; prompt user to enter a string
call WriteString ; print the prompt message
mov edx, OFFSET string ; give start address of string in edx for reading the string
mov ecx, MAX_LENGTH ; set a maximum of 100 characters to read
call ReadString ; read the string
mov edx,OFFSET promptChars ; prompt user to enter the characters to find
call WriteString ; print the prompt message
mov edx, OFFSET chars ; give start address of chars in edx for reading the characters to find
mov ecx, MAX_LENGTH ; set a maximum of 100 characters to read
call ReadString ; read the string of characters
mov esi, OFFSET chars ; load address of chars to find in esi
mov edi, OFFSET matched ; load address of matched chars in edi
loopChars:
mov bl, [esi] ; load a character to match from the chars into bl
cmp bl, 0 ; see if we reached the end of the string
je endLoop ; if so, end the loop
mov eax, OFFSET string ; put the string address in eax
call FindChar ; call the procedure to find the character
cmp eax, 0 ; see if the result was not found
je next ; if so, go to next
mov [edi], al ; else, save character found in the matched string
inc edi ; advance to next position in the matched string
next:
inc esi ; advance to next character in the characters to find string
jmp loopChars ; repeat loop to search for next
endLoop:
mov edx,OFFSET resultMsg ; print result message
call WriteString
mov edx,OFFSET matched ; print resulting matched string
call WriteString
call Crlf ; leave an empty line
exit ; exit the program
main ENDP
;---------------------------------------------------------
; FindChar
;
; Finds a character inside the supplied string, if it finds the
; character returns it, otherwise returns NULL
; Receives: EAX = start address of the string
; EBX = character to find
; Returns: EAX = character found or NULL
;---------------------------------------------------------
FindChar PROC
push esi ; save esi on the stack
mov esi, eax ; put address of start of string in esi
srchLoop:
mov al, [esi] ; load a character from the string
cmp al, 0 ; see if we reached the end of the string
je charNotFound ; if so, we didn't find the character
cmp al, bl ; else, compare loaded char with character to find
je charFound ; if they are equal, return char was found
inc esi ; else, advance to next character in string
jmp srchLoop ; start the loop again
charNotFound:
mov eax, 0 ; return a NULL (zero) to indicate not found
charFound: ; simply return the value in eax
pop esi ; restore prevoius value of esi
ret ; return to the main procedure
FindChar ENDP
END main