flat assembler
Message board for the users of flat assembler.

flat assembler > DOS > failed line drawing

Author
Thread Post new topic Reply to topic
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
as advice suggested, I keep everything in a text file.
and post comments to many routine.
I also try indentation.
but again, it don't draw a line... (though dotting a pixel works)
it takes several hours Crying or Very sad
I follow wikipedia's line drawing algorithms
http://en.wikipedia.org/wiki/Line_drawing_algorithm
welcome any feedback or suggestion
here's my code

thanks!

Code:
org 100h ;all assume es = 0xa000 ;require mov ax 13h int 10h to work init: push 0xa000 pop es mov ax, 13h int 10h main: ;push color, dx, dy, x1, y1, x2, y2, x call test_dot ;works call test_draw_dot_on_line ;something go wrong call key_wait call quit test_draw_dot_on_line: push 0x000f;color push 0x0010;dx push 0x0010;dy push 0x0010;x1 push 0x0010;y1 push 0x0020;x2 push 0x0020;y2 push 0x0002;x call draw_dot_on_line sub sp, 16 ret test_line: push 0x000f push 0x0000 push 0x0000 push 0x0010 push 0x0010 call draw_line sub sp, 10 ret test_dot: push 0x000f push 0x0000 push 0x0000 call draw_dot sub sp, 6 push 0x0004 push 0x0001 push 0x0001 call draw_dot sub sp, 6 ret key_wait: mov ah, 00h int 16h ret quit: mov ax, 4c00h int 21h draw_line: ;push color, x1, y1, x2, y2 ;call draw_dot_on_line with successive x ;x2 must be equal to or greater than x1 .init: mov si, sp mov cx, 0 .loop: push cx mov ax, [ss:si+8] mov bx, [ss:si+4] add ax, cx ;cx increases as loop runs cmp ax, bx jge draw_line_end push word [ss:si+10] ;color mov ax, [ss:si+4] ;x2 mov bx, [ss:si+8] ;x1 sub ax, bx push ax ;dx mov ax, [ss:si+2] ;y2 mov bx, [ss:si+6] ;y1 sub ax, bx push ax ;dy push word [ss:si+8] ;x1 push word [ss:si+6] ;y1 push word [ss:si+4] ;x2 push word [ss:si+2] ;y2 push cx call draw_dot_on_line sub sp, 16 pop cx jmp draw_line.loop draw_line_end: pop cx ret draw_dot_on_line: ;push color, dx, dy, x1, y1, x2, y2, x ;calculate y and plot mov si, sp mov ax, [ss:si+2] ;x mov bx, [ss:si+10];x1 sub ax, bx ;ax = x-x1 mov bx, [ss:si+14];dx div bx ;ax = (x-x1)/dx mov bx, [ss:si+12];dy imul ax, bx ;ax = dy*(x-x1)/dx mov bx, [ss:si+8] add ax, bx ;ax = y1 + dy * (x-x1)/dx push word [ss:si+16] ;color push word [ss:si+2] ;x push ax ;y call draw_dot sub sp, 6 ret draw_dot: ;push color, x, y mov si, sp ;ss:si = call's push mov dx, [ss:si+2] ;dx = y (max 200) mov cx, [ss:si+4] ;cx = x (max 320) mov bx, [ss:si+6] ;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. ret
[/url]

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



Joined: 16 Jun 2003
Posts: 3475
Location: Bulgaria
In order to remove the parameters from the stack you have to "add sp, const" not to "sub".

Although:
1. don't use CCALL convention. use "ret N" (STDCALL) or simple register argument passing. It will save you from troubles.

2. "call quit" is pretty useless in this context. It will not make the program clear. Use direct instructions in place.

3. use BP register in order to access parameters in the stack - BP works on SS by default.
Post 08 Jan 2013, 05:45
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
freecrac



Joined: 19 Oct 2011
Posts: 117
Location: Germany Hamburg
kty11 wrote:
as advice suggested, I keep everything in a text file.
and post comments to many routine.
I also try indentation.
but again, it don't draw a line... (though dotting a pixel works)
it takes several hours Crying or Very sad
I follow wikipedia's line drawing algorithms
http://en.wikipedia.org/wiki/Line_drawing_algorithm
welcome any feedback or suggestion
here's my code

