1

我试图在汇编中的两点 (x1,y1) 和 (x2,y2) 之间画一条线,我决定使用递归画线算法而不是 Bresenham 的。我认为这个想法很简单并编写了代码,但由于某种原因它不起作用。它根本不会在屏幕上绘制任何像素,有时会引发非法指令异常。这是代码:

line_recursive proc
    pop dx            ; dx = y1
    pop cx            ; cx = x1
    pop bx            ; bx = y0
    pop ax            ; ax = x0

    cmp dx, bx       ; y1==y0
    jne version1
    cmp ax, cx       ; x1==x0
    je  version2
   version1:
    mov si,ax
    add si,cx
    mov di,bx
    add di,dx
    shr si, 1         ; si = (x1+x0)/2 = x
    shr di, 1         ; di = (y1+y0)/2 = y

    push ax
    push cx
    push dx
    mov ah,0Ch  ;print pixel.
    mov al,12
    mov cx,si
    mov dx,di
    int 10h
    pop dx
    pop cx
    pop ax

    ; stack for call for (x0,y0, x,y)
    push ax
    push bx
    push si
    push di
    ; stack for call for (x1,y1, x,y)
    push cx
    push dx
    push si
    push di

    call line_recursive     ; (x1,y1, x,y)
    call line_recursive     ; (x0,y0, x,y)
   version2:
    ret
line_recursive endp

在将几个数字推送到 dx、cx、bx 和 ax 后,我尝试在程序中调用它。它根本什么也没画,我看不出为什么?我知道我使用的像素打印速度很慢,但这对我目前的学习来说很好。我对汇编中递归的想法是完全错误的,还是有其他原因导致此代码不起作用?

4

1 回答 1

1

如果您坚持将调用参数放在堆栈上,那么 Frank Kotler 是对的,那么您有 2 个选择:

  1. 更改您的呼叫顺序
    • 这不是很好的选择,因为需要返回地址标签
  2. 在程序中更改参数获取
    • 这是更好的方法

选项1:

    push adr0
    push x0
    push y0
    push x1
    push y1
    jmp line_recursive
adr0:

选项 2:

tmp:    dw 0
line_recursive:
    pop eax           ; eax= return address - not shore if you are using 16/32 bit adress so for 16 bits should be only ax
    pop dx            ; dx = y1
    pop cx            ; cx = x1
    pop bx            ; bx = y0
    pop ax            ; ax = x0
    mov [cs:tmp],ax
    push eax          ; restore return adress - not shore if you are using 16/32 bit adress so for 16 bits should be only ax
    mov ax,[cs:tmp]   ; ax = x0

    cmp dx, bx       ; y1==y0
    jne version1
    cmp ax, cx       ; x1==x0
    je  version2

version1:

    mov si,ax
    add si,cx
    mov di,bx
    add di,dx
    shr si, 1         ; si = (x1+x0)/2 = x
    shr di, 1         ; di = (y1+y0)/2 = y

    push ax
    push cx
    push dx
    mov ah,0Ch  ;print pixel.
    mov al,12
    mov cx,si
    mov dx,di
    int 10h
    pop dx
    pop cx
    pop ax

    push ax ; stack for call for (x0,y0, x,y)
    push bx
    push si
    push di
    call line_recursive     ; (x1,y1, x,y)

    push cx ; stack for call for (x1,y1, x,y)
    push dx
    push si
    push di
    call line_recursive     ; (x0,y0, x,y)

version2:
    ret

不要忘记将我的代码转换为您的汇编规范

  • 顺便说一句,您忘记提及您使用的是哪一个,什么平台,操作系统,型号,....
于 2013-09-27T08:01:46.077 回答