JustPaste.it

.text #Line drawing from (A, B) to (C, D)

#A=$16, B=$17, C=$18, and D=$19

main:#----------------------------------#Start up calculations

      sub $8, $18, $16 #Find delta x ($8)

      sub $9, $19, $17 #Find delta y ($9)

      blt $0, $8, keep #Branch if delta x is positive

      sub $8, $0, $8 #Remove sign

      ori $10, $0, -1 #Load sign ($10)

keep:#----------------------------------#Sign checks

      blt $0, $9, compare #Branch if delta y is positive

      sub $9, $0, $9 #Remove sign

      ori $11, $0, -1 #Load sign ($11)

compare:#-------------------------------#Setup correct algorithm             

      xor $12, $11, $10 #Calculate sign of slope 0=positive, -1=negative

      or $14, $8, $0 #Place change in x in $14

      or $15, $9, $0 #Place change in y in $15

prep:#----------------------------------#Determines if sign needs correction      

      beq $12, $0, sign #Branch if sign is positive

      bgt $9, $8, mgreat #Determine if slope is greater or less than 1, unsigned      

mless:#---------------------------------#Start slope less than 1

      bgt $16, $18, switch #Find lower x value

      j, start #Jump to start

sign:#----------------------------------#Corrects the sign

      ori $12, $0, 1 #Set $12 to 1

      j, prep #Jump to prep

start:#---------------------------------#Begins output segment

      or $16, $0, $16 #Output starting x

      or $17, $0, $17 #Output starting y    

algo:#----------------------------------#Main algorithm

      add $15, $15, $9 #Add change in y to current state

      blt $15, $8, noadd #If state is greater than denominator branch

      add $17, $17, $12 #Add 1 to current y value

      sub $15, $15, $8 #Subtracts denominator from state

noadd:#---------------------------------#Skips adding to the y value

      addi $16, $16, 1        #Adds 1 to x value

      beq $18, $16, end #Branched if the line is done

      j, algo #Loops algo until the line is complete

switch:#--------------------------------#Switches points (A,B) and (C,D)

      or $20, $16, $0 #Move A to $20

      or $21, $17, $0 #Move B to $21

      or $16, $18, $0 #Move C to $16

      or $17, $19, $0 #Move D to $17

      or $18, $20, $0 #Move A to $18

      or $19, $21, $0 #Move B to $19

      bgt $9, $8, mgreat #Return to mgreat

      j, mless #Return to mless

mgreat:#--------------------------------#Start slope greater than 1

      bgt $17, $19, switch #Find lower y value

begin:#---------------------------------#Begins output segment

      or $16, $0, $16 #Output starting x

      or $17, $0, $17 #Output starting y

rithm:#---------------------------------#Main algorithm

      add $14, $14, $8 #Add change in y to current state

      blt $14, $9, addfalse #If state is less than denominator branch

      add $16, $16, $12 #Add 1 to current x value

      sub $14, $14, $9 #Subtract denominator from state

addfalse:#------------------------------#Skips adding to the x value

      addi $17, $17, 1 #Adds 1 to y value

      beq $17, $19, end #Branch if line is done

      j, rithm #Loops rithm until the line is complete       

end: