简体   繁体   English

在8086汇编中获取字符串并计算其中的字符

[英]Taking string in 8086 Assembly and counting a characters in it

I'm currently trying to write a code in assembly for taking some string from user and calculate the 'a' characters in it. 我目前正在尝试编写汇编代码以从用户那里获取一些字符串并计算其中的'a'字符。 The job seems very simple but the problem is I can't really count properly and don't know what is the problem. 这项工作看起来很简单,但问题是我无法真正正确地计数,也不知道问题出在哪里。 For example for the word 'amin', the output is 97, or for others something like >6. 例如,对于单词“ amin”,输出为97,对于其他类似> 6的输出。 And there is not many tutorials in internet for assembly 8086. So if anyone can help I'll be thankful. 互联网上没有关于组装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 reads user input into the buffer at DS:DX. int 21h,ah = 0ah将用户输入读入DS:DX的缓冲区。 The first byte of the buffer pointed to by DS:DX is the maximum length, followed by the actual length, followed by the characters read. DS:DX指向缓冲区的第一个字节是最大长度,然后是实际长度,然后是读取的字符。 You need to define str immediately following len; 您需要在len之后立即定义str otherwise the input overwrites count and char . 否则,输入将覆盖countchar The reason you get 97 is that count is overwritten by the first character of your input. 之所以得到97,是因为count被输入的第一个字符覆盖。

To make this clearer in your code, I suggest writing it like this: 为了使您的代码更清楚,我建议这样编写:

buf:
    max db 80
    len db ?
    str db 80 dup (?)

count db 0
char db 'a'

Then before the int 21h,ah=0ah, 然后在int 21h,ah = 0ah之前,

    mov dx, offset buf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM