Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Draw a line using Bresenham's algorithm
- cpu 186
- org 100h
- start:
- ; Setup screen
- MOV AX, 0013h ; Screen 13h
- INT 10h
- MOV AX, 0A000h ; Screenseg = 0xA000
- MOV ES, AX
- .setPoints:
- ; Set X/Y Coords for drawing
- MOV AX, 297 ; X0
- MOV BX, 120 ; Y0
- MOV CX, 40 ; X1
- MOV DX, 25 ; Y1
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Start of actual line code ;
- calcDiff:
- .diffX:
- MOV DI, CX ; CX = x1
- SUB DI, AX ; DI = x1 - x0
- .diffY:
- MOV BP, DX ; DX = y1
- SUB BP, BX ; BP = y1 - y0
- pickAlgorithm: ; find abs(diffY) and abs(diffX)
- absDiffX:
- TEST DI, DI ; Check if it's signed
- JNS .isPositive
- .makePositive:
- NEG DI
- .isPositive:
- absDiffY:
- TEST BP, BP ; Check if it's signed
- JNS .isPositive
- .makePositive:
- NEG BP
- .isPositive:
- CMP BP, DI ; if (abs(diffY) < abs(diffX)) then goto xBasedLine
- JL xBasedLine
- ;;;;;;;;;;;;;;;;;;;;;
- ; Y-Based line code ;
- yBasedLine:
- CMP BX, DX ; if x0 > x1 swap start and end coords
- JLE .setScreenPos
- XCHG BX, DX ; Swap origin and destination so we draw
- XCHG AX, CX ; the line top to bottom
- .setScreenPos:
- MOV DS, BX ; Save y0 in DS for now
- ; Set first point of screen
- MOV SI, BX ; SI = BX*256 + BX*64 [BX = y0]
- SHL BX, 6 ; BX*64
- SHL SI, 8 ; BX*256
- ADD SI, BX ; SI = y0*320
- ADD SI, AX ; SI = x0 + (y0*320)
- MOV BX, DS ; restore y0 to BX
- .calcVars:
- MOV DI, CX ; dx = x1 - x0
- SUB DI, AX
- MOV BP, DX ; dy = y1 - y0
- SUB BP, BX
- MOV CX, 1 ; move X by 1 each loop (CX = xi)
- CMP DI, 0 ; if (dx < 0) then flip line
- JGE .doneFlip
- .flipLine:
- MOV CX, -1
- NEG DI ; dx = -dx
- .doneFlip:
- SAL DI, 1 ; DI = 2*dx
- MOV AX, DI ; D = 2*dx - dy
- SUB AX, BP
- SAL BP, 1 ; BP = 2*dy
- .nextPoint:
- MOV [ES:SI], BYTE 0Ah
- CMP AX, 0
- JLE .doneAdjustX
- .adjustX:
- ADD SI, CX ; x = x + xi
- SUB AX, BP ; D = D - 2*dy
- .doneAdjustX:
- ADD AX, DI ; D = D + 2*dx
- ADD SI, 320 ; y++
- INC BX ; y0++
- CMP BX, DX ; if (y0 < y1) goto nextPoint
- JL .nextPoint
- .doneLine:
- JMP end
- ;;;;;;;;;;;;;;;;;;;;;
- ; X-Based line code ;
- xBasedLine:
- CMP AX, CX ; if y0 > y1 swap start and end coords
- JLE .setScreenPos
- XCHG BX, DX ; Swap origin and destination so we draw
- XCHG AX, CX ; the line left to right.
- .setScreenPos:
- MOV DS, BX ; Save y0 in DS for now
- ; Set first point of screen
- MOV SI, BX ; SI = BX*256 + BX*64 [BX = y0]
- SHL BX, 6 ; BX*64
- SHL SI, 8 ; BX*256
- ADD SI, BX ; SI = y0*320
- ADD SI, AX ; SI = x0 + (y0*320)
- MOV BX, DS ; restore y0 to BX
- .calcVars:
- MOV DI, CX ; dx = x1 - x0
- SUB DI, AX
- MOV BP, DX ; dy = y1 - y0
- SUB BP, BX
- MOV DX, 320 ; inc one scanline per loop (DX = yi)
- CMP BP, 0 ; if (dy < 0) then flip line
- JGE .doneFlip
- .flipLine:
- MOV DX, -320
- NOT BP ; dy = -dy
- INC BP
- .doneFlip:
- SAL BP, 1 ; BP = 2*dy
- MOV BX, BP ; D = 2*dy - dx
- SUB BX, DI
- SAL DI, 1 ; DI = 2*dx
- .nextPoint:
- MOV [ES:SI], BYTE 0Ah
- CMP BX, 0
- JLE .doneAdjustY
- .adjustY:
- ADD SI, DX ; y = y + yi
- SUB BX, DI ; D = D - 2*dx
- .doneAdjustY:
- ADD BX, BP ; D = D + 2*dy
- INC SI ; x++
- INC AX ; x0++
- CMP AX, CX ; if (x0 < x1) goto nextPoint
- JL .nextPoint
- end:
- RET
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement