944,043 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Marked Solved
  • Views: 7492
  • Assembly RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 12th, 2006
0

display the number of capitals letters in the sentence

Expand Post »
Hello everyone : )

i'm working on :



Quote ...
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 :




Assembly Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rooro is offline Offline
10 posts
since Dec 2006
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

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


Assembly Syntax (Toggle Plain Text)
  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.

Assembly Syntax (Toggle Plain Text)
  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

Assembly Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

Thanks a lot Ancient Dragon


Quote ...
.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 !


Assembly Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rooro is offline Offline
10 posts
since Dec 2006
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

Assembly Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

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 !


Assembly Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rooro is offline Offline
10 posts
since Dec 2006
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

Assembly Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

Assembly Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rooro is offline Offline
10 posts
since Dec 2006
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

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.
Assembly Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

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 :

Assembly Syntax (Toggle Plain Text)
  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 ?

Assembly Syntax (Toggle Plain Text)
  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 :

Assembly Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rooro is offline Offline
10 posts
since Dec 2006
Dec 12th, 2006
0

Re: display the number of capitals letters in the sentence

>>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).

Quote ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: assembly using PIC16F877A HELP
Next Thread in Assembly Forum Timeline: help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC