You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

x86汇编程序中output数组无法输出到屏幕的问题求助

问题修复方案

核心错误点

  1. 打印无输出的直接原因:你使用DOS 09H功能打印字符串时,会从DX指向的首字符开始输出,遇到$就终止。你在Prep_Output里把结果十位、个位存到了output[1]和output[2],而output[0]还是初始化的$,等于打印时第一个字符就是终止符,自然没有内容输出。
  2. 输入缓冲区结构不符合DOS 0AH调用要求:0AH输入的缓冲区规范为:第一个字节存最大允许输入字符数,第二个字节由DOS自动回写实际输入的字符数,第三个字节开始才是输入的字符内容。你当前的定义错误地把预设的最大长度当成了实际输入长度读取。
  3. 输入字符索引偏移错误:按照你当前的缓冲区定义,输入的第一个字符实际存储在Chars[0],你读取时用了Chars[1]、Chars[2],拿到的是错误的字符。

修改后的完整代码

.model small
.stack 64
.data
    Prompt     DB    'Enter a number between 10 and 49 number:  ', '$'
    Prompt2    DB    'Enter another number between 10 and 49 number:  ', '$'
    ; 修正输入缓冲区结构,符合0AH调用要求
    StrPar     DB    4          ; 最大输入长度
    Actual_Len DB    ?          ; DOS回写实际输入长度
    Chars      DB    4 DUP('$') ; 存储输入字符
    output     DB    4 DUP('$') ; 输出缓冲区

.code
MAIN proc FAR
     mov  AX,@data
     mov  DS,AX

     call Clear
     call Set_Cursor
     call Request_Input
     call Get_Input
     call Add_Input  
     call Clear
     call Set_Cursor
     call Request_Input2
     call Get_Input
     call Clear
     call Set_Cursor
     call Add_Input2    
     call Prep_Output
     call Print_Output 

     mov  AX,4C00h
     int  21h
MAIN  endp

Clear proc NEAR 
      mov AX, 06H
      mov AL, 00H
      mov BH, 71H
      mov CX, 0000H
      mov DX, 184FH
      int 10H
      ret
Clear endp

Set_Cursor proc NEAR
    mov AH, 02H
    mov BH, 0
    mov DH, 10
    mov DL, 5
    int 10H
    ret
Set_Cursor endp

Request_Input proc NEAR
    mov AH, 09H
    lea DX,Prompt
    int 21H
    ret
Request_Input endp

Request_Input2 proc NEAR
    mov AH, 09H
    lea DX,Prompt2
    int 21H
    ret
Request_Input2 endp

Get_Input proc NEAR
    mov AH, 0AH
    lea DX, StrPar
    int 21H
    mov BH,00
    ; 修正:读取DOS回写的实际输入长度
    mov BL,Actual_Len
    ; 输入字符末尾加$终止符
    mov Chars[BX],'$'
    ret
Get_Input endp

Add_Input proc NEAR 
    mov AH, 00
    mov DX, 00
    ; 修正:输入第一个字符在Chars[0](十位)
    mov BX,0
    mov DL, Chars[BX]
    sub DL,48 
    mov AX,10
    mul DX
    mov DX,AX
    ; 修正:输入第二个字符在Chars[1](个位)
    mov BX,1
    mov CX,00
    mov CL, Chars[BX]
    sub CX,48 
    add DX,CX
    ret
Add_Input endp

Add_Input2 proc Near
    mov AX,00
    ; 修正:输入第一个字符在Chars[0](十位)
    mov BX,0
    mov CL, Chars[BX]
    sub CX, 48 
    mov AX,10
    mul CX
    mov CX,AX
    ; 修正:输入第二个字符在Chars[1](个位)
    mov BX,1
    mov BL, Chars[BX]
    sub BL,48
    mov BH,00
    add CX,BX
    ret
Add_Input2 endp

Prep_Output proc NEAR
    mov AX,00
    add DX,CX 
    mov AX,DX
    mov BX,10 
    div BL
    add AL,48
    add AH,48
    ; 修正:把结果从output[0]开始存,保证打印时不会第一个字符就是$
    mov BX, 0   
    mov output[BX],AL
    mov BX, 1
    mov output[BX],AH
    ret
Prep_Output endp

Print_Output proc NEAR
    mov AH, 09H
    lea DX, output
    int 21H 
    ret 
Print_Output endp

end MAIN

内容的提问来源于stack exchange,提问作者user14745176

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 07:36:07