So these are the goals of my code: The goal of this assignment is to give you experience working with loops, arrays, and creating and using your own procedures. You need to create a program that allows the user to enter a string and search for a user-specified character in the string. The program will return the number of times the character was found in the string, the first occurrence, and last occurrence of the character.
Requirements: Allow the user to enter a character to use in the search. You must create a search procedure that receives a reference (pointer) to a string the user entered, the size of the string, and the character to use in the search. This procedure cannot get input from the user or display output to the screen. It must simply look through the string, find the necessary information, and return outputs (number of occurrences, first occurrence, and last occurrence) using registers. You must make use of variables to store all user inputs and values. You must make use of comments and good programming practices (proper spacing, proper indentation of code, and good naming) in your code.
Example Program:
Enter a string: CSC203 RCSJ
Enter a character to use in the search: C
Performing search...
Search completed.
Search results:
The character 'C' was found 3 times in the string.
First occurrence: 1
Last occurrence: 9
My code looked like this:
INCLUDE Irvine32.inc
.DATA
inputString BYTE 100 DUP(0) ; Define a buffer for the user's input string
searchChar BYTE ? ; Define a variable to store the character to search for
;strLength DWORD ? ; Define a variable to store the length of the string
count DWORD 0 ; Define a variable to store the count of occurrences
firstIndex DWORD 0 ; Define a variable to store the index of the first
lastIndex DWORD 0 ; Define a variable to store the index of the last occurrence
promptString BYTE "Enter a string: ",0
promptChar BYTE "Enter a character to use in the search: ",0
searchStart BYTE "Performing search...",0
searchEnd BYTE "Search completed.",0
resultsTitle BYTE "Search results:",0
foundCountString BYTE "The character '%c' was found %d times in the string.",0dh, 0ah, 0
firstOccurString BYTE "First occurrence: %d",0
lastOccurString BYTE "Last occurrence: %d",0
.CODE
main PROC
; prompt the user for input string
mov edx, OFFSET promptString
call WriteString
mov edx, OFFSET inputString
mov ecx, SIZEOF inputString
call ReadString
; prompt the user for search character
mov edx, OFFSET promptChar
call WriteString
call ReadString
call Crlf
mov searchChar, al
call Crlf
; perform the search
mov edx, OFFSET searchStart
call WriteString
mov count, 0 ; initialize count to 0
call SearchForChar
call Crlf
call Crlf
mov edx, OFFSET searchEnd
call WriteString
call Crlf
call Crlf
; display the results
mov edx, OFFSET resultsTitle
call WriteString
call Crlf
mov eax, count
movzx ecx, searchChar
mov edx, OFFSET foundCountString
call WriteString
call Crlf
call WriteChar ; add this line to print the searchChar value
call WriteDec ; add this line to print the count value
mov edx, OFFSET firstOccurString
call WriteString
mov eax, firstIndex
call WriteDec
call Crlf
mov edx, OFFSET lastOccurString
call WriteString
mov eax, lastIndex
call WriteDec
call Crlf
exit
main ENDP
SearchForChar PROC
; initialize variables
mov ecx, edx
mov esi, OFFSET inputString
movzx ebx, searchChar
mov count, 0
mov firstIndex, -1
mov lastIndex, -1
; loop through the string
mov edi, 0
searchLoop:
cmp BYTE PTR [esi+edi], 0 ; check for end of string
je endSearchLoop
cmp BYTE PTR [esi+edi], bl ; compare current character with
jne nextChar
add count, 1 ; increment count
cmp firstIndex, -1 ; check if first occurrence has been found
jne nextChar
mov firstIndex, edi ; set first occurrence
nextChar:
mov lastIndex, edi ; update last occurrence
inc edi ; move to next character
jmp searchLoop
endSearchLoop:
; return the results in registers
mov eax, count
mov ebx, firstIndex
mov ecx, lastIndex
ret
SearchForChar ENDP
END main
The result of this was this display on the debug screen:
Enter a string: teddy
Enter a character to use in the search: d
Performing search...
Search completed.
Search results:
The character '%c' was found %d times in the string.
0First occurrence: %d4294967295
Last occurrence: %d4
I need help with formatting the code and replacing the '%c' and %d with the correct values. So it would say "The character 'd' was found 2 times in the string."