# Board index » delphi » Assembly routes for Circle drawing

## Assembly routes for Circle drawing

Does anyone out there know how to draw a circle in Assembly?
I need a perfect route to draw a circle, i mean a perfect circle.  I
tried to use Algerbra to draw a circle but it missed lots of pixel
and it is too slow too.  Can some one show me a better way?
I am very appreciated.

## Re:Assembly routes for Circle drawing

##### Quote
Never Die! wrote:

> Does anyone out there know how to draw a circle in Assembly?
> I need a perfect route to draw a circle, i mean a perfect circle.  I
> tried to use Algerbra to draw a circle but it missed lots of pixel
> and it is too slow too.  Can some one show me a better way?
> I am very appreciated.

Look up the Bresenham (I think it was) algorithm for circle
drawing.

- Alf

## Re:Assembly routes for Circle drawing

##### Quote
>Does anyone out there know how to draw a circle in Assembly?
>I need a perfect route to draw a circle, i mean a perfect circle.  I
>tried to use Algerbra to draw a circle but it missed lots of pixel
>and it is too slow too.  Can some one show me a better way?
>I am very appreciated.

Using algebra and/or trigonometry are generally the slowest (though most
accurate) ways of drawing a circle on a computer.  The Bresenham is one of
the best bets and is very fast because 1.) it is pure integer math, and 2.)
it takes advantage of the reflective properties of a circle -- only a 45
degree arc is calculated.

Unfortunately, the Bresenham algorithm is not quite accurate, especially
with small circles (which look like octagons).  But there's a modified
Bresenham algorithm that is a good deal more accurate.  Here's the code,
both in Turbo Pascal and assembler.  I believe I translated the circle()
procedure from a C listing I have, and I translated the assembler listing
from the Pascal source.

BTW, you'll need to provide your own plot() procedure, but that should be
easy enough to accomplish.

This is written for TP7, and is intended for mode 13h (320x200x256c).

var
x, y, d : integer;
dE, dSE : integer;

begin
x := 0;

dE := 3;
dSE := -(rad shl 1) + 5;
plot(xcent-x, ycent-y, colr);
plot(xcent-x, ycent+y, colr);
plot(xcent+y, ycent-x, colr);
plot(xcent-y, ycent+x, colr);

while y>x do
begin
if d<0 then
begin
d := d + dE;
dE := dE + 2;
dSE := dSE + 2;
x := x + 1;
end
else
begin
d := d + dSE;
dE := dE + 2;
dSE := dSE + 4;
x := x + 1;
y := y - 1;
end;
plot(xcent+x, ycent-y, colr);
plot(xcent-x, ycent+y, colr);
plot(xcent-x, ycent-y, colr);
plot(xcent+x, ycent+y, colr);
plot(xcent+y, ycent-x, colr);
plot(xcent-y, ycent+x, colr);
plot(xcent-y, ycent-x, colr);
plot(xcent+y, ycent+x, colr);
end;
end;

{bx = xp; cx = yp; dx = d}

var
dE, dSE : integer;

asm
shl   ax,1
not   ax
mov   [dSE],ax
mov   ax,3
mov   [dE],3
mov   bx,0
mov   dx,1
sub   dx,cx
mov   ax,[SegA000]
mov   es,ax
@Plot1A:
mov   ax,[ycent]
sub   ax,cx
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,bx
mov   al,colr
stosb
@Plot2A:
mov   ax,[ycent]
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,bx
mov   al,colr
stosb
@Plot3A:
mov   ax,[ycent]
sub   ax,bx
shl   ax,6
mov   di,ax
shl   ax,2
mov   al,colr
stosb
@Plot4A:
mov   ax,[ycent]
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,cx
mov   al,colr
stosb
@CircLoop:
cmp   bx,cx
jg    @Done
@CheckDecide:
test  dx,-1
js    @NegDecision
@NonNegDecision:
inc   bx
dec   cx
jmp   @Plot1
@NegDecision:
inc   bx
@Plot1:
{plot (xcent-yp,ycent-xp,colr);}
mov   ax,[ycent]
sub   ax,bx
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,cx
mov   al,colr
stosb
@Plot2:
{plot (xcent-yp,ycent+xp,colr);}
mov   ax,[ycent]
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,cx
mov   al,colr
stosb
@Plot3:
{plot (xcent+yp,ycent-xp,colr);}
mov   ax,[ycent]
sub   ax,bx
shl   ax,6
mov   di,ax
shl   ax,2
mov   al,colr
stosb
@Plot4:
{plot (xcent+yp,ycent+xp,colr);}
mov   ax,[ycent]
shl   ax,6
mov   di,ax
shl   ax,2
mov   al,colr
stosb
@Plot5:
{plot (xcent-xp,ycent-yp,colr);}
mov   ax,[ycent]
sub   ax,cx
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,bx
mov   al,colr
stosb
@Plot6:
{plot (xcent-xp,ycent+yp,colr);}
mov   ax,[ycent]
shl   ax,6
mov   di,ax
shl   ax,2
sub   di,bx
mov   al,colr
stosb
@Plot7:
{plot (xcent+xp,ycent-yp,colr);}
mov   ax,[ycent]
sub   ax,cx
shl   ax,6
mov   di,ax
shl   ax,2
mov   al,colr
stosb
@Plot8:
{plot (xcent+xp,ycent+yp,colr);}
mov   ax,[ycent]
shl   ax,6
mov   di,ax
shl   ax,2