-1

I wrote a program that receives a string and then I want to search and display the number of characters l in the string Show number if present; 0 if not present The problem with the program is exactly in the search loop The rest of the program works properly. I take the string but I can not search and display the result. I also put the image I want from the output Please help. I'm confused enter image description here

m02      macro
         mov ah,02
         mov dl,al
         int 21h
         endm
m08      macro
         mov ah,08
         int 21h
         endm
m09      macro str1
         mov ah,09
         mov dx,offset str1
         int 21h
         endm
m4c      macro
         mov ah,4ch
         int 21h
         endm

stk      segment stack 'stack'
         dw 32 dup(?)
stk      ends
dts      segment    
p1       db 10,13,'Please enter you text:',10,13,'$'
p2       db 10,13,'Number of characters (L) in the your text:    $'
string   db 11 dup(?),'$'
newLine  db 10,13,'$'
character db 0

dts      ends
cds      segment
         assume cs:cds,ss:stk,ds:dts
main    proc far
         mov  ax,seg dts
         mov  ds,ax
         mov  si,offset string
         m09  p1 
         mov  cx,11
tek:     m08
         mov byte ptr [si],al
         inc  si
         m02
         loop tek
         mov  cx,11
         mov  bx, 10
         mov  al,character
search:  cmp  byte ptr string[bx], 'l'
         je   skip
         inc  al
skip:    dec  bx
         jns  search
         m09 p2
         m09 al
         m4c
main    endp
cds     ends        
        end main
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Good
  • 81
  • 1
  • 7
  • 1
    You want `cmp` not `test`. Also `inc` changes the flags so your `je` is not using the result of the comparison. Obviously you only want to increment the count if it matched. Furthermore you need to ensure your loop runs the appropriate number of times. `dx[character]` is not a valid addressing mode so it should not even assemble. – Jester Nov 03 '21 at 12:31
  • 1
    What *does* happen when you run it? Not a [mcve] without test results. But probably you should look up `test` in the manual, and note that it does a bitwise AND. You're looking for `cmp`. (Also, `inc` writes ZF, so `inc character` / `je search` jumps if `inc` produced a zero. And I think you're adding two addresses with `dx[character]`, if that even assembles with `dx` outside the square brackets. Oh, no it definitely won't, DX isn't usable in 16-bit addressing modes. You don't want `character db 0` at all, you want a pointer or integer array index in a register.) – Peter Cordes Nov 03 '21 at 12:33
  • @PeterCordes I am a beginner, is it possible to write the correct code to get the result to better understand me? – Good Nov 03 '21 at 12:36
  • You could write it in C and have a compiler make an example for you. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) Or if you search on stack overflow, there are very likely some existing Q&As with working code for counting character occurrences in a string or char array. (Or for counting matches in an array of integers, which is literally the same problem; computers represent characters as integers.) – Peter Cordes Nov 03 '21 at 13:23
  • I'm not interested in writing a custom tutorial for every new beginner on Stack Overflow; I follow the assembly tag for performance / optimization questions. Some of this stuff, like syntax for addressing, is already covered in most existing tutorials, and single-stepping with a debugger once you get it to assemble should make a lot of other problems clearer. If you still have a mystery you can't solve that way, then post a [mcve] of it, with details on what you see in a debugger vs. what you expected to happen. – Peter Cordes Nov 04 '21 at 06:27

1 Answers1

1

I want to search and display the number of characters

If by number of characters you mean the length of the string, then no searching is needed since you have designed the input so that the length is always 11.

More useful could be to count the number of non-whitespace characters. Then the loop would have to not increment the counter if the current character happens to be a space character:

         mov  bx, 10
search:  cmp  byte ptr string[bx], ' '
         je   skip
         inc  character
skip:    dec  bx
         jns  search

"Number of characters (L) in the your text:"

Next code counts the number of lowercase "l" and uppercase "L". If it didn't use case-insensitiveness, you would not obtain 3 like in the picture! This time the loop skips incrementing the counter is the current character, after capitalization, is not "L".

         mov  bx, 10
search:  mov  al, string[bx]
         and  al, 0xDF          ; For case-insensitive
         cmp  al, "L"
         jne  skip
         inc  character
skip:    dec  bx
         jns  search

Remains displaying the numerical result, which your present program doesn't do!
See this Displaying numbers with DOS

ps The issues in your search loop were sufficiently dealt with in the comments by Jester and Peter Cordes.

Late catch

m09 p2
m09 al      <<<< This won't work!
m4c

To print the number in the AL register, you can't use your m09 macro that is destined for outputting strings.
You have the m02 macro for this purpose. Once the code works correctly, the only thing left is converting the value 3 that will be in AL to the character "3".

m09 p2
mov al, character
add al, 48
m02
m4c
Sep Roland
  • 33,889
  • 7
  • 43
  • 76