display the number of capitals letters in the sentence

Thread Solved

Join Date: Dec 2006
Posts: 10
Reputation: Rooro is an unknown quantity at this point 
Solved Threads: 0
Rooro Rooro is offline Offline
Newbie Poster

display the number of capitals letters in the sentence

 
0
  #1
Dec 12th, 2006
Hello everyone : )

i'm working on :



Write a program that takes an input sentence from the user and on next line display the number of capitals letters in the sentence.

Note:
ยท User is not allowed to enter a sentence with more than 9 Capital letters.


what i did is :




  1. .MODEL SMALL
  2. .STACK 100H
  3. .DATA
  4.  
  5. MSG1 DB 'PLEASE ENTER YOUR SENTENCE: $'
  6.  
  7. MSG2 DB 0DH,0AH,'THE NUMBER OF CAPITAL LETTERS IN SENTENCE :$'
  8.  
  9. MSG3 DB 0DH,0AH,'You have entered more than 9 capital liter $'
  10.  
  11.  
  12. .CODE
  13.  
  14. MAIN PROC
  15.  
  16.  
  17. MOV AX,@DATA ;initialize DS
  18. MOV DS,AX
  19.  
  20.  
  21. ;DISPLAY MSG1
  22.  
  23. LEA DX,MSG1 ;GET MSG1
  24. MOV AH,9 ;DISPLAY STRING
  25. INT 21H ;DISPLAY MSG1
  26.  
  27.  
  28. ;READ CHARCHTER
  29.  
  30. ;if the user has entered more than 9 capital liter exit ...
  31.  
  32. MOV bx,0
  33. mov cx,9
  34.  
  35. MOV AH,1 ;READ sentenc
  36.  
  37.  
  38.  
  39.  
  40. TOP:
  41.  
  42. INT 21h
  43.  
  44. Mov bl,al
  45.  
  46. cmp bl,'A'
  47.  
  48. JGE Count
  49.  
  50. CMP bl,'Z'
  51. JLE Count
  52.  
  53.  
  54. Count:
  55.  
  56. INC bx
  57. LOOP TOP
  58.  
  59.  
  60. LEA DX,MSG2 ;GET MSG2
  61. MOV AH,9 ;DISPLAY STRING
  62. INT 21H ;DISPLAY MSG1
  63.  
  64. mov ah,2
  65. mov DX,BX
  66. INT 21H
  67.  
  68.  
  69.  
  70. MOV AH,4CH ; return control to DOS
  71. INT 21H
  72.  
  73. MAIN ENDP
  74.  
  75. END MAIN
  76.  
  77. END

My problem is on counting the number of capital letters .

Can anyone help please ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: display the number of capitals letters in the sentence

 
0
  #2
Dec 12th, 2006
That loop needs to check for al = 13H ( <Enter> key) to stop the loop. As coded now the loop will never stop.

>> Mov bl,al
Why do that? just use al for tests


  1. cmp bl,'A'
  2.  
  3. JGE Count
  4.  
  5. CMP bl,'Z'
  6. JLE Count
I think you have the comparisons backwards. If the character < 'A' or > 'Z' then it is not a capital letter.

  1. cmp al,'A'
  2.  
  3. JL Count
  4.  
  5. CMP al,'Z'
  6. JG Count

>>My problem is on counting the number of capital letters .
You have to select a register to do the counting. cx is normally used for that purpose, but bx and dx will do as well. Just initialize it to zero before the loop and increment it after the checks

  1. xor cx,cx ; clear the register
  2. TOP:
  3.  
  4. INT 21h
  5.  
  6. cmp al,'A'
  7. JL Count
  8. CMP al,'Z'
  9. JG Count
  10. inc cx ; count number of capital letters
  11.  
  12. Count:
  13. LOOP TOP
Last edited by Ancient Dragon; Dec 12th, 2006 at 12:40 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: Rooro is an unknown quantity at this point 
Solved Threads: 0
Rooro Rooro is offline Offline
Newbie Poster

Re: display the number of capitals letters in the sentence

 
0
  #3
Dec 12th, 2006
Thanks a lot Ancient Dragon


.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'PLEASE ENTER YOUR SENTENCE: $'
MSG2 DB 0DH,0AH,'THE NUMBER OF CAPITAL LETTERS IN
SENTENCE '
MSG3 DB 0DH,0AH,'You have entered more than 9 capital
LETTERS $'

