如何计算 8086 ALP 中字符串中的元音数量?
How to count number of vowels in a string in 8086 ALP?
我编写了一个汇编程序来计算用户读取的字符串中的元音数量。字符串的读取和长度的计算工作正常。但是当比较字符串的字符时,它对前两个不起作用 characters.This 是我的代码。
.MODEL small
.STACK
.DATA
input db 10,?
length db ?
count db ?
.CODE
.STARTUP
;reading string
mov dx,00h
mov dx,offset input
mov ah,0Ah
int 21h
;calculating length
mov length,00h
mov si,offset input+2
;checking for vowels
loopi: cmp [si],'$'
je next
add length,01h
inc si
loop loopi
next:
mov cx,00h
mov cl,length
mov si,offset input+2
mov count,00h
counting:cmp [si],'a'
je count1
cmp [si],'e'
je count1
cmp [si],'i'
je count1
cmp [si],'o'
je count1
cmp [si],'u'
je count1
inc si
loop counting
cmp cl,00h
je exit
count1:inc count
inc si
loop counting
exit:
.EXIT
end
此代码不是 comparing/checking 字符串的前两个字符。有人可以尽快帮我解决这个问题吗?
任何帮助将不胜感激。非常感谢。
Reading of string and calculation of length are working fine. But when comparing the characters of the string, it is not working for the first two characters.
正好,恰恰是比较部分就好了!你的烦恼是从输入开始存在的,因为你没有理解问号在汇编编程中的作用。
input db 10,?
length db ?
count db ?
在所有这些行中,问号 ? 代表一个字节,大多数(如果不是全部)汇编程序将初始化为值零 0。你得到的是:
input db 10, 0
length db 0
count db 0
这对于 length 和 count 很好,但对于 input 则不然应该是 的输入缓冲区。您确实没有所需的存储空间 space。这是 length、count 等被错误覆盖的内存!
解决方案是input db 10, 0, 10 dup (?)
。这允许输入 9 个字符。为什么是9?因为 DOS 总是在输入后附加一个回车符 return 13 并且该回车符 return 还需要这个 10 字节存储 space 中定义的一个字节10 dup (?)
.
这个马车 return 也解释了为什么你的长度计算会失败。当您应该搜索 ASCII 码 13.
时,您正在搜索“$”
当然,计算长度是多余的,因为 DOS 已经通知您了。输入结构的第二个字节是的长度。
mov cx, 0
mov cl, [input+1] ; length
总计:
.DATA
input db 10, 0, 10 dup (?)
count db ?
.CODE
.STARTUP
;reading string
mov dx, offset input
mov ah, 0Ah
int 21h
;checking for vowels
xor cx, cx ; Also clears the CX register like `mov cx, 0`
mov count, cl ; Count = 0
mov si, offset input+2
mov cl, [si-1] ; length is 2nd byte
counting:
cmp [si], 'a'
je count1
cmp [si], 'e'
je count1
cmp [si], 'i'
je count1
cmp [si], 'o'
je count1
cmp [si], 'u'
je count1
inc si
loop counting
cmp cl, 0 \ You can replace these 2 by
je exit / a single `jmp exit`
count1:
inc count
inc si
loop counting
exit:
.EXIT
更好的解决方案
- 没有使用慢
loop
指令
- 最小化内存访问
- 使用像
lodsb
这样的字符串原语
- 如果字符串为空则不会失败!
- 尽量减少跳来跳去的次数
此处显示:
;checking for vowels
cld ; For completeness because `lodsb` depends on it
mov si, offset input+2
mov dl, -1
vowel:
inc dl
other:
lodsb ; This is `mov al, [si]` followed by `inc si`
cmp al, 'a'
je vowel
cmp al, 'e'
je vowel
cmp al, 'i'
je vowel
cmp al, 'o'
je vowel
cmp al, 'u'
je vowel
cmp al, 13
jne other
mov count, dl
我编写了一个汇编程序来计算用户读取的字符串中的元音数量。字符串的读取和长度的计算工作正常。但是当比较字符串的字符时,它对前两个不起作用 characters.This 是我的代码。
.MODEL small
.STACK
.DATA
input db 10,?
length db ?
count db ?
.CODE
.STARTUP
;reading string
mov dx,00h
mov dx,offset input
mov ah,0Ah
int 21h
;calculating length
mov length,00h
mov si,offset input+2
;checking for vowels
loopi: cmp [si],'$'
je next
add length,01h
inc si
loop loopi
next:
mov cx,00h
mov cl,length
mov si,offset input+2
mov count,00h
counting:cmp [si],'a'
je count1
cmp [si],'e'
je count1
cmp [si],'i'
je count1
cmp [si],'o'
je count1
cmp [si],'u'
je count1
inc si
loop counting
cmp cl,00h
je exit
count1:inc count
inc si
loop counting
exit:
.EXIT
end
此代码不是 comparing/checking 字符串的前两个字符。有人可以尽快帮我解决这个问题吗? 任何帮助将不胜感激。非常感谢。
Reading of string and calculation of length are working fine. But when comparing the characters of the string, it is not working for the first two characters.
正好,恰恰是比较部分就好了!你的烦恼是从输入开始存在的,因为你没有理解问号在汇编编程中的作用。
input db 10,? length db ? count db ?
在所有这些行中,问号 ? 代表一个字节,大多数(如果不是全部)汇编程序将初始化为值零 0。你得到的是:
input db 10, 0
length db 0
count db 0
这对于 length 和 count 很好,但对于 input 则不然应该是
解决方案是input db 10, 0, 10 dup (?)
。这允许输入 9 个字符。为什么是9?因为 DOS 总是在输入后附加一个回车符 return 13 并且该回车符 return 还需要这个 10 字节存储 space 中定义的一个字节10 dup (?)
.
这个马车 return 也解释了为什么你的长度计算会失败。当您应该搜索 ASCII 码 13.
时,您正在搜索“$”当然,计算长度是多余的,因为 DOS 已经通知您了。输入结构的第二个字节是的长度。
mov cx, 0
mov cl, [input+1] ; length
总计:
.DATA
input db 10, 0, 10 dup (?)
count db ?
.CODE
.STARTUP
;reading string
mov dx, offset input
mov ah, 0Ah
int 21h
;checking for vowels
xor cx, cx ; Also clears the CX register like `mov cx, 0`
mov count, cl ; Count = 0
mov si, offset input+2
mov cl, [si-1] ; length is 2nd byte
counting:
cmp [si], 'a'
je count1
cmp [si], 'e'
je count1
cmp [si], 'i'
je count1
cmp [si], 'o'
je count1
cmp [si], 'u'
je count1
inc si
loop counting
cmp cl, 0 \ You can replace these 2 by
je exit / a single `jmp exit`
count1:
inc count
inc si
loop counting
exit:
.EXIT
更好的解决方案
- 没有使用慢
loop
指令 - 最小化内存访问
- 使用像
lodsb
这样的字符串原语
- 如果字符串为空则不会失败!
- 尽量减少跳来跳去的次数
此处显示:
;checking for vowels
cld ; For completeness because `lodsb` depends on it
mov si, offset input+2
mov dl, -1
vowel:
inc dl
other:
lodsb ; This is `mov al, [si]` followed by `inc si`
cmp al, 'a'
je vowel
cmp al, 'e'
je vowel
cmp al, 'i'
je vowel
cmp al, 'o'
je vowel
cmp al, 'u'
je vowel
cmp al, 13
jne other
mov count, dl