我最近開始在 TASM 中編碼,目前正在研究精靈。在我的程式中,我收到一個精靈并將其列印在螢屏上(圖形模式)。
該程式基本上作業正常,但列印的數字看起來很奇怪。應該在彼此正下方的像素看起來有些遙遠。
這就是我呼叫函式的方式:
push 10 ; amount of lines
push 10 ; length of each line
push 4 ; color
push 100 ; y
push 160 ; x
push offset card2
call Sprite_Figure
這是功能:
proc Sprite_Figure
push bp
mov bp, sp
push ax
push bx
push cx
push dx
push si
mov si, [bp 4] ; sprite offset
mov cx, [bp 14] ; number of lines
loop1:
mov dx, [bp 12] ; size of one line
loop2:
push dx
xor dx, dx
cmp [byte ptr si], 0
je continue
print:
mov bx, [bp 8] ; current row
mov ax, 320
mul bx
mov bx, ax ; calculation to get location of pixel on screen
add bx, [bp 6] ; x
mov ax, [bp 10] ; color
mov [es:bx], al ; color location, es val is 0A000h
continue:
pop dx
inc si ; next element in array
dec dx
inc [bp 6] ; right by one
cmp dx, 0
jne loop2
mov ax, 320
sub ax, [bp 12] ; the size of one line
add [bp 6], ax ; new line
inc [bp 8] ; one line down
dec cx
cmp cx, 0
jne loop1
pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret 12
endp Sprite_Figure
這是精靈在螢屏上的外觀:

有沒有人有任何想法?
uj5u.com熱心網友回復:
該代碼混合了兩種向下移動的方式!這解釋了為什么代碼在螢屏上以雙倍速率下降,值 320 被添加兩次。
基本上你有幾種方法來繪制你的字符矩陣:
- 一對嵌套回圈,您可以在其中改變X和Y坐標并每次計算螢屏上的地址。
- 一對嵌套回圈,您在其中計算一次螢屏上左上角像素的地址,然后根據需要更改該地址。
線
inc [bp 8] ; one line down
適合第一種方法,而線條
mov ax, 320 sub ax, [bp 12] ; the size of one line add [bp 6], ax ; new line
適合第二種方法。
方法1
因為內部回圈重復增加了X坐標,為了恢復,您必須減去增量的總數:
...
dec dx
jnz loop2
mov ax, [bp 12] ; Size of one line (number of increments)
sub [bp 6], ax ; X is now restored
inc [bp 8] ; Y one line down
dec cx
jnz loop1
方法2
坐標僅用于一次獲取 BX 中的地址。之后只有地址不同。回圈中的指令少得多,這意味著這是一種更有效的方法。
mov ax, 320
mul [word ptr bp 8] ; Y
mov bx, ax
add bx, [bp 6] ; X
mov si, [bp 4] ; sprite offset
mov cx, [bp 14] ; number of lines
mov al, [bp 10] ; color
loop1:
mov dx, [bp 12] ; size of one line
loop2:
cmp [byte ptr si], 0
je continue
mov [es:bx], al ; ES val is 0A000h
continue:
inc si ; next byte in character pattern
inc bx ; X right by one on screen
dec dx
jnz loop2
sub bx, [bp 12] ; Address of left side
add bx, 320 ; Address of new line
dec cx
jnz loop1
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486385.html