.CODE
MAIN PROC

MOV AX,@DATA ;initialize DS
MOV DS,AX

;DISPLAY MSG1
LEA DX,MSG1 ;GET MSG1
MOV AH,9 ;DISPLAY STRING
INT 21H ;DISPLAY MSG1

;READ CHARCHTER

TOP:

INT 21h

cmp al,'A'
JL Count
CMP al,'Z'
JG Count
inc cx ; count number of capital letters
Count:
LOOP TOP
LEA DX,MSG2 ;GET MSG2
MOV AH,9 ;DISPLAY STRING
INT 21H ;DISPLAY MSG1
mov ah,2
mov DX,CX
INT 21H

MOV AH,4CH ; return control to DOS
INT 21H
MAIN ENDP
END MAIN
END



i did another code , but still having problem which is when i want to read a sentenc from the user the program takes only one character !


  1. .MODEL SMALL
  2. .STACK 100H
  3. .DATA
  4.  
  5. MSG1 DB 'PLEASE ENTER YOUR SENTENCE: $'
  6.  
  7. MSG2 DB 0DH,0AH,'THE NUMBER OF CAPITAL LETTERS IN
  8.  
  9. SENTENCE :$'
  10.  
  11. MSG3 DB 0DH,0AH,'You have entered more than 9 capital
  12.  
  13. LETTERS $'
  14.  
  15.  
  16. .CODE
  17.  
  18. MAIN PROC
  19.  
  20.  
  21. MOV AX,@DATA ;initialize DS
  22. MOV DS,AX
  23.  
  24. mov bX,0
  25. ;DISPLAY MSG1
  26.  
  27. LEA DX,MSG1 ;GET MSG1
  28. MOV AH,9 ;DISPLAY STRING
  29. INT 21H ;DISPLAY MSG1
  30.  
  31.  
  32. MOV AH,1 ;READ sentenc
  33.  
  34. TOP:
  35. INT 21h
  36.  
  37.  
  38. WHILE_:
  39.  
  40.  
  41. cmp al,0dh
  42.  
  43. je END_WHILE
  44.  
  45.  
  46. cmp al,'A'
  47. CMP al,'Z'
  48. JMP Count
  49.  
  50. LOOP TOP
  51.  
  52. Count:
  53.  
  54. INC bx
  55.  
  56. cmp bx,9
  57. JLE Display1
  58. JG Display2
  59.  
  60.  
  61.  
  62. END_WHILE:
  63.  
  64.  
  65. Display1:
  66.  
  67. LEA DX,MSG2 ;GET MSG1
  68. MOV AH,9 ;DISPLAY STRING
  69. INT 21H ;DISPLAY MSG1
  70.  
  71. mov ah,2
  72. mov DX,BX
  73. INT 21H
  74.  
  75.  
  76. Display2:
  77.  
  78. LEA DX,MSG3 ;GET MSG1
  79. MOV AH,9 ;DISPLAY STRING
  80. INT 21H ;DISPLAY MSG1
  81.  
  82.  
  83. MOV AH,4CH ; return control to DOS
  84. INT 21H
  85.  
  86.  
  87.  
  88. MAIN ENDP
  89.  
  90. END MAIN
  91.  
  92. END
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: display the number of capitals letters in the sentence

 
0
  #4
Dec 12th, 2006
  1. cmp al,'A'
  2. CMP al,'Z'
  3. JMP Count
you have to put a jump after every comparison -- see my previous code for example.

>>LOOP TOP
you can not use that construct in this program because you have to set cx to the number of times you want it to loop. use a jmp instead of loop.

>>MOV AH,1 ;READ sentenc
move that line down a bit so that it is inside the loop and befor the call to int 21. I think you have to reset ah every time.

>>mov bX,0
move that down a bit just before the beginning of the loop so that int 21 functions does not destroy its value. It may not, but you don't know for sure.
Last edited by Ancient Dragon; Dec 12th, 2006 at 3:06 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: Rooro is an unknown quantity at this point 
Solved Threads: 0
Rooro Rooro is offline Offline
Newbie Poster

Re: display the number of capitals letters in the sentence

 
0
  #5
