首页 文章

在装配中对角打印数字

提问于
浏览
0

我试图在装配中对角显示0-9,但是输出将我对角线打印的数字放在窗口的中间 .

这是代码:

start:
mov ah, 02h 
mov cl, 0Ah ;counter (10)
mov dx, 02h
;mov bx, 02h
mov dl, 30h ;start printing 0-9
mov dh, 02h ;start row
mov al, 02h
int 21h

again:

int 10h
int 21h
;add dx, 01h
inc dh
inc dx
inc al

loop again
mov ax, 4c00h
int 21h

输出应该是:

0
1
2
3
4

6
7
8
9

当前输出打印出来,但是在窗口的中间 . 我尝试添加一个新的寄存器 bh 并在执行文件时使用它将光标放在我当前的位置 . 如何从光标开始显示它?我应该把它放在循环上并增加寄存器 ah 吗?

1 回答

  • 2

    您当前的程序失败是因为您可能混合了两个碰巧具有相同功能编号02h的系统功能,但希望在 DL 寄存器中接收完全不同的信息 . DOS OutputCharacter函数需要一个字符代码并将其设置为48,但BIOS SetCursor函数将解释与列相同的值48 . 这就是结果显示在屏幕中间的原因!

    因为您说要从当前光标位置开始,在程序启动时几乎总是位于屏幕的左边缘,所以根本不需要设置光标位置 .

    mov     ah, 02h
        mov     dl, "0"
    Next:
        push    dx          ;Preserve current character
        int     21h
        mov     dl, " "     ;Your desired output shows this space?
        int     21h
        mov     dl, 10      ;Linefeed moves the cursor 1 line down
        int     21h
        pop     dx          ;Restore current character
        inc     dl
        cmp     dl, "9"
        jbe     Next
    

    您可以通过查看递增的 DL 寄存器中的值来决定循环返回,而不是使用单独的计数器 .


    请注意,您使用的 loop 指令取决于 CX 寄存器,但您只是初始化了 CL 的下半部分!这通常是程序崩溃的原因 .


    EDIT

    鉴于DOSBox在被要求显示字符10时会发出回车符和换行符(在this comment by Michael Petch引起我的注意),我写了下一个小程序,我在最新的DOSBox中测试了它的准确性,即版本0.74 .

    ORG     256          ;Create .COM program
    
        mov     ah, 02h      ;DOS.DisplayCharacter
        mov     dx, "0"      ;DH is spaces counter, DL is current character
        jmps    First        ;Character "0" has no prepended spaces!
    Next:
        push    dx           ;(1)
        mov     dl, " "
    Spaces:
        int     21h
        dec     dh
        jnz     Spaces
        pop     dx           ;(1)
    First:
        int     21h          ;Display character in DL
        push    dx           ;(2)
        mov     dl, 10       ;Only on DOSBox does this do Carriage return AND Linefeed !
        int     21h
        pop     dx           ;(2)
        add     dx, 0201h    ;SIMD : DH+2 and DL+1
        cmp     dl, "9"
        jbe     Next
    
        mov     ax, 4C00h    ;DOS.TerminateWithExitcode
        int     21h
    

相关问题