ARM: Sierpinski triangle

From ScienceZero
Jump to: navigation, search

Draws the sierpinski triangle on a 320x256 screen with 8 bits per pixel using the chaos game method. A vertex of a triangle is selected at random and averaged with the current position to find the new position to plot a pixel.

       MOV     R9,#320                    ; Screen width
       MOV     R2,#5                      ; Random number generator seed
       MOV     R4,#160                    ; X position of top vertex
 
       MOV     R8,#1048576                ; Loop counter
       MOV     R11,#160                   ; Set start position to middle of the screen
       MOV     R12,#128                   

.LOOP  RSB     R2,R2,R2,ROR #21           ; Generate pseudo random number in the range 0 to 2
       MOVS    R3,R2,LSR #31              ; LSB in carry, MSB in R3
       BHI     LOOP                       ; Repeat if 3

       ADDCC   R11,R11,R4,LSL R3          ; Calculate new position halfway
       ADDCC   R12,R12,R3,LSL #8          ; between the old and the new vertex
       ADDCS   R12,R12,#256
 
       MOV     R11,R11,LSR #1
       MOV     R12,R12,LSR #1
 
       MLA     R7,R12,R9,R11              ; R7 = (R12 * 320) + R11
       STRB    R12,[R0,R7]                
       SUBS    R8,R8,#1
       BNE     LOOP

Sierpinskicodercolour.png