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 ?

Posted on 2006-12-12 07:37:47 by Rooro
1. homework.. read the rules
2. use buffered input, that way your little bit of code will work...
Posted on 2006-12-12 10:07:18 by evlncrn8
A to Z goes from 41 to 5A inclusive.
So dump a letter at a time into al and cmp it

zzzz
mov al,
cmp al,41
jb zzzz
cmp al,5A
ja zzzz
Posted on 2006-12-12 10:13:23 by eek

evlncrn8

Sorry , i did know that i have not do this , but what i need is only small help : )

use buffered input, that way your little bit of code will work...


what do you mean by : buffered ?

i'm new in assembly languge.



_____________________________________________

eek Thanks ...

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
Posted on 2006-12-12 12:02:13 by Rooro
Thats because I very wickedly wrote code that loops forever. :D
To see if you might figure that out, as part of your homework.
You'll need to increment the routine along the string so it loads the next letter after the test.

mov bx,0
mov cx,
zzzz
inc       ;increment the pointer
mov al,
dec cx
cmp al,41
jb zzzz
cmp al,5A
ja zzzz
inc bx                         ;set to zero at start, to count capitals
inc cx                          ;prevents double cx decrement
loopnz zzzz                         ;This decrements cx and
               ;falls through when cx=0

There is one more error in there, it won't count the very first letter so you'll need to have a go at that.
Posted on 2006-12-12 20:29:43 by eek
Actually that last one would still loop too much


mov bx,0
mov cx,
dec si                             ;si string pointer
zzzz
inc si
mov al,              ;or lodsb, which does both inc and mov
dec cx
cmp cx,FFFF
jz xxxx                            ;This escapes from eternal looping
cmp al,41
jb zzzz
cmp al,5A
ja zzzz
inc bx
inc cx
loopnz zzzz
xxxx

Its pretty guffy code, but if it works your teacher is unlikely to accuse you of copying it from anyone... :D
Posted on 2006-12-12 21:09:29 by eek
And if you want to do buffered keyboard input then its

mov ah,0A
int 21

which you'll need to googlie for info.
Posted on 2006-12-12 21:32:20 by eek
Here is the complete code I wrote in TASM 3.2. It uses 8086 instructions throughout the code and is pretty self explanatory. I have commented every single line of the code but if you find it hard to understand, post back and I'll explain it more.

The , , and procedures are part of the OASML 1.0 library and I have just removed their description parts just to save some space. The procedure is the procedure that counts the number of upper-case letters in a null-terminated string and then returns the result in the AX register. The trick that I've used is to subtract 65 from any character/byte and then compare it to the value 25. Values higher than that are not upper-case.

For example, if you have the character 'B' which has the ASCII of 66 (0x42), you should subtract 65 from it to get 1. 1 is in the range 0<1<25, therefore, the letter is upper-case. Another example is when you have the character 'f' with the ASCII code of 102 (0x66). You can now subtract 65 from it to get 37 which is not in the range 0...25, therefore, this is not an upper-case letter. Here is the code:

P.S: The program does not use the DOS trap except for the termination part and it also uses Null-terminated strings throughout the code.

Posted on 2006-12-15 00:39:18 by XCHG

Hi,

the previos example surely works, but i tried a shorter version ( Masm 16bit ):

.model tiny
dos equ 21h
print macro string
  lea dx,string
  mov ah,09
  int dos
endm
.data
msg1 db "Enter sentence (and press Enter)",13,10,"$"
msg2 db 13,10,"counted capitals : $"
.code
.startup
        print msg1
        xor cx,cx
@@:
        mov ah,01
        int  21h
        cmp al,0dh
        je @f
        .if al>='A'
          .if al<='Z'
            inc cl
            cmp cl,9
            jae @f
          .endif
      .endif
      jmp @b
@@:
    print msg2
    or cl,30h
    mov dl,cl
    mov ah,02
    int dos
.exit
end


u don't have to write novels even when u are programming in Assembler :)
Posted on 2006-12-17 12:52:11 by kermit
The shortest path is not always the best. What if the user presses the Back-space key? What if you made the program write the characters to a buffer in order to solve this problem but the user entered a dollar sign as one of the characters? Your program is using the DOS trap thus working with the dollar-terminated strings.

Let?s say you decide to use null-terminated strings instead to be able to solve the dollar-sign related issue. In this case, you are going to have to use the 0x10 trap in the IVT and you are left to handle the back-space key yourself. You are going to have to get the cursor position, decrement the column, put a null-byte there, remove that character from your buffer too and etc?

Of course you can still use the DOS trap but not like above.
Posted on 2006-12-18 12:55:26 by XCHG
really All of thanks to all of you ...  ;)


eek

thanks for your help , and I get your idea : )

specially :

And if you want to do buffered keyboard input then its

mov ah,0A
int 21


_________________________________

XCHG

great , I will improve my self in assembly by your code , thanks a lot


__________________________________

kermit

thanks for your help , this new short way is new for me ! i will try to get it :)


______________________


this is what I have 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
Posted on 2006-12-19 11:39:39 by Rooro