0

I am writing a program to prompt user to enter a string, and then enter a char they want to search the string for. Once complete, I want to display the number of occurrences. My compare instruction is where I am getting stuck; it is not working like it should.

.data
String1 BYTE "Enter a sentence: ", 0
String2 BYTE 50 DUP (?)
String3 BYTE "Decimal length: ", 0
String4 BYTE "Hexidecimal length: ", 0
String5 BYTE "You entered: ", 0
String6 BYTE "Enter a character: ", 0
String7 BYTE "Searching for: ", 0
String8 BYTE "Number of occurences: ", 0
char BYTE ?


Exercise2 PROC
mov al, 0Ah
call WriteChar
mov al, 0Dh
call WriteChar
mov edx, OFFSET String1
call WriteString
mov ecx, 50
mov edx, OFFSET String2
call ReadString
mov edx, OFFSET String6
call WriteString
call ReadChar

mov esi, OFFSET String2
mov edi, OFFSET char
mov ecx, 12 ;arbitrary string length
mov edx, 0
CLD         
REPE CMPSB
inc dl
;how do i store number of occerrence in dl??
mov eax, edx
call WriteDec

Exercise2 ENDP
5
  • You need a loop. There's no single instruction that counts matches for you. You could loop on repe cmpsb to basically while(haystack = memchr(haystack, needle, len)) { match++; } (with updating len as well), but it's probably easier to just compare a character at a time. Commented Sep 30, 2018 at 21:19
  • 1
    Repe loops while the character matches. To find the first occurrence of a character in a string, use repne.
    – prl
    Commented Sep 30, 2018 at 21:38
  • 1
    One possible implementation is How to count the occurence of specific characters in a string in emu8086. But it's pretty inefficient (and I wouldn't recommend the loop instruction for looping), and you'd have to port it to 32-bit. Commented Sep 30, 2018 at 21:49
  • Could you elaborate on what would be in the loop?
    – MichaelL
    Commented Sep 30, 2018 at 21:56
  • "what would be in the loop" - Check to see if the "current character" is the character you are looking for. Adjust the "current character." Repeat. Commented Oct 1, 2018 at 2:52

0

Browse other questions tagged or ask your own question.