Dec 12th, 2006
also after I chaned the program i'm still having problem which is when i want to read a sentenc from the user the program takes only one character !


  1. .MODEL SMALL
  2. .STACK 100H
  3. .DATA
  4. MSG1 DB 'PLEASE ENTER YOUR SENTENCE: $'
  5. MSG2 DB 0DH,0AH,'THE NUMBER OF CAPITAL LETTERS IN
  6. SENTENCE :$'
  7. MSG3 DB 0DH,0AH,'You have entered more than 9 capital
  8. LETTERS $'
  9.  
  10. .CODE
  11. MAIN PROC
  12.  
  13. MOV AX,@DATA ;initialize DS
  14. MOV DS,AX
  15.  
  16. ;DISPLAY MSG1
  17. LEA DX,MSG1 ;GET MSG1
  18. MOV AH,9 ;DISPLAY STRING
  19. INT 21H ;DISPLAY MSG1
  20.  
  21. MOV AH,1 ;READ sentenc
  22. mov bX,0
  23. TOP:
  24. MOV AH,1 ;READ sentenc
  25. INT 21h
  26.  
  27. WHILE_:
  28.  
  29. cmp al,0dh
  30. je END_WHILE
  31.  
  32. cmp al,'A'
  33. JMP Count
  34. CMP al,'Z'
  35. JMP Count
  36. LOOP TOP
  37. Count:
  38. INC bx
  39. cmp bx,9
  40. JLE Display1
  41. JG Display2
  42.  
  43. END_WHILE:
  44.  
  45. Display1:
  46. LEA DX,MSG2 ;GET MSG1
  47. MOV AH,9 ;DISPLAY STRING
  48. INT 21H ;DISPLAY MSG1
  49. mov ah,2
  50. mov DX,BX
  51. INT 21H
  52.  
  53. Display2:
  54. LEA DX,MSG3 ;GET MSG1
  55. MOV AH,9 ;DISPLAY STRING
  56. INT 21H ;DISPLAY MSG1
  57.  
  58. MOV AH,4CH ; return control to DOS
  59. INT 21H
  60.  
  61. MAIN ENDP
  62. END MAIN
  63. END
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: display the number of capitals letters in the sentence

 
0
  #6
Dec 12th, 2006
  1. JMP Count
  2. CMP al,'Z'
  3. JMP Count
  4. LOOP TOP
The jumps are incorrect -- please see my previous post. jmp instruction is an unconditional jump meaning that the program will go regardless of any previous comparisons. you need to use the JL and JG jumps are in my example.

Also you failed to remove the LOOP. See my previous post for explaination why you can not use that in your program.

Out of curiosity, what other programming languages do you know? loops in assembly are very much the same in all other languages.
Last edited by Ancient Dragon; Dec 12th, 2006 at 3:22 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: Rooro is an unknown quantity at this point 
Solved Threads: 0
Rooro Rooro is offline Offline
Newbie Poster

Re: display the number of capitals letters in the sentence

 
0
  #7
Dec 12th, 2006
  1. .MODEL SMALL
  2. .STACK 100H
  3. .DATA
  4. MSG1 DB 'PLEASE ENTER YOUR SENTENCE: $'
  5. MSG2 DB 0DH,0AH,'THE NUMBER OF CAPITAL LETTERS IN
  6. SENTENCE :$'
  7. MSG3 DB 0DH,0AH,'You have entered more than 9 capital
  8. LETTERS $'
  9.  
  10. .CODE
  11. MAIN PROC
  12.  
  13. MOV AX,@DATA ;initialize DS
  14. MOV DS,AX
  15.  
  16. ;DISPLAY MSG1
  17. LEA DX,MSG1 ;GET MSG1
  18. MOV AH,9 ;DISPLAY STRING
  19. INT 21H ;DISPLAY MSG1
  20.  
  21.  
  22. MOV AH,1 ;READ sentenc
  23. INT 21h
  24. mov bX,0
  25. cmp al,0dh
  26. cmp al,'A'
  27. JL Count
  28. CMP al,'Z'
  29. JG Count
  30.  
  31. Count:
  32. INC bx
  33. cmp bx,9
  34. JLE Display1
  35. JG Display2
  36.  
  37.  
  38. Display1:
  39. LEA DX,MSG2 ;GET MSG1
  40. MOV AH,9 ;DISPLAY STRING
  41. INT 21H ;DISPLAY MSG1
  42. mov ah,2
  43. mov DX,BX
  44. INT 21H
  45.  
  46. Display2:
  47. LEA DX,MSG3 ;GET MSG1
  48. MOV AH,9 ;DISPLAY STRING
  49. INT 21H ;DISPLAY MSG1
  50.  
  51. MOV AH,4CH ; return control to DOS
  52. INT 21H
  53.  
  54. MAIN ENDP
  55. END MAIN
  56. END

