在 8086 汇编中获取字符串并计算其中的字符
Taking string in 8086 Assembly and counting a characters in it
我目前正在尝试在汇编中编写代码,以从用户那里获取一些字符串并计算其中的 'a' 个字符。这项工作看起来很简单,但问题是我真的数不清,也不知道问题出在哪里。例如,对于单词 'amin',输出为 97,对于其他单词,输出为 >6。网上关于8086汇编的教程不多,所以如果有人能帮助我,我将不胜感激。
stk segment
dw 32 dup(?)
stk ends
dts segment
p1 db 10,13,'Please enter max 80 char',10,13,'$'
p2 db 10,13,'Number of (a) chars: $'
max db 80
len db ?
count db 0
char db 'a'
str db 80 dup (?)
dts ends
cds segment
assume cs:cds, ss:stk, ds:dts
main proc far
mov ax, seg dts
mov ds,ax
mov ah,09
mov dx,offset p1
int 21h
mov ah,0ah
mov dx,offset max
int 21h
lea si,str
mov cl,len
mov ch,0 ; Initializing CX(Counter) Register for loop
check:
mov al,[si]
cmp char,al
jne skip
inc count
skip:
inc si; Next char in str
loop check
mov al,count
mov ah,0
mov dl,10
div dl
add ax,3030h; making the right ascii code for printing
mov bx,offset max-3
mov [bx],ax
mov ah,09
mov dx,offset p2
int 21h
mov ah,4ch
int 21h
main endp
cds ends
end main
int 21h,ah=0ah 将用户输入读入 DS:DX 处的缓冲区。 DS:DX指向的缓冲区的第一个字节是最大长度,后面是实际长度,后面是读取的字符。您需要在 len 之后立即定义 str
;否则输入会覆盖 count
和 char
。你得到 97 的原因是 count
被你输入的第一个字符覆盖。
为了让你的代码更清楚,我建议这样写:
buf:
max db 80
len db ?
str db 80 dup (?)
count db 0
char db 'a'
然后在int 21h之前,ah=0ah,
mov dx, offset buf
我目前正在尝试在汇编中编写代码,以从用户那里获取一些字符串并计算其中的 'a' 个字符。这项工作看起来很简单,但问题是我真的数不清,也不知道问题出在哪里。例如,对于单词 'amin',输出为 97,对于其他单词,输出为 >6。网上关于8086汇编的教程不多,所以如果有人能帮助我,我将不胜感激。
stk segment
dw 32 dup(?)
stk ends
dts segment
p1 db 10,13,'Please enter max 80 char',10,13,'$'
p2 db 10,13,'Number of (a) chars: $'
max db 80
len db ?
count db 0
char db 'a'
str db 80 dup (?)
dts ends
cds segment
assume cs:cds, ss:stk, ds:dts
main proc far
mov ax, seg dts
mov ds,ax
mov ah,09
mov dx,offset p1
int 21h
mov ah,0ah
mov dx,offset max
int 21h
lea si,str
mov cl,len
mov ch,0 ; Initializing CX(Counter) Register for loop
check:
mov al,[si]
cmp char,al
jne skip
inc count
skip:
inc si; Next char in str
loop check
mov al,count
mov ah,0
mov dl,10
div dl
add ax,3030h; making the right ascii code for printing
mov bx,offset max-3
mov [bx],ax
mov ah,09
mov dx,offset p2
int 21h
mov ah,4ch
int 21h
main endp
cds ends
end main
int 21h,ah=0ah 将用户输入读入 DS:DX 处的缓冲区。 DS:DX指向的缓冲区的第一个字节是最大长度,后面是实际长度,后面是读取的字符。您需要在 len 之后立即定义 str
;否则输入会覆盖 count
和 char
。你得到 97 的原因是 count
被你输入的第一个字符覆盖。
为了让你的代码更清楚,我建议这样写:
buf:
max db 80
len db ?
str db 80 dup (?)
count db 0
char db 'a'
然后在int 21h之前,ah=0ah,
mov dx, offset buf