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 :

.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 liter $'
 
 
.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
 
;if the user has entered more than 9 capital liter exit ...
 
MOV bx,0
mov cx,9
 
MOV AH,1  ;READ sentenc
 
 
 
 
TOP:
 
INT 21h
 
Mov bl,al
 
cmp bl,'A'
 
JGE Count
 
CMP bl,'Z'
JLE Count
 
 
Count:
 
INC bx
LOOP TOP
 
 
LEA DX,MSG2  ;GET MSG2
MOV AH,9  ;DISPLAY STRING
INT 21H  ;DISPLAY MSG1
 
mov ah,2
mov DX,BX
INT 21H
 
 
 
MOV AH,4CH ; return control to DOS
INT 21H
 
MAIN ENDP
 
 END MAIN
 
END

My problem is on counting the number of capital letters .

Can anyone help please ?

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

cmp bl,'A'
 
JGE Count
 
CMP bl,'Z'
JLE Count

I think you have the comparisons backwards. If the character < 'A' or > 'Z' then it is not a capital letter.

cmp al,'A'
 
JL Count
 
CMP al,'Z'
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

xor cx,cx ; clear the register
TOP:
 
INT 21h
 
cmp al,'A'
JL Count
CMP al,'Z'
JG Count
inc cx ; count number of capital letters

Count:
LOOP TOP

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 !

.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

mov bX,0
;DISPLAY MSG1

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


MOV AH,1 ;READ sentenc

TOP:
INT 21h


WHILE_:


cmp al,0dh

je END_WHILE


cmp al,'A'
CMP al,'Z'
JMP Count

LOOP TOP

Count:

INC bx

cmp bx,9
JLE Display1
JG Display2



END_WHILE:


Display1:

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

mov ah,2
mov DX,BX
INT 21H


Display2:

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


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



MAIN ENDP

END MAIN

END
cmp al,'A'
CMP al,'Z'
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.

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 !

.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

MOV AH,1  ;READ sentenc
mov bX,0
TOP:
MOV AH,1  ;READ sentenc
INT 21h

WHILE_:

cmp al,0dh
je END_WHILE

cmp al,'A'
JMP Count
CMP al,'Z'
JMP Count
LOOP TOP
Count:
INC bx
cmp bx,9
JLE Display1
JG Display2
 
END_WHILE:

Display1:
LEA DX,MSG2  ;GET MSG1
MOV AH,9  ;DISPLAY STRING
INT 21H  ;DISPLAY MSG1
mov ah,2
mov DX,BX
INT 21H

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

MOV AH,4CH ; return control to DOS
INT 21H
 
MAIN ENDP
 END MAIN
END
JMP Count
CMP al,'Z'
JMP Count
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.

.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
 
 
MOV AH,1  ;READ sentenc
INT 21h
mov bX,0
cmp al,0dh
cmp al,'A'
JL Count
CMP al,'Z'
JG Count
 
Count:
INC bx
cmp bx,9
JLE Display1
JG Display2
 
 
Display1:
LEA DX,MSG2  ;GET MSG1
MOV AH,9  ;DISPLAY STRING
INT 21H  ;DISPLAY MSG1
mov ah,2
mov DX,BX
INT 21H
 
Display2:
LEA DX,MSG3  ;GET MSG1
MOV AH,9  ;DISPLAY STRING
INT 21H  ;DISPLAY MSG1
 
MOV AH,4CH ; return control to DOS
INT 21H
 
MAIN ENDP
 END MAIN
END

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


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 ?

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.

;----
; ---- start of loop reading the keyboard
; ---- one character at a time
;----
mov bX,0 -- initialize capital character counter
loop_top:
MOV AH,1  ;READ A CHARACTDR
INT 21h
cmp al,0dh ; end of line?
je Display1 ; yes, then stop
; test for capital letters
cmp al,'A'
JL loop_end ; if char < 'A' then go
CMP al,'Z'
JG loop_end ; if char > 'Z' then go

INC bx  ; increment capital letter counter
cmp bx,9
JG Display2 ; too many capital letters

loop_end:
jmp loop_top


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

mov dl,bl ; print the number of characters
add dl,'0' ; make it readable
MOV AH,2  ;display the character that is in dl
INT 21H
jmp exit_program ; all done
 
Display2:
LEA DX,MSG3  ;GET MSG1
MOV AH,9  ;DISPLAY STRING
INT 21H  ;DISPLAY MSG1

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

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 :

mov dl,bl ; print the number of characters
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 ?

cmp bx,19
JG Display2  ; more than 19 capital letters

if <9

Display1

if >9

display3

first we have to display number 1

like :

mov ah,2
mov dl,'1'
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 ?

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

<< i'm attempting to do another program with maximum 19 capital letters

