对角线打印数字 [英] Print numbers diagonally in assembly
问题描述
我试图在汇编中对角显示0-9,但是输出将对角打印的数字放在窗口的中间.
I'm trying to display 0-9 diagonally in assembly, but the output puts my diagonally printed numbers in the middle of the window.
这是代码:
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
5
6
7
8
9
当前输出将打印该内容,但在窗口的中间.我尝试添加新的寄存器bh
,并在执行文件时使用它将光标置于当前位置.如何从光标开始显示它?我应该把它放在循环上并递增寄存器ah
吗?
The current output prints that, but on the middle of the window. I've tried adding a new register bh
and use it to put my cursor on my current position when I execute the file. How can I display it starting on my cursor? Should I put it on a loop and increment register ah
?
推荐答案
您当前的程序失败,因为您可怕地混合了2个系统功能,而这些功能恰好具有相同的功能编号02h,但希望在
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
您可以通过查看递增的DL
寄存器中的值,而不是使用单独的计数器来决定进行环回.
Instead of using a separate counter, you can decide about looping back by looking at the value in the incremented DL
register.
请注意,您使用了loop
指令,该指令取决于CX
寄存器,但仅初始化了它的CL
下半部分!这通常是程序崩溃的原因.
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.
编辑
鉴于DOSBox在要求显示字符10时同时发出回车符和换行符(请注意
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
这篇关于对角线打印数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!