As part of a first year university course in computer architecture some time ago we were tasked with learning 8086 ASM.  The final course work required creating a small animation of shapes moving around the screen changing colours and the like.As I would not expect anyone to review 720 lines of assembler I'm just going to provide my implementation of Bresenham's Line Algorithm.Note that this is not the entire program but a single macro from the working program.Also note that at the time of writing I had little to no experience with 8086 ASM (I still don't in fact), but I'm very interested in knowing how I did and how it could have been improved upon.If anyone is interested in reviewing the rest of the project as well (it can be done in small parts) then let me know.;=========================================================================================================;                       BRESENHAM LINE ALGORITHM (lx1,ly1)-(lx2,ly2);=========================================================================================================line macro lx1, ly1, lx2, ly2    local ldxsetup1, ldxsetup2, ldysetup1, ldysetup2    local lxisetup1, lxisetup2, lxisetupexit, lyisetup1, lyisetup2, lyisetupexit    local numsetup1, numsetup2, numsetupexit    local lloopstart, lloopif, lloopifexit, lloopend    pushall    mov ax, lx2    sub ax, lx1    cmp ax, 0    jge ldxsetup2ldxsetup1:    mov bx, -1    mul bxldxsetup2:    mov ldx, ax    mov ax, ly2    sub ax, ly1    cmp ax, 0    jge ldysetup2ldysetup1:    mov bx, -1    mul bxldysetup2:    mov ldy, ax    mov ax, lx1    mov lx, ax    mov ax, ly1    mov ly, ax    mov ax, lx2    cmp ax, lx1    jge lxisetup1    jmp lxisetup2lxisetup1:    mov ax, 1    jmp lxisetupexitlxisetup2:    mov ax, -1lxisetupexit:    mov lxi1, ax    mov lxi2, ax    mov ax, ly2    cmp ax, ly1    jge lyisetup1    jmp lyisetup2lyisetup1:    mov ax, 1    jmp lyisetupexitlyisetup2:    mov ax, -1lyisetupexit:    mov lyi1, ax    mov lyi2, ax    mov ax, ldx    mov bx, ldy    cmp ax, bx    jge numsetup1    jmp numsetup2numsetup1:    mov ax, 0    mov lxi1, ax    mov lyi2, ax    mov ax, ldx    mov lden, ax    mov lnumpix, ax    shr ax, 1    mov lnum, ax    mov ax, ldy    mov lnumadd, ax    jmp numsetupexitnumsetup2:    mov ax, 0    mov lxi2, ax    mov lyi1, ax    mov ax, ldy    mov lden, ax    mov lnumpix, ax    shr ax, 1    mov lnum, ax    mov ax, ldx    mov lnumadd, axnumsetupexit:    mov ax, lnum    mov dx, 0lloopstart:    cmp dx, lnumpix    jg lloopend    plot lx, ly    add ax, lnumadd    cmp ax, lden    jge lloopif    jmp lloopifexitlloopif:    sub ax, lden    mov bx, lx    add bx, lxi1    mov lx, bx    mov bx, ly    add bx, lyi1    mov ly, bxlloopifexit:    mov bx, lx    add bx, lxi2    mov lx, bx    mov bx, ly    add bx, lyi2    mov ly, bx    inc dx    jmp lloopstartlloopend:    popallendm;=========================================================================================================Here's the definitions of pushall and popall:;=========================================================================================================;                       PUSH ALL DATA TO STACK;=========================================================================================================pushall macro    push ax    push bx    push cx    push dxendm;=========================================================================================================;=========================================================================================================;                       POP ALL DATA FROM STACK;=========================================================================================================popall macro    pop dx    pop cx    pop bx    pop axendm;=========================================================================================================
8086 ASM Bresenham's Line Algorithm
beginner;algorithm;graphics;assembly
A few quick remarksBecause it was written as a macro, this lengthy code will be inserted everywhere that you need to draw a line. This is wasteful. Better write this kind of code as a procedure that gets called.You definitely need to add comments to the code. The names used for the labels don't make up for the lack of comments!Because a lowercase l and the digit 1 are so resemblant, I found it hard at times to easily read all those variable names that start with an 'l'.Analysismov ax, lx2sub ax, lx1cmp ax, 0jge ldxsetup2In code like this there's no need for an explicit cmp ax, 0 as the preceding sub ax, lx1 already defines all the necessary processor flags for correct operation of the following jge ldxsetup2.ldxsetup1:  mov bx, -1  mul bxThis part is essentially calculating the opposite of the number in the AX register. Why not use the neg ax instruction? It's well suited for the task. It gives smaller code and doesn't clobber the BX and DX registers.Also note that the ldxsetup1 label isn't really needed in this program. It's noise.  mov ax, lx2  cmp ax, lx1  jge lxisetup1  jmp lxisetup2lxisetup1:  mov ax, 1  jmp lxisetupexitlxisetup2:  mov ax, -1lxisetupexit:  mov lxi1, ax  mov lxi2, axIn this code there's a lot to optimize.  Step 1, remove a redundant jmp and the now unnecessary label lxisetup1:  mov  ax, lx2  cmp  ax, lx1  jl   lxisetup2      ;Opposite conditional jump and fall through  mov  ax, 1  jmp  lxisetupexitlxisetup2:  mov  ax, -1lxisetupexit:  mov  lxi1, ax  mov  lxi2, axStep 2, simplify even more by using an extra register and less jumping around:  mov  bx, -1      ;BX=-1 Increment if x2 < x1  mov  ax, lx2  cmp  ax, lx1  jl   lxisetupexit  neg  bx          ;BX=1  Increment if x2 >= x1lxisetupexit:  mov  lxi1, bx  mov  lxi2, bxStep 3, combine the calculation of the increments with the calculation of the delta's:  mov  bx, 1       ;BX=1  Increment if x2 >= x1  mov  ax, lx2  sub  ax, lx1  jge  ldxsetup2  neg  bx          ;BX=-1 Increment if x2 < x1  neg  ax          ;Abs(x2-x1)ldxsetup:  mov  ldx, ax  mov  lxi1, bx  mov  lxi2, bxThe resulting code is much smaller and the number of local labels is halved.  cmp ax, bx  jge numsetup1  jmp numsetup2numsetup1:Here once again, use the opposite conditional jump and otherwise fall through:cmp  ax, bxjl   numsetup2numsetup1:  mov ax, 0  mov lxi1, ax  mov lyi2, ax  ...numsetup2:  mov ax, 0  mov lxi2, ax  mov lyi1, axThis is the most bizarre part of your program. These 4 variables are zeroed, never again re-assigned, yet happily used in lots of additions that as a consequence don't really add anything! That is unless the plot lx, ly macro call changes them. It would have been great to see how plot is defined because now it's next to impossible to verify that this program actually draws a line or just a single dot.If anyone is interested in reviewing the rest of the project as well (it can be done in small parts) then let me know.I would love to see the rest. I don't find 720 lines of code much. As you'll see from my profile I tend to turn in long papers myself.