thanks!


A Bresenham line algorithm. http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
Code:
;---------------------------------------------------------------------------- ; L I N I E Startpoint: BX, SI Endpoint: CX, DI Segment: FS Color: AL ;---------------------------------------------------------------------------- 0000 8B D7 LINE: mov dx, di 0002 2B CB sub cx, bx 0004 7C 4A jl short T0 0006 67| 03 1C75 00000000 add bx, [esi*2] 000E 90 nop 000F 90 nop 0010 2B D6 sub dx, si 0012 0F 8C 0150 R jl T1 0016 BE 0141 mov si, Max_X + 1 0019 90 nop 001A 90 nop 001B 90 nop 001C 90 nop 001D 90 nop 001E 3B CA cmp cx, dx 0020 0F 8C 01D0 R jl T2 0024 03 D2 add dx, dx 0026 90 nop 0027 8B EA mov bp, dx 0029 2B D1 sub dx, cx 002B 90 nop 002C 8B FA mov di, dx 002E 2B D1 sub dx, cx ;-------------- 0030 64: 88 07 M00: mov fs:[bx], al 0033 23 FF and di, di 0035 7D 09 jge short M01 0037 43 inc bx 0038 03 FD add di, bp 003A 49 dec cx 003B 75 F3 jnz M00 003D C3 ret ;------------------------------------------------------------- 0040 03 DE M01: add bx, si 0042 03 FA add di, dx 0044 49 dec cx 0045 75 E9 jnz M00 0047 C3 ret ;---------------------------------------------------------------------------- 0050 F7 D9 T0: neg cx 0052 67| 03 1C75 00000000 add bx, [esi*2] 005A 2B D6 sub dx, si 005C BE 0141 mov si, Max_X + 1 005F 7C 7F jl short T01 0061 90 nop 0062 90 nop 0063 BE 013F mov si, Max_X - 1 0066 3B CA cmp cx, dx 0068 7C 36 jl short T02 006A 03 D2 add dx, dx 006C 90 nop 006D 90 nop 006E 90 nop 006F 90 nop 0070 90 nop 0071 8B EA mov bp, dx 0073 2B D1 sub dx, cx 0075 90 nop 0076 90 nop 0077 90 nop 0078 90 nop 0079 90 nop 007A 8B FA mov di, dx 007C 90 nop 007D 90 nop 007E 2B D1 sub dx, cx ;-------------- 0080 64: 88 07 M02: mov fs:[bx], al 0083 23 FF and di, di 0085 7D 09 jge short M03 0087 4B dec bx 0088 03 FD add di, bp 008A 49 dec cx 008B 75 F3 jnz M02 008D C3 ret ;------------------------------------------------------------- 0090 03 DE M03: add bx, si 0092 03 FA add di, dx 0094 49 dec cx 0095 75 E9 jnz M02 0097 C3 ret ;---------------------------------------------------------------------------- 00A0 03 C9 T02: add cx, cx 00A2 90 nop 00A3 90 nop 00A4 90 nop 00A5 90 nop 00A6 90 nop 00A7 8B E9 mov bp, cx 00A9 2B CA sub cx, dx 00AB 90 nop 00AC 8B F9 mov di, cx 00AE 2B CA sub cx, dx ;-------------- 00B0 64: 88 07 M04: mov fs:[bx], al 00B3 23 FF and di, di 00B5 7D 19 jge short M05 00B7 81 C3 0140 add bx, Max_X 00BB 03 FD add di, bp 00BD 4A dec dx 00BE 75 F0 jnz M04 00C0 C3 ret ;------------------------------------------------------------- 00D0 03 DE M05: add bx, si 00D2 03 F9 add di, cx 00D4 4A dec dx 00D5 75 D9 jnz M04 00D7 C3 ret ;---------------------------------------------------------------------------- 00E0 F7 DA T01: neg dx 00E2 3B CA cmp cx, dx 00E4 7C 2A jl short T03 00E6 03 D2 add dx, dx 00E8 8B EA mov bp, dx 00EA 2B D1 sub dx, cx 00EC 8B FA mov di, dx 00EE 2B D1 sub dx, cx ;-------------- 00F0 64: 88 07 M06: mov fs:[bx], al 00F3 23 FF and di, di 00F5 7D 09 jge short M07 00F7 4B dec bx 00F8 03 FD add di, bp 00FA 49 dec cx 00FB 75 F3 jnz M06 00FD C3 ret ;------------------------------------------------------------- 0100 2B DE M07: sub bx, si 0102 03 FA add di, dx 0104 49 dec cx 0105 75 E9 jnz M06 0107 C3 ret ;---------------------------------------------------------------------------- 0110 03 C9 T03: add cx, cx 0112 90 nop 0113 90 nop 0114 90 nop 0115 90 nop 0116 90 nop 0117 8B E9 mov bp, cx 0119 2B CA sub cx, dx 011B 90 nop 011C 8B F9 mov di, cx 011E 2B CA sub cx, dx ;-------------- 0120 64: 88 07 M08: mov fs:[bx], al 0123 23 FF and di, di 0125 7D 19 jge short M09 0127 81 EB 0140 sub bx, Max_X 012B 03 FD add di, bp 012D 4A dec dx 012E 75 F0 jnz M08 0130 C3 ret ;------------------------------------------------------------- 0140 2B DE M09: sub bx, si 0142 03 F9 add di, cx 0144 4A dec dx 0145 75 D9 jnz M08 0147 C3 ret ;---------------------------------------------------------------------------- 0150 F7 DA T1: neg dx 0152 BE 013F mov si, Max_X - 1 0155 90 nop 0156 90 nop 0157 90 nop 0158 90 nop 0159 3B CA cmp cx, dx 015B 7C 33 jl short T11 015D 03 D2 add dx, dx 015F 90 nop 0160 90 nop 0161 90 nop 0162 90 nop 0163 90 nop 0164 8B EA mov bp, dx 0166 2B D1 sub dx, cx 0168 90 nop 0169 90 nop 016A 90 nop 016B 90 nop 016C 8B FA mov di, dx 016E 2B D1 sub dx, cx ;-------------- 0170 64: 88 07 M10: mov fs:[bx], al 0173 23 FF and di, di 0175 7D 09 jge short M11 0177 43 inc bx 0178 03 FD add di, bp 017A 49 dec cx 017B 75 F3 jnz M10 017D C3 ret ;------------------------------------------------------------- 0180 2B DE M11: sub bx, si 0182 03 FA add di, dx 0184 49 dec cx 0185 75 E9 jnz M10 0187 C3 ret ;---------------------------------------------------------------------------- 0190 03 C9 T11: add cx, cx 0192 90 nop 0193 90 nop 0194 90 nop 0195 90 nop 0196 90 nop 0197 8B E9 mov bp, cx 0199 2B CA sub cx, dx 019B 90 nop 019C 8B F9 mov di, cx 019E 2B CA sub cx, dx ;-------------- 01A0 64: 88 07 M12: mov fs:[bx], al 01A3 23 FF and di, di 01A5 7D 19 jge short M13 01A7 81 EB 0140 sub bx, Max_X 01AB 03 FD add di, bp 01AD 4A dec dx 01AE 75 F0 jnz M12 01B0 C3 ret ;------------------------------------------------------------- 01C0 2B DE M13: sub bx, si 01C2 03 F9 add di, cx 01C4 4A dec dx 01C5 75 D9 jnz M12 01C7 C3 ret ;---------------------------------------------------------------------------- 01D0 03 C9 T2: add cx, cx 01D2 90 nop 01D3 90 nop 01D4 90 nop 01D5 90 nop 01D6 90 nop 01D7 8B E9 mov bp, cx 01D9 2B CA sub cx, dx 01DB 90 nop 01DC 8B F9 mov di, cx 01DE 2B CA sub cx, dx ;-------------- 01E0 64: 88 07 M14: mov fs:[bx], al 01E3 23 FF and di, di 01E5 7D 19 jge short M15 01E7 81 C3 0140 add bx, Max_X 01EB 03 FD add di, bp 01ED 4A dec dx 01EE 75 F0 jnz M14 01F0 C3 ret ;------------------------------------------------------------- 0200 03 DE M15: add bx, si 0202 03 F9 add di, cx 0204 4A dec dx 0205 75 D9 jnz M14 0207 C3 ret ;----------------------------------------------------------------------------

