Print numbers diagonally in assembly

梦想与她 提交于 2019-11-29 18:40:15

Your current program fails because you horribly mix 2 system functions that happen to have the same function number 02h, but that expect to receive totally different information in the DL register. The DOS OutputCharacter function expects a character code and you set it at 48, but the BIOS SetCursor function will interpret the same value 48 as the column. That's why the results are displayed in the middle of the screen!

Since you say you want to start from the current cursor position, which will nearly always be at the left edge of the screen at program start, there's no need to set the cursor position at all.

    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

Instead of using a separate counter, you can decide about looping back by looking at the value in the incremented DL register.


Notice that you used the loop instruction which depends on the CX register but that you only initialized the CL bottom half of it! That's often a reason of program crashing.


EDIT

Given that DOSBox emits both Carriage return and Linefeed when asked to display character 10 (brought to my attention in this comment by Michael Petch), I wrote this next little program that I tested for accuracy in the latest DOSBox available which is version 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!