程序集 x86 - 绘制正方形图形模式的问题

Assembly x86 - problems with drawing a square graphic mode

我需要在程序集 x86 中绘制一个正方形。我知道如何画一个正方形,但我的问题是我有很多变量,我不想为每个 2 个变量(x 和 y)创建一个过程。我添加了一个定义 x 和 y 的示例。 这是我尝试做的:

proc ChangeColumn4Number4
    inc [FourthColumnArray + 3]
    mov [Player1Drawx], 85h
    mov [Player1Drawy], 27h
    jmp DrawPlayer1Disc
endp ChangeColumn4Number4

DrawPlayer1Loop:
    mov bh,0h
    mov cx,[Player1Drawx]
    mov dx,[Player1Drawy]
    mov al,[player1disccolor]
    mov ah,0ch
    int 10h
    add [Player1Drawx], 1h
    mov ax, dx
    add ax, 14h
    cmp dx, ax
    jl DrawPlayer1Loop

DrawPlayer1Disc: 
    mov bh, 0h
    mov dx, [Player1Drawy]
    add [Player1Drawy], 1h 
    mov ax, dx
    add ax, 14h
    cmp dx, ax
    jl DrawPlayer1Loop

本网站有人指导这样做,但它不起作用。 感谢您的帮助。

使用此代码作为模板、起点或原样:

;X          +0ah
;Y          +08h
;Size       +06h
;Color      +04h
drawSquare:
 push bp
 mov bp, sp

 pusha                      ;Push all, can replace with single pushes

 mov ax, WORD [bp+04h]   
 mov ah, 0ch                ;AH = 0ch, AL = Color

 xor bx, bx                 ;Page 0
 mov dx, WORD [bp+08h]      ;DX = Y coord

 mov si, WORD [bp+06h]      
 mov di, si
 add di, dx                 ;DI = Size + Y
 add si, WORD [bp+0ah]      ;SI = Size + X

__ds_drawSquare:
  mov cx, WORD [bp+0ah]     ;CX = X Coord

___ds_drawRow:

   int 10h

   add cx, 01h              ;Increment X coord
   cmp cx, si
  jb ___ds_drawRow          ;Stay in the same row if X < Initial X + Size 

  add dx, 01h               ;Increment Y coord
  cmp dx, di
 jb __ds_drawSquare         ;Keep drawing rows if Y < Initial Y + Size

 popa                       ;See pusha above

 pop bp
 ret 08h

代码非常简单,无需进一步解释。
参数在栈上传递,这里是一个使用案例

BITS 16

ORG 100h

mov ax, 0013h
int 10h

push 10
push 30
push 40
push 0ch
call drawSquare

push 20
push 80
push 30
push 06h
call drawSquare

push 200
push 100
push 60
push 09h
call drawSquare

push 270
push 140
push 50
push 07h
call drawSquare

push 10
push 30
push 40
push 0ch
call drawSquare

push 30
push 110
push 40
push 03h
call drawSquare

xor ah, ah
int 16h

mov ax, 4c00h
int 21h

产生

代码适用于 NASM,使其适应您使用的任何汇编器。


在视频缓冲区范围内绘图是的责任。

正在调整您的代码。这需要四个变量:左上坐标、颜色和正方形大小。我还展示了如何对正方形大小进行硬编码。我是盲写的,因为我没有任何 DOSBOX - 希望它有效,以便您可以使用它。

DrawSquare:
    mov dx,[Player1Drawy]       ;top edge
    mov di,[SideLength]         ;control y loop
    ;mov di,14h                  ;or this, if the side length is fixed

SquareYloop:
    mov cx,[Player1Drawx]       ;left edge
    mov si,[SideLength]         ;control x loop
    ;mov si,14h                  ;or this, if the side length is fixed

SquareXloop:
    push cx                     ;I don't know if these 4 pushes are necessary...
    push dx
    push si
    push di

    mov bh,0h                   ;video page
    mov al,[player1disccolor]   ;colour
    mov ah,0ch                  ;draw pixel function
    int 10h                     ;BIOS video interrupt

    pop di                      ;... or these 4 matching pops are necessary
    pop si                      ;depends on whether int 10h func corrupts them
    pop dx
    pop cx

    inc cx                      ;advance X position
    dec si                      ;count side of square to control the loop
    jne SquareXloop             ;next horizontal pixel

    inc dx                      ;advance Y position
    dec di                      ;count side of square to control the loop
    jne SquareYloop             ;next row