Dirk
Post 08 Jan 2013, 06:55
View user's profile Send private message Send e-mail Reply with quote
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
I catch the bug!

Code:
org 100h mov ax, 10 mov bx, 10 div bx add ax, '0' int 29h mov ax, 00h int 16h mov ax, 4c00h int 21h

this crash on div instruction

but following code doesn't crash!
Code:
org 100h mov dx, 0 mov ax, 10 mov bx, 10 div bx add ax, '0' int 29h mov ax, 00h int 16h mov ax, 4c00h int 21h

_________________
since 1990 November 4th
Post 08 Jan 2013, 07:43
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3475
Location: Bulgaria
div instruction divides 32bit value in DX:AX, not the single AX. So, you have to put valid number in DX.
Post 08 Jan 2013, 09:32
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
this code works sometimes and sometimes not
I don't know why Very Happy

Code:
org 100h ;naive line drawing algorithm init: push 0xa000 pop es mov ax, 13h int 10h main: call test_line call key_wait mov ax, 4c00h int 21h test_line: push 0x000f push 0x0003 push 0x001f push 0x001f push 0x001f call draw_line ret key_wait: mov ah, 00h int 16h ret draw_line: ;push color, x1, y1, x2, y2 ;call draw_dot_on_line with successive x ;x2 must be equal to or greater than x1 .init: push bp mov bp, sp mov cx, 0 .loop: push cx mov ax, [bp+8] mov bx, [bp+6] add ax, cx ;cx increases as loop runs cmp ax, bx jge draw_line.end push word [bp+12] ;color mov ax, [bp+6] ;x2 mov bx, [bp+10] ;x1 sub ax, bx push ax ;dx mov ax, [bp+4] ;y2 mov bx, [bp+8] ;y1 sub ax, bx push ax ;dy push word [bp+10] ;x1 push word [bp+8] ;y1 push word [bp+6] ;x2 push word [bp+4] ;y2 push cx call draw_dot_on_line ;clean up 16bytes with ret 16 pop cx inc cx jmp draw_line.loop .end: leave pop cx ret 10 draw_dot_on_line: ;push color, dx, dy, x1, y1, x2, y2, x ;calculate y and plot push bp mov bp, sp push word [bp+18] ;color mov ax, [bp+4] ; x add ax, [bp+12] ; x1 push ax mov ax, [bp+4] ;x mov bx, [bp+12];x1 sub ax, bx ;ax = x-x1 mov bx, [bp+14];dy imul ax, bx ;ax = dy*(x-x1) mov dx, 0 mov bx, [bp+16];dx div bx ;ax = dy*(x-x1)/dx mov bx, [bp+10] add ax, bx ;ax = y1 + dy * (x-x1)/dx push ax ;y call draw_dot leave ret 16 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


thanks for the feed backs!

_________________
since 1990 November 4th
Post 08 Jan 2013, 14:00
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
kty11,

Examine closely draw_line()'s stack:
Code:
.init: push bp mov bp, sp ;;; Call frame offsets: ;;; bp+2: return address ;;; bp+4: y2 ;;; bp+6: y1 ;;; bp+8: x2 ;;; bp+10: x1 ;;; bp+12: color mov cx, 0 .loop: push cx mov ax, [bp+8];;; x2 mov bx, [bp+6];;; y1 add ax, cx ;cx increases as loop runs cmp ax, bx jge draw_line.end
There are more errors: e.g. you do push cx inside loop (i.e. for each iteration), but do corresponding pop cx outside loop (i.e. only once per draw_line() call).
Moreover, that pop cx comes after leave, which drops saved cxes from stack, so the popped value is the return address (and following ret 10 will use y2 instead Wink).

Use debugger, it shows that clearly.

Horizontal and vertical (and maybe diagonal) lines are so special cases that they probably should be handled specifically.
Post 11 Jan 2013, 06:05
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 © 2004-2018, Tomasz Grysztar.

Powered by rwasa.