Replacing unnecessary MUL
instructions with ADD
instructions in order to increase performance.
; AX = x1, BX = y1, CX = x2, DX = y2
draw_line:
pusha
mov dword [.pos], 0
mov [.x1], ax ; store values in memory
mov [.y2], bx
mov [.x2], cx
mov [.y1], dx
sub cx, ax
sub dx, bx
mov ax, cx
call to_abs
mov [.w], ax
mov ax, dx
call to_abs
mov [.h], ax
mov ax, [.x1] ; do comparisons to check on which diagonal is the line
mov bx, [.y1]
mov cx, [.x2]
mov dx, [.y2]
sub cx, ax
sub dx, bx
and cx, 1000000000000000b
and dx, 1000000000000000b
cmp cx, dx
je .topleft
mov byte [.dg], 1
jmp .continue1
.topleft:
mov byte [.dg], 2
.continue1:
mov ax, [.x1] ; set values for x and y (top left corner of the rectangle container)
mov bx, [.x2]
call minimum
mov [.x], ax
mov ax, [.y1]
mov bx, [.y2]
call minimum
mov [.y], ax
mov ax, [.w]
mov bx, [.h]
cmp ax, bx
je .diag
jge .horiz
.verti:
mov ax, 0
mov dx, [.w]
div word [.h]
mov [.t], ax
mov bx, 0
.loop1:
cmp bx, [.h]
je .end
mov eax, 0
mov ax, [.t]
add [.pos], eax
mov eax, [.pos]
shr eax, 16
mov dx, ax
cmp byte [.dg], 2
je .chg1
.continue2:
mov cx, dx
mov dx, bx
add cx, [.x]
add dx, [.y]
call draw_pixel
inc bx
jmp .loop1
.chg1:
push ax
mov ax, [.w]
sub ax, dx
mov dx, ax
pop ax
jmp .continue2
.horiz:
mov ax, 0
mov dx, [.h]
div word [.w]
mov [.t], ax
mov bx, 0
.loop2:
cmp bx, [.w]
je .end
mov eax, 0
mov ax, [.t]
add [.pos], eax
mov eax, [.pos]
shr eax, 16
mov dx, ax
cmp byte [.dg], 2
je .chg2
.continue3:
mov cx, bx
add cx, [.x]
add dx, [.y]
call draw_pixel
inc bx
jmp .loop2
.chg2:
push ax
mov ax, [.h]
sub ax, dx
mov dx, ax
pop ax
jmp .continue3
.diag:
mov bx, 0
.loop3:
cmp bx, [.h]
je .end
mov dx, bx
mov cx, bx
cmp byte [.dg], 2
je .chg3
.continue4:
add cx, [.x]
add dx, [.y]
call draw_pixel
inc bx
jmp .loop3
.chg3:
push ax
mov ax, [.h]
sub ax, dx
mov dx, ax
pop ax
jmp .continue4
.end:
popa
ret
.x1 dw 0
.y1 dw 0
.x2 dw 0
.y2 dw 0
.x dw 0
.y dw 0
.w dw 0
.h dw 0
.t dw 0
.dg db 0
.pos dd 0