flat assembler
Message board for the users of flat assembler.

Index > DOS > bresenham line drawing [it seems work]

Author
Thread Post new topic Reply to topic
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
kty11 09 Jan 2013, 03:03
(maybe) my first successful project!
(because showing register is actually print wrong result)

I don't know it is correctly run or run wrong

but anyhow, thanks for all feedback to my post!
I finally made first program that drawing a line!
Laughing

the source code is here

Code:
org 100h
init:
        push 0xa000
        pop es
        mov ax, 13h
        int 10h
main:
        call test_line
        call key_wait
        mov ax, 4c00h
        int 21h
key_wait:
        mov ah, 00h
        int 16h
        ret
test_line:
        push 0x000f
        push 0x000f
        push 0x000f
        push 0x00cc
        push 0x00cc
        call bresenham
        ret
bresenham:
         ;param push color, x0, y0, x1, y1
         ;                      +8  +6  +4
         ;local dx, dy, sx, sy, err, e2
         ;      -2  -4  -6
         push bp
         mov bp, sp
         sub sp, 12 ;for local var storage

         push word [bp+6];x1
         push word [bp+10];x0
         call distance
         ;ax = distance between x1, x0
         mov [bp-2], ax ;save to dx local var
         push word [bp+4];y1
         push word [bp+8];y0
         call distance
         ;ax = distance between y1, y0
         mov [bp-4], ax ;save to dy local var
.cmpx0x1:
         mov ax, [bp+10]; x0
         mov bx, [bp+6] ;x1
         cmp ax, bx     ; x0,x1
         jl .sx_1
.sx_n1:
         mov word [bp-6], -1;sx = -1 ;suspicious..
         jmp .cmpy0y1
.sx_1:
         mov word [bp-6], 1 ;sx = 1
.cmpy0y1:
         mov ax, [bp+8];y0
         mov bx, [bp+4];y1
         cmp ax, bx
         jl .sy_1
.sy_n1:
         mov word [bp-8], -1;sy=-1
         jmp .next
.sy_1:
         mov word [bp-8], 1;sy=1
.next:
         mov ax, [bp-2];dx
         mov bx, [bp-4];dy
         sub ax, bx
         mov word [bp-10], ax ;err = dx-dy
.loop:
         push word [bp+12];color
         push word [bp+10];x0
         push word [bp+8];y0
         call draw_dot
.cmp_x0x1:
         mov ax, [bp+10];x0
         mov bx, [bp+6];x1
         cmp ax,bx
         je .cmp_y0y1
         jmp .set_e2
.cmp_y0y1:
         mov ax, [bp+8];y0
         mov bx, [bp+4];y1
         cmp ax, bx
         je .loop_end
.set_e2:
         mov ax, word [bp-10];err
         shl ax,1 ;err*2
         mov word [bp-12],ax ;e2 = err*2
.cmp_e2_ndy:
         mov ax,[bp-4];dy
         mov cl, -1
         imul cl ;ax = -ax. dy = -dy
         mov bx,[bp-12];e2
         ;ax = -dy, bx = e2
         cmp bx, ax
         jng .cmp_e2_dx
         mov ax, [bp-10];err
         mov bx, [bp-4];dy
         sub ax, bx
         mov [bp-10], ax ;err = err-dy
         mov ax, [bp+10] ;x0
         mov bx, [bp-6];sx
         add ax, bx
         mov [bp+10], ax ;x0 = x0+sx
.cmp_e2_dx:
         mov ax, [bp-12];e2
         mov bx, [bp-2];dx
         cmp ax, bx
         jge .endcmp
         mov ax, [bp-10];err
         mov bx, [bp-2];dx
         add ax, bx
         mov [bp-10],ax ; err = err+dx
         mov ax, [bp+8] ;y0
         mov bx, [bp-8] ;sy
         add ax, bx
         mov [bp+8],ax
.endcmp:
         jmp .loop
.loop_end:
         leave
         ret 10
draw_dot:
        ;push color, x, y
        push bp
        mov bp, sp
        ;bp = call's push
        mov dx, [bp+4]  ;dx = y (max 200)
        mov cx, [bp+6]  ;cx = x (max 320)
        mov bx, [bp+8]  ;bx = color (use bl only)
        mov ax, 320d       ;ax = width
        imul dx, ax
        add dx, cx
        mov di, dx
        mov [es:di], bl  ;not bx but bl.
        leave
        ret 6
distance:
        ;push a,b
        push bp
        mov bp, sp
        mov ax, [bp+6];a
        mov bx, [bp+4];b
        cmp ax, bx
        jge distance.a_b
        sub bx, ax
        mov ax, bx
        jmp distance.end
.a_b:
        sub ax, bx
.end:
        leave
        ret 4

    


Description:
Filesize: 10.12 KB
Viewed: 5891 Time(s)

bresenham asm.png



_________________
since 1990 November 4th
Post 09 Jan 2013, 03:03
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 09 Jan 2013, 09:03
good start, pc assembly always starts with iterative division to trace lines Smile

next is to boot from floppy image Smile
Post 09 Jan 2013, 09:03
View user's profile Send private message Visit poster's website Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 70
Location: Belgium
SeproMan 13 Jan 2013, 15:28
kty11,

The video mode 13h has a resolution of 320x200 and you are drawing a line to a point off screen! You have set (x1,y1)=(204,204). Take care because such things are not always harmless.

At ".cmp_e2_ndy" your comment states that "AX=-AX" where in fact AX becomes -AL.
This will become a problem on a video resolution with more than 256 lines. Simply use "neg ax"

Are you aware that the line you've drawn is actually a special case? 45°
You should try many different lines to verify if your program is correct.

Since you like using the instruction "leave" why not consider using "enter" also?
You can replace "push bp : mov bp,sp : sub sp,12" by "enter 12,0" (and shave off 2 bytes)

_________________
Real Address Mode.
Post 13 Jan 2013, 15:28
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1676
Location: Toronto, Canada
AsmGuru62 13 Jan 2013, 16:02
That does not look like a correct 45 degree line.
First I see 2 pixels on top of each other, then same pixels shifted down
and the pattern repeats. A correct line would be where a single pixels
are connected with their corners. This above line looks like TWO lines
of 45 drawn side by side.
Post 13 Jan 2013, 16:02
View user's profile Send private message Send e-mail Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 70
Location: Belgium
SeproMan 13 Jan 2013, 18:19
AsmGuru62,

Your observation is correct but it undoubtedly relates to the fact that the picture is taken from a windowed DosBox. On a true 320x200 screen it'll probably look fine.

_________________
Real Address Mode.
Post 13 Jan 2013, 18:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.