Home > Net >  Difficulty finding the number of characters in the assembly
Difficulty finding the number of characters in the assembly

Time:11-07

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

CodePudding user response:

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
  • Related