Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Filename: drawRadioactivePinwheel.s
- * Author: Christopher Liu
- * Userid: cs30xcp
- * Date: 1/30/19
- * Sources of Help: PA1 Writeup, Discussion Slides
- */
- @ Raspberry Pi directives
- .cpu cortex-a53 @ Version of our Pis
- .syntax unified @ Modern ARM syntax
- .equ FP_OFFSET, 4 @ Offset from sp to set fp
- .equ LOCAL_VAR_SPACE, 24 @ Total number of local var
- @ bytes to allocate
- .equ HALF_SIZE_OFFSET, -8 @ Var half-size offset from fp
- .equ NUM_UPWARD_CHARS_OFFSET, -12 @ # of upward chars offset
- .equ NUM_DOWNWARD_CHARS_OFFSET, -16 @ # of downward chars offset
- .equ NUM_SPACE_CHARS_OFFSET, -20 @ # of space chars offset
- .equ I_OFFSET, -24 @ Local var i offset from fp
- .equ ROW_OFFSET, -28 @ Local var row offset from fp
- .equ PARAM_SPACE, 16 @ Total number of formal param
- @ bytes to allocate
- .equ SIZE_OFFSET, -32 @ Formal param size offset
- .equ UPWARD_TRI_CHAR_OFFSET, -36 @ Upward tri char offset
- .equ DOWNWARD_TRI_CHAR_OFFSET, -40 @ Downward tri char offset
- .equ HALF_DIVISOR, 2 @ Divides numbers in two
- .equ DOUBLE_FACTOR, 2 @ Doubles values of numbers
- .equ SPACE_CHAR, ' ' @ Space character
- .equ NEWLINE_CHAR, '\n' @ Newline character
- .global drawRadioactivePinwheel @ Specify drawRadioactivePinwheel as a
- @ global symbol
- .text @ Switch to Text segment
- .align 2 @ Align on evenly divisible by 4 byte address;
- @ .align n where 2^n determines alignment
- /*
- * Function Name: drawRadioactivePinwheel()
- * Function Prototype: void drawRadioactivePinwheel( int size, char
- * upwardTriChar, char downwardTriChar );
- * Description: Prints a pinwheel in a radioactive format by printing spaces,
- * upward characters, and downward characters in a certain
- * sequence with loops of which iterations are determined by
- * the size inputted
- * Parameters: size - the width/height of the entire pinwheel
- * upwardTriChar - the character used in triangles pointing up
- * downwardTriChar - the characted used in triangles pointing down
- * Side Effects: Prints a radioactive pinwheel design based on the size.
- * Error Conditions: None
- * Return value: None
- *
- * Registers used:
- * r0 - arg 1, scratch, local var -- size, used in operations, held row &
- * char to be printed in printChar
- * r1 - arg 2, scratch -- upwardTriChar and used in operations
- * r2 - arg 3, scratch, local var -- downwardTriChar, used in operations,
- * and held i for use in loops
- * r3 - scratch, local var -- used in operations, and held num of chars
- * for checking loops against i
- *
- * Stack variables:
- * halfSize - [fp - 8] -- holds the size divided by two
- * numUpwardChars - [fp - 12] -- holds the # of upward chars to print
- * numDownwardChars - [fp - 16] -- holds the # of downward chars to t
- * numSpaceChars - [fp - 20] -- holds the # of space chars to print
- * i - [fp - 24] - holds the index of all for loops
- * row - [fp - 28] - holds which row of the pinwheel half is program is on
- * size - [fp - 32] - holds the formal parameter value of size
- * upwardTriChar - [fp - 36] - holds the param value of upwardTriChar
- * downwardTriChar - [fp - 40] - holds the param value of downwardTriChar
- */
- drawRadioactivePinwheel:
- @ Standard prologue
- push {fp, lr} @ Save registers: fp, lr
- add fp, sp, FP_OFFSET @ Set fp to base of saved registers
- @ Allocate local variable space
- sub sp, sp, LOCAL_VAR_SPACE @ Allocate memory for local variables
- @ Init local vars
- mov r3, HALF_DIVISOR @ Move HALF_DIVISOR into r3
- sdiv r3, r0, r3 @ temp (r3) = size (r0) / HALF_DIVISOR
- str r3, [fp, HALF_SIZE_OFFSET] @ Initialize halfSize as r3
- mov r3, 0 @ Move 0 into r3
- str r3, [fp, ROW_OFFSET] @ Initialize local var row as 0 (r3)
- @ Allocate formal parameter space
- sub sp, sp, PARAM_SPACE @ Allocate memory for formal params
- @ Store formal params
- str r0, [fp, SIZE_OFFSET] @ Store size (r0) on stack
- str r1, [fp, UPWARD_TRI_CHAR_OFFSET] @ Store r1 on stack
- str r2, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Store r2 on stack
- @ while (row < halfsize) conditions
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
- cmp r0, r1 @ row (r0) >= halfSize (r1)
- bge end_top_while @ Branch to "end_top_while" if r0 >= r1
- top_while:
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
- @ numUpwardChars = DOUBLE_FACTOR * row + 1
- mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
- mul r2, r2, r0 @ temp (r2) = DOUBLE_FACTOR * row (r0)
- add r2, r2, 1 @ temp (r2) = temp (r2) + 1
- str r2, [fp, NUM_UPWARD_CHARS_OFFSET] @ Store # of up chars
- @ numSpaceChars = halfSize - row - 1
- sub r3, r1, r0 @ temp (r3) = halfSize (r1) - row (r0)
- sub r3, r3, 1 @ temp (r3) = temp (r3) - 1
- str r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Store # of ' ' chars
- @ numDownwardChars = DOUBLE_FACTOR * (halfSize - row - 1) + 1
- mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
- mul r3, r2, r3 @ temp (r3) = DOUBLE_FACTOR * temp (r3)
- add r3, r3, 1 @ temp (r3) = temp (r3) + 1
- str r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Store # of down chars
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
- bge end_top_space_for_left @ Branch to end of for if r2 >= r3
- top_space_for_left:
- mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) < numSpaceChars (r3)
- blt top_space_for_left @ Branch to start of for if r2 < r3
- end_top_space_for_left:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numUpwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
- cmp r2, r3 @ i (r2) >= numUpwardChars (r3)
- bge end_top_up_for_left @ Branch to end of for if r2 >= r3
- top_up_for_left:
- ldr r0, [fp, UPWARD_TRI_CHAR_OFFSET] @ Set r0 as up tri char
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numUpwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
- cmp r2, r3 @ i (r2) < numUpwardChars (r3)
- blt top_up_for_left @ Branch to start of for if r2 < r3
- end_top_up_for_left:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numDownwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
- cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
- bge end_top_down_for @ Branch to end of for if r2 >= r3
- top_down_for:
- ldr r0, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Set r0 as down tri ch
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numDownwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
- cmp r2, r3 @ i (r2) < numDownwardChars (r3)
- blt top_down_for @ Branch to start of for if r2 < r3
- end_top_down_for:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numUpwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
- cmp r2, r3 @ i (r2) >= numUpwardChars (r3)
- bge end_top_up_for_right @ Branch to end of for if r2 >= r3
- top_up_for_right:
- ldr r0, [fp, UPWARD_TRI_CHAR_OFFSET] @ Set r0 as up tri char
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numUpwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
- cmp r2, r3 @ i (r2) < numUpwardChars (r3)
- blt top_up_for_right @ Branch to start of for if r2 < r3
- end_top_up_for_right:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
- bge end_top_space_for_right @ Branch to end of for if r2 >= r3
- top_space_for_right:
- mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) < numSpaceChars (r3)
- blt top_space_for_right @ Branch to start of for if r2 < r3
- end_top_space_for_right:
- mov r0, NEWLINE_CHAR @ Set r0 as NEWLINE_CHAR
- bl printChar @ Call printChar
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- add r0, r0, 1 @ Increment row (++row)
- str r0, [fp, ROW_OFFSET] @ Update row value on stack as r0
- @ while (row < halfsize) conditions
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
- cmp r0, r1 @ row (r0) < halfSize (r1)
- blt top_while @ Branch to "top_while" if r0 < r1
- end_top_while:
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- sub r0, r0, 1 @ Decrement row (--row)
- str r0, [fp, ROW_OFFSET] @ Update row value on stack as r0
- @ while (row >= 0) conditions
- cmp r0, 0 @ row (r0) < 0
- blt end_bot_while @ Branch to "end_bot_while" if r0 < 0
- bot_while:
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
- @ numDownwardChars = DOUBLE_FACTOR * row + 1
- mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
- mul r2, r2, r0 @ temp (r2) = DOUBLE_FACTOR * row (r0)
- add r2, r2, 1 @ temp (r2) = temp (r2) + 1
- str r2, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Store # of down chars
- @ numSpaceChars = halfSize - row - 1
- sub r3, r1, r0 @ temp (r3) = halfSize (r1) - row (r0)
- sub r3, r3, 1 @ temp (r3) = temp (r3) - 1
- str r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Store # of ' ' chars
- @ numUpwardChars = DOUBLE_FACTOR * (halfSize - row - 1) + 1
- mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
- mul r3, r2, r3 @ temp (r3) = DOUBLE_FACTOR * temp (r3)
- add r3, r3, 1 @ temp (r3) = temp (r3) + 1
- str r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Store # of up chars
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
- bge end_bot_space_for_left @ Branch to end of for if r2 >= r3
- bot_space_for_left:
- mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) < numSpaceChars (r3)
- blt bot_space_for_left @ Branch to start of for if r2 < r3
- end_bot_space_for_left:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numDownwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
- cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
- bge end_bot_down_for_left @ Branch to end of for if r2 >= r3
- bot_down_for_left:
- ldr r0, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Set r0 as down tri ch
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numDownwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
- cmp r2, r3 @ i (r2) < numUpwardChars (r3)
- blt bot_down_for_left @ Branch to start of for if r2 < r3
- end_bot_down_for_left:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numUpwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
- cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
- bge end_bot_up_for @ Branch to end of for if r2 >= r3
- bot_up_for:
- ldr r0, [fp, UPWARD_TRI_CHAR_OFFSET] @ Set r0 as up tri char
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numUpwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
- cmp r2, r3 @ i (r2) < numDownwardChars (r3)
- blt bot_up_for @ Branch to start of for if r2 < r3
- end_bot_up_for:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numDownwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
- cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
- bge end_bot_down_for_right @ Branch to end of for if r2 >= r3
- bot_down_for_right:
- ldr r0, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Set r0 as down tri ch
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numDownwardChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
- cmp r2, r3 @ i (r2) < numDownwardChars (r3)
- blt bot_down_for_right @ Branch to start of for if r2 < r3
- end_bot_down_for_right:
- mov r2, 0 @ Move 0 into r2
- str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
- bge end_bot_space_for_right @ Branch to end of for if r2 >= r3
- bot_space_for_right:
- mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
- bl printChar @ Call printChar
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- add r2, r2, 1 @ Increment i (++i)
- str r2, [fp, I_OFFSET] @ Update i value on stack as r2
- @ for (i < numSpaceChars) conditions
- ldr r2, [fp, I_OFFSET] @ Get current value of i
- ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
- cmp r2, r3 @ i (r2) < numSpaceChars (r3)
- blt bot_space_for_right @ Branch to start of for if r2 < r3
- end_bot_space_for_right:
- mov r0, NEWLINE_CHAR @ Set r0 as NEWLINE_CHAR
- bl printChar @ Call printChar
- ldr r0, [fp, ROW_OFFSET] @ Get current value of row
- sub r0, r0, 1 @ Decrement row (--row)
- str r0, [fp, ROW_OFFSET] @ Update row value on stack as r0
- @ while (row >= 0) conditions
- cmp r0, 0 @ row (r0) >= 0
- bge bot_while @ Branch to "bot_while" if r0 >= 0
- end_bot_while:
- @ Standard epilogue
- sub sp, fp, FP_OFFSET @ Set sp to top of saved registers
- pop {fp, pc} @ Restore fp; restore lr into pc for
- @ return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement