Assembly Language for Beginners

(nextflipdebug2) #1
8.13. DEMOS
Indeed, weuse16-bitregisters, whichholdsignedvaluesintherangeof-32768..32767, soifany
of the coordinates is greater than 32767 during the signed multiplication, this point is definitely
out of bounds: we jump to theMandelBreaklabel.


  • There is also a division by 64 (SAR instruction). 64 sets scale.


Try to increase the value and you can get a closer look, or to decrease if for a more distant look.


  • We are at theMandelBreaklabel, there are two ways of getting here: the loop ended with CX=0 (
    the point is inside the Mandelbrot set); or because an overflow has happened (CX still holds some
    value). Now we write the low 8-bit part of CX (CL) to the video buffer.


The default palette is rough, nevertheless, 0 is black: hence we see black holes in the places where
the points are in the Mandelbrot set. The palette can be initialized at the program’s start, but keep
in mind, this is only a 64 bytes program!


  • Theprogramrunsinaninfiniteloop, becauseanadditionalcheckwheretostop, oranyuserinterface
    will result in additional instructions.


Some other optimization tricks:


  • The 1-byte CWD is used here for clearing DX instead of the 2-byteXOR DX, DXor even the 3-byte
    MOV DX, 0.

  • The 1-byteXCHG AX, CXis used instead of the 2-byteMOV AX,CX. The current value of AX is not
    needed here anyway.

  • DI (position in video buffer) is not initialized, and it is 0xFFFE at the start^53.


That’s OK, because the program works for all DI in the range of 0..0xFFFF eternally, and the user
can’t notice that it is started off the screen (the last pixel of a 320*200 video buffer is at address
0xF9FF). So some work is actually done off the limits of the screen.

Otherwise, you’ll need an additional instructions to set DI to 0 and check for the video buffer’s end.

My “fixed” version

Listing 8.27: My “fixed” version
1 org 100h
2 mov al,13h
3 int 10h
4
5 ; set palette
6 mov dx, 3c8h
7 mov al, 0
8 out dx, al
9 mov cx, 100h
10 inc dx
11 l00:
12 mov al, cl
13 shl ax, 2
14 out dx, al ; red
15 out dx, al ; green
16 out dx, al ; blue
17 loop l00
18
19 push 0a000h
20 pop es
21
22 xor di, di
23
24 FillLoop:
25 cwd
26 mov ax,di
27 mov cx,320
28 div cx
29 sub ax,100
30 sub dx,160
31


(^53) More information about initial register values:http://go.yurichev.com/17004

Free download pdf