【问题标题】:Taking string in 8086 Assembly and counting a characters in it在 8086 汇编中获取字符串并计算其中的字符
【发布时间】:2018-04-10 17:17:22
【问题描述】:

我目前正在尝试在汇编中编写代码,以从用户那里获取一些字符串并计算其中的“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

【问题讨论】:

  • “而且网上没有很多关于 8086 的组装教程。” 真的吗?你在防火墙后面的中国吗?如果您会说“优秀教程不多”,我可以同意,这很少见,但实际上对于 x86,您可以找到大量的东西,它甚至有专门的 SW 项目“emu8086”来给学生讲课(但是最好远离那个模拟器,如果可以的话,它不是高质量的)......只需搜索堆栈溢出标签 [x86-16] 应该会引导您找到许多答案中的工作示例(以及问题中的许多常见错误/陷阱) - 点击你的 Q 下的标签并检查一些..
  • 关于您的代码...服务int 21h, 0a从用户输入字符串需要不同结构的内存缓冲区,您对countchar的定义代替了第一个和第二个用于输入的实际缓冲区的字节。

标签: assembly masm x86-16


【解决方案1】:

int 21h,ah=0ah 将用户输入读入 DS:DX 的缓冲区。 DS:DX 指向的缓冲区的第一个字节是最大长度,然后是实际长度,然后是读取的字符。您需要在 len 之后立即定义 str;否则输入将覆盖countchar。您得到 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2021-03-03
    • 2019-12-29
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多