我们如何计算8086程序集中字符串中元音的数量?

时间:2013-06-03 14:41:23

标签: assembly computer-architecture

我想计算输入字符串中元音的数量,只用小写字母书写 我的代码就是这个,但他没有把它们算得正确(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

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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.

答案 2 :(得分: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