我有一个简单的绘图程序,当按箭头键时,像素“蛇”会向该方向移动。我的问题是该程序可以在 200x255 像素上运行,但我想让它在 200x 320 像素上运行。所以我需要将 x 坐标存储在 16 位寄存器中,而不是 8 位寄存器中(最大 255 像素)。我尝试重写它,但由于像素计算,这个东西对我来说有点太先进了,而且我找不到帮助。
Draw: //I calculate the pixel position like Pixel = Y * 320 + X
pop dx
xor ah, ah
mov al, dh
push dx
mov bx, 320
mul bx
pop dx
add al, dl
jnc Pixel
inc ah
Pixel: //Color and stuff
push dx
mov di, ax
mov al, [si]
mov es:[di], al
//我读取输入键,然后决定在哪里显示像素
Left:
pop dx
dec dl
cmp dl, 1
jnc Store
inc dl
jmp Store
Right: //I need 320 and 16 bit register here instead of dl
pop dx
inc dl
cmp dl, 250
jc Store
dec dl
jmp Store
Up:
pop dx
dec dh
cmp dh, 1
jnc Store
inc dh
jmp Store
Down:
pop dx
inc dh
cmp dh, 200
jc Store
dec dh
jmp Store
Store:
push dx
jmp Draw
非常感谢您的帮助
最佳答案
How to make drawing program to work on 200x320 pixels (instead of 200x255) assembly?
这些决议看起来很陌生。通常我们会将其指定为 XRes x YRes。
您当前的程序使用字节大小的寄存器DL
表示X,DH
表示Y。这适合您的255x200分辨率,其中单个值不得超过255。如果您如果想要转向更大的 320x200 分辨率,则使用字大小的寄存器 CX
表示 X,使用 DX
表示 Y。这些也是 BIOS 为指定而做出的选择坐标。随波逐流总是很高兴!
很难理解如何使用堆栈来存储坐标。我认为你可以简化这个......
下一个解决方案期望 (X,Y) 保持在一个尊重屏幕周围 5 个像素边框的矩形内。根据需要更改数字:
XRES equ 320
YRES equ 200
BORDER equ 5 ; Resolution 320x200 --> X=[5,314] Y=[5,194]
; assuming CX contains X and DX contains Y
Left:
cmp cx, BORDER + 1
cmc
sbb cx, 0
jmp Draw
Right:
cmp cx, XRES - BORDER - 1
adc cx, 0
jmp Draw
Up:
cmp dx, BORDER + 1
cmc
sbb dx, 0
jmp Draw
Down:
cmp dx, YRES - BORDER - 1
adc dx, 0
jmp Draw
使用字大小的寄存器,地址的计算现在要简单得多:
Draw:
push dx
mov ax, XRES
mul dx ; DX:AX == Y * 320
pop dx
add ax, cx ; AX == Y * 320 + X
mov di, ax
...
如果您不限于 8086那么这就变成:
Draw:
imul di, dx, XRES ; DI == Y * 320
add di, cx ; DI == Y * 320 + X
...
关于assembly - 如何使绘图程序在 200x320 像素(而不是 200x255)组件上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64736714/