i can not do my work clearly , realy i'm littlel tired


i want to know :

i should have something repeted many times to check every charactar but i can not use for loop right ? insted of that can I use WHILE loop ? if yes how can I do it ?

also how the program knows that the user fenish entring the sentenc ? , i have to compare the al with what exactly ? whith 0dH or 13h ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: display the number of capitals letters in the sentence

 
0
  #8
Dec 12th, 2006
Well I think you are making things worse, not better. Here is one way to do what you want. I have not assembled or tested this.
  1.  
  2. ;----
  3. ; ---- start of loop reading the keyboard
  4. ; ---- one character at a time
  5. ;----
  6. mov bX,0 -- initialize capital character counter
  7. loop_top:
  8. MOV AH,1 ;READ A CHARACTDR
  9. INT 21h
  10. cmp al,0dh ; end of line?
  11. je Display1 ; yes, then stop
  12. ; test for capital letters
  13. cmp al,'A'
  14. JL loop_end ; if char < 'A' then go
  15. CMP al,'Z'
  16. JG loop_end ; if char > 'Z' then go
  17.  
  18. INC bx ; increment capital letter counter
  19. cmp bx,9
  20. JG Display2 ; too many capital letters
  21.  
  22. loop_end:
  23. jmp loop_top
  24.  
  25.  
  26.  
  27. Display1:
  28. LEA DX,MSG2 ;GET MSG1
  29. MOV AH,9 ;DISPLAY STRING
  30. INT 21H ;DISPLAY MSG1
  31.  
  32. mov dl,bl ; print the number of characters
  33. add dl,'0' ; make it readable
  34. MOV AH,2 ;display the character that is in dl
  35. INT 21H
  36. jmp exit_program ; all done
  37.  
  38. Display2:
  39. LEA DX,MSG3 ;GET MSG1
  40. MOV AH,9 ;DISPLAY STRING
  41. INT 21H ;DISPLAY MSG1
  42.  
  43. exit_program:
  44. MOV AH,4CH ; return control to DOS
  45. INT 21H
  46.  
  47. MAIN ENDP
  48. END MAIN
  49. END
Last edited by Ancient Dragon; Dec 12th, 2006 at 4:37 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: Rooro is an unknown quantity at this point 
Solved Threads: 0
Rooro Rooro is offline Offline
Newbie Poster

Re: display the number of capitals letters in the sentence

 
0
  #9
Dec 12th, 2006
Ancient Dragon

I do not konw what can I say to you :o

Thanks A lot ....


i have understand all the code unless this point :

  1.  
  2.  
  3. mov dl,bl ; print the number of characters
  4. add dl,'0' ; make it readable

may you explain it plaese ?

I want to learn why you have chose the dl register + 0 ?

to convert from ASCII code to decimal, to display it in the right way ?



-----------------------

if the user is allowed to enter a sentence with maximum 19 capital letters , How the display3 will be ?

  1.  
  2.  
  3. cmp bx,19
  4. JG Display2 ; more than 19 capital letters


if <9

Display1

if >9

display3

first we have to display number 1

like :

  1.  
  2. mov ah,2
  3. mov dl,'1'
  4. int 21h

then,we will do subtract with 10 with that number which is > 9
then, display it , but how can I display this number in decimal ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: display the number of capitals letters in the sentence

 
0
  #10
Dec 12th, 2006
>>may you explain it plaese ?
If you look at a standard ascii chart you will see that the letter '0' has a decimal value of 48. So, to convert a binary value all you have to do is add 48 to it. For example, assume there was just one capital letter in the string, that makes dl = 1. add 48 to that gives you dl value of 49. Then look at the ascii chart and see that the decimal value of 49 is the letter '1'. Note that the letter '1' and the decimal value 1 are not the same thing.

>>f the user is allowed to enter a sentence with maximum 19 capital letters , How the display3 will be ?
don't you mean 9 capital characters? Display2 shows MSG3. There is no Display3 (or at least you did not write one).

display3

first we have to display number 1

like :
<snip>
I don't see any requirement for you to display two digits. There is not limit to the string length you can enter, only a limit of 9 capital letters. You can enter 255 characters if you want to.
Last edited by Ancient Dragon; Dec 12th, 2006 at 7:35 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC