装配x86 - 绘制方形图形模式的问题

时间:2016-06-25 16:25:01

标签: assembly x86 square

我需要在程序集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

本网站的某位导演以这种方式指导,但它并不起作用。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

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

;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

哪个产生

Squares on the screen

代码适用于NASM,适用于您使用的任何汇编程序。

在视频缓冲区范围内绘制您的责任。

答案 1 :(得分:2)

调整您的代码。这需要四个变量:左上角坐标,颜色和方形大小。我还展示了如何对方形大小进行硬编码。我写了它是盲目的,因为我没有任何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