....

if the user is allowed to enter a sentence with maximum 19 capital letters

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


; start of loop reading the input
; one character at a time
mov bX,0 ; initialize CAPITAL LETTERS counter
loop_top:
MOV AH,1 ;READ A CHARACTDR
INT 21h
cmp al,0dh ; end of line?
je Display1 ; yes, then stop
; test for capital letters
cmp al,'A'
JL loop_end ; if char < 'A' then go to the end of the loop
CMP al,'Z'
JG loop_end ; if char > 'Z' then go to the end of the loop
INC bx ; increment capital letter counter
cmp bx,19
JG Display2 ; more than 19 capital letters
loop_end:
jmp loop_top


Display1:
LEA DX,MSG2 ;GET MSG1
MOV AH,9 ;DISPLAY STRING
INT 21H ;DISPLAY MSG1
mov dx,bx ; print the number of characters
add dx,'0' ;To convert the number of characters from ASCII to
decimal number
MOV AH,2 ;display the number of character that is in dl
INT 21H
jmp exit_program ; all done

Display2:
LEA DX,MSG3 ;GET MSG3
MOV AH,9 ;DISPLAY STRING
INT 21H ;DISPLAY MSG1
exit_program:
MOV AH,4CH ; return control to DOS
INT 21H

MAIN ENDP
END MAIN
ENDF

the output will be :

[IMG]http://www.4img.com/ar/up/06/12/12/6d9cb7de5e8ac30bd5e8734bc96a35c1.jpg[/IMG]

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

; start of loop reading the input
; one character at a time
mov bX,0   ; initialize CAPITAL LETTERS counter
loop_top:
MOV AH,1   ;READ A CHARACTDR
INT 21h
cmp al,0dh  ; end of line?
je Display1  ; yes, then stop
; test for capital letters
cmp al,'A'
JL loop_end  ; if char < 'A' then go to the end of the loop
CMP al,'Z'
JG loop_end  ; if char > 'Z' then go to the end of the loop
INC bx    ; increment capital letter counter
 
cmp bx,19
JG Display2   ; more than 19 capital letters

loop_end:
jmp loop_top

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

             

mov ah,2
mov dl,'1'
int 21h

mov dl,bl   ; print the number of characters
add dl,'0'  ;To convert the number of characters from ASCII to decimal number
SUB dl,10
MOV AH,2   ;display the number of character that is in dl
INT 21H
 
 
 

jmp exit_program ; all done
 
 

Display2:
LEA DX,MSG3   ;GET MSG3
MOV AH,9   ;DISPLAY STRING
INT 21H   ;DISPLAY MSG3
 
exit_program:
MOV AH,4CH  ; return control to DOS
INT 21H
 
MAIN ENDP
 END MAIN
ENDF

the problem if the user enterd less than 9 !!!!!

DONE :)

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

; start of loop reading the input
; one character at a time
mov bX,0   ; initialize CAPITAL LETTERS counter
loop_top:
MOV AH,1   ;READ A CHARACTDR
INT 21h
cmp al,0dh  ; end of line?
je Display1  ; yes, then stop
; test for capital letters
cmp al,'A'
JL loop_end  ; if char < 'A' then go to the end of the loop
CMP al,'Z'
JG loop_end  ; if char > 'Z' then go to the end of the loop
INC bx    ; increment capital letter counter
 
cmp bx,19
JG Display2   ; more than 19 capital letters

loop_end:
jmp loop_top
Display1:
LEA DX,MSG2   ;GET MSG1
MOV AH,9   ;DISPLAY STRING
INT 21H   ;DISPLAY MSG1
CMP bx,9
JLE Display3
 
mov ah,2
mov dl,'1'
int 21h

mov dl,bl   ; print the number of characters
add dl,'0'  ;To convert the number of characters from ASCII to decimal number
SUB dl,10
MOV AH,2   ;display the number of character that is in dl
INT 21H
jmp exit_program 
Display3:
mov dl,bl   ; print the number of characters
add dl,'0'  ;To convert the number of characters from ASCII to decimal number
MOV AH,2   ;display the number of character that is in dl
INT 21H
 
 
jmp exit_program ; all done
 
 

Display2:
LEA DX,MSG3   ;GET MSG3
MOV AH,9   ;DISPLAY STRING
INT 21H   ;DISPLAY MSG3
 
exit_program:
MOV AH,4CH  ; return control to DOS
INT 21H
 
MAIN ENDP
 END MAIN
ENDF

What changes would this have to be made if it was to run on 8086 assembly?

.686
.MODEL flat, stdcall
.STACK

.DATA

...


I am trying to run it...but no luck! The exe file after compiling just crashes!

This question has already been answered. Start a new discussion instead.