0

我想计算输入字符串中元音的数量,只用小写字母。
我的代码是这个,但他没有正确计算它们(cmp说明从不true

data segment 
      s db 10 dup(' ')
data ends

code segment 

assume ds:data, cs:code

debut: mov ax,data
mov ds,ax

mov dx,offset s
mov ah,0ah
int 21h

mov cl,s[1] 
mov di,offset s
mov bx,0

nr_vocale: cmp cl,0
je sfarsit
mov al,[di]
cmp al,'a'
je increment
mov al,[di]
cmp al,'e'
je increment
mov al,[di]
cmp al,'i'
je increment
mov al,[di]
cmp al,'o'
je increment
mov al,[di]
cmp al,'u'
je increment
inc di
dec cl


increment:  inc bx
 sfarsit: mov dl,bl
 mov ah,2
 int 21h

 mov ah,4ch
 int 21h

 ends code
end debut
4

3 回答 3

1

您没有循环,只需检查字符串中的第一个字母然后退出。

同样奇怪的是,您从 s[1] 加载 cl 就好像长度存储在字符串中一样。或者如果长度在那里,则字符串数据可能在 s[2] 处,这必须反映在 di 中。

加载一次就足够了,只需执行 cmp 指令即可。

于 2013-06-03T14:54:53.790 回答
0

这将计算元音

countvowels:                     
mov di,VOWL                     
mov si,STR                      
mov bp,di                       
mov cx,END                      
mov bx,STR                      
sub cx,bx             ;stringlength for loop          
dec cx                ;adjust for cx looping         
push cx               ;saved for repeating          
sub si,1              ;tweak si          

nextletter:                      
inc si                          
mov al,[si]                     

vowelrotate:                     
cmp al,[di]                     
jz addone                       
loop nextletter                 
inc di                ;vowel pointer rotate at end of string          
mov si,STR-1          ;return to start of string          
pop cx                ;loop count for string          
push cx                         
mov ah,"x"            ;check for end of vowels           
cmp [di],ah                     
jz finished                     
jmp nextletter                  

addone:                ;vowel count          
inc dx                          
jmp nextletter                  

finished:                        
##                              

VOWL                            
db "aeioux"                     
STR                             
db "input string from keyboard"         
END                             
于 2013-06-04T10:47:32.863 回答
0

假设这s是您的字符串偏移量和$字符串终止符,根据您的评论,那么您可以执行以下操作:

        mov    si,offset s    
        cld                ; Scan forward
        mov    bx,0        ; bx is count of vowels
check:
        lodsb              ; Load string byte DS:SI in AL and increment SI
        cmp    al, '$'     ; Terminator?
        je     finished
;;      or     al, 20h     ; Set string to lower case (optional)
        cmp    al, 'a'     
        je     increment
        cmp    al, 'e'     
        je     increment
        cmp    al, 'i'     
        je     increment
        cmp    al, 'o'     
        je     increment
        cmp    al, 'u'     
        je     increment
increment:
        inc bx
        jmp    check       ; repeat
finished:
        ;; BX holds the number of vowels.
于 2013-06-03T17:58:22.040 回答