代码之家  ›  专栏  ›  技术社区  ›  mihaicata1205

TASM程序未打印任何内容

  •  2
  • mihaicata1205  · 技术社区  · 7 年前

    我编写了一个程序,用于计算TASM中整数数组的平均值,但控制台不会显示任何内容,即使算法似乎工作正常。 有人知道问题出在哪里吗?

    DATA SEGMENT PARA PUBLIC 'DATA'
    msg db "The average is:", "$"
    sir db 1,2,3,4,5,6,7,8,9
    lng db $-sir
    DATA ENDS
    
    
    CODE SEGMENT PARA PUBLIC 'CODE'
     MAIN PROC FAR
    ASSUME CS:CODE, DS:DATA
    PUSH DS
    XOR AX,AX
    PUSH AX
    MOV AX,DATA
    MOV DS,AX    ;initialization part stops here
    
    mov cx, 9
    mov ax, 0
    mov bx, 0
    sum:
    add al, sir[bx]  ;for each number we add it to al and increment the nr of 
      ;repetions
    inc bx
    loop sum
    
    idiv bx
    
    MOV AH, 09H   ;the printing part starts here, first with the text
    LEA DX, msg
    INT 21H
    
    mov ah, 02h  
    mov dl, al    ;and then with the value
    int 21h
    
    
    ret
    MAIN ENDP
    CODE ENDS
    END MAIN
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Fifoernik    7 年前
    idiv bx
    

    单词大小的划分将划分 DX:AX 按操作数中的值 BX . 您的代码未设置 DX 预先

    这个 最简单的 这里的解决方案是使用字节大小的除法 idiv bl 这将导致分裂 AX 按中的值 BL 将商保留在 AL 剩余部分 AH .

    数组中非常小的数字加起来就是45。这将导致商为5,余数为0。


    MOV AH, 09H   ;the printing part starts here, first with the text
    LEA DX, msg
    INT 21H
    
    mov ah, 02h  
    mov dl, al    ;and then with the value
    int 21h
    

    程序的这一部分有两个问题。

    • 当您要使用来自的结果时 AL公司 ,它已被DOS系统调用破坏,该调用将带值离开 AL="$" .
    • 要将结果显示为字符,仍需添加“0”。这将从5转换为“5”。

    此解决方案解决了所有这些问题:

    idiv bl
    push ax         ;Save quotient in AL
    
    lea dx, msg
    mov ah, 09h
    int 21h         ;This destroys AL !!!
    
    pop dx          ;Get quotient back straight in DL
    add dl, "0"     ;Make it a character
    mov ah, 02h
    int 21h
    
        2
  •  0
  •   Michael    7 年前

    idiv bx 将32位值除以 dx:ax 通过 bx . 因此,在除名之前,您需要签署extend ax 进入 dx ,您可以通过 cwd 指示

    另一个问题是您需要添加 '0' 到中的值 al (或 dl )之前 int 21h/ah=02h 以便将其转换为字符。请注意,此方法仅适用于单个数字值。


    你可能还想改变这一点 ret 在结束时 mov ax,4c00h / int 21h ,这是退出DOS程序的正确方法。