Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.97 KB | None | 0 0
  1. /*
  2. * Filename: drawRadioactivePinwheel.s
  3. * Author: Christopher Liu
  4. * Userid: cs30xcp
  5. * Date: 1/30/19
  6. * Sources of Help: PA1 Writeup, Discussion Slides
  7. */
  8.  
  9. @ Raspberry Pi directives
  10. .cpu cortex-a53 @ Version of our Pis
  11. .syntax unified @ Modern ARM syntax
  12.  
  13. .equ FP_OFFSET, 4 @ Offset from sp to set fp
  14.  
  15. .equ LOCAL_VAR_SPACE, 24 @ Total number of local var
  16. @ bytes to allocate
  17. .equ HALF_SIZE_OFFSET, -8 @ Var half-size offset from fp
  18. .equ NUM_UPWARD_CHARS_OFFSET, -12 @ # of upward chars offset
  19. .equ NUM_DOWNWARD_CHARS_OFFSET, -16 @ # of downward chars offset
  20. .equ NUM_SPACE_CHARS_OFFSET, -20 @ # of space chars offset
  21. .equ I_OFFSET, -24 @ Local var i offset from fp
  22. .equ ROW_OFFSET, -28 @ Local var row offset from fp
  23.  
  24. .equ PARAM_SPACE, 16 @ Total number of formal param
  25. @ bytes to allocate
  26. .equ SIZE_OFFSET, -32 @ Formal param size offset
  27. .equ UPWARD_TRI_CHAR_OFFSET, -36 @ Upward tri char offset
  28. .equ DOWNWARD_TRI_CHAR_OFFSET, -40 @ Downward tri char offset
  29.  
  30. .equ HALF_DIVISOR, 2 @ Divides numbers in two
  31. .equ DOUBLE_FACTOR, 2 @ Doubles values of numbers
  32.  
  33. .equ SPACE_CHAR, ' ' @ Space character
  34. .equ NEWLINE_CHAR, '\n' @ Newline character
  35.  
  36. .global drawRadioactivePinwheel @ Specify drawRadioactivePinwheel as a
  37. @ global symbol
  38.  
  39. .text @ Switch to Text segment
  40. .align 2 @ Align on evenly divisible by 4 byte address;
  41. @ .align n where 2^n determines alignment
  42. /*
  43. * Function Name: drawRadioactivePinwheel()
  44. * Function Prototype: void drawRadioactivePinwheel( int size, char
  45. * upwardTriChar, char downwardTriChar );
  46. * Description: Prints a pinwheel in a radioactive format by printing spaces,
  47. * upward characters, and downward characters in a certain
  48. * sequence with loops of which iterations are determined by
  49. * the size inputted
  50. * Parameters: size - the width/height of the entire pinwheel
  51. * upwardTriChar - the character used in triangles pointing up
  52. * downwardTriChar - the characted used in triangles pointing down
  53. * Side Effects: Prints a radioactive pinwheel design based on the size.
  54. * Error Conditions: None
  55. * Return value: None
  56. *
  57. * Registers used:
  58. * r0 - arg 1, scratch, local var -- size, used in operations, held row &
  59. * char to be printed in printChar
  60. * r1 - arg 2, scratch -- upwardTriChar and used in operations
  61. * r2 - arg 3, scratch, local var -- downwardTriChar, used in operations,
  62. * and held i for use in loops
  63. * r3 - scratch, local var -- used in operations, and held num of chars
  64. * for checking loops against i
  65. *
  66. * Stack variables:
  67. * halfSize - [fp - 8] -- holds the size divided by two
  68. * numUpwardChars - [fp - 12] -- holds the # of upward chars to print
  69. * numDownwardChars - [fp - 16] -- holds the # of downward chars to t
  70. * numSpaceChars - [fp - 20] -- holds the # of space chars to print
  71. * i - [fp - 24] - holds the index of all for loops
  72. * row - [fp - 28] - holds which row of the pinwheel half is program is on
  73. * size - [fp - 32] - holds the formal parameter value of size
  74. * upwardTriChar - [fp - 36] - holds the param value of upwardTriChar
  75. * downwardTriChar - [fp - 40] - holds the param value of downwardTriChar
  76. */
  77. drawRadioactivePinwheel:
  78. @ Standard prologue
  79. push {fp, lr} @ Save registers: fp, lr
  80. add fp, sp, FP_OFFSET @ Set fp to base of saved registers
  81.  
  82. @ Allocate local variable space
  83. sub sp, sp, LOCAL_VAR_SPACE @ Allocate memory for local variables
  84. @ Init local vars
  85. mov r3, HALF_DIVISOR @ Move HALF_DIVISOR into r3
  86. sdiv r3, r0, r3 @ temp (r3) = size (r0) / HALF_DIVISOR
  87. str r3, [fp, HALF_SIZE_OFFSET] @ Initialize halfSize as r3
  88. mov r3, 0 @ Move 0 into r3
  89. str r3, [fp, ROW_OFFSET] @ Initialize local var row as 0 (r3)
  90.  
  91. @ Allocate formal parameter space
  92. sub sp, sp, PARAM_SPACE @ Allocate memory for formal params
  93. @ Store formal params
  94. str r0, [fp, SIZE_OFFSET] @ Store size (r0) on stack
  95. str r1, [fp, UPWARD_TRI_CHAR_OFFSET] @ Store r1 on stack
  96. str r2, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Store r2 on stack
  97.  
  98. @ while (row < halfsize) conditions
  99. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  100. ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
  101. cmp r0, r1 @ row (r0) >= halfSize (r1)
  102. bge end_top_while @ Branch to "end_top_while" if r0 >= r1
  103.  
  104. top_while:
  105. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  106. ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
  107. @ numUpwardChars = DOUBLE_FACTOR * row + 1
  108. mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
  109. mul r2, r2, r0 @ temp (r2) = DOUBLE_FACTOR * row (r0)
  110. add r2, r2, 1 @ temp (r2) = temp (r2) + 1
  111. str r2, [fp, NUM_UPWARD_CHARS_OFFSET] @ Store # of up chars
  112. @ numSpaceChars = halfSize - row - 1
  113. sub r3, r1, r0 @ temp (r3) = halfSize (r1) - row (r0)
  114. sub r3, r3, 1 @ temp (r3) = temp (r3) - 1
  115. str r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Store # of ' ' chars
  116. @ numDownwardChars = DOUBLE_FACTOR * (halfSize - row - 1) + 1
  117. mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
  118. mul r3, r2, r3 @ temp (r3) = DOUBLE_FACTOR * temp (r3)
  119. add r3, r3, 1 @ temp (r3) = temp (r3) + 1
  120. str r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Store # of down chars
  121.  
  122. mov r2, 0 @ Move 0 into r2
  123. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  124. @ for (i < numSpaceChars) conditions
  125. ldr r2, [fp, I_OFFSET] @ Get current value of i
  126. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  127. cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
  128. bge end_top_space_for_left @ Branch to end of for if r2 >= r3
  129.  
  130. top_space_for_left:
  131. mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
  132. bl printChar @ Call printChar
  133.  
  134. ldr r2, [fp, I_OFFSET] @ Get current value of i
  135. add r2, r2, 1 @ Increment i (++i)
  136. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  137. @ for (i < numSpaceChars) conditions
  138. ldr r2, [fp, I_OFFSET] @ Get current value of i
  139. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  140. cmp r2, r3 @ i (r2) < numSpaceChars (r3)
  141. blt top_space_for_left @ Branch to start of for if r2 < r3
  142.  
  143. end_top_space_for_left:
  144. mov r2, 0 @ Move 0 into r2
  145. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  146. @ for (i < numUpwardChars) conditions
  147. ldr r2, [fp, I_OFFSET] @ Get current value of i
  148. ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
  149. cmp r2, r3 @ i (r2) >= numUpwardChars (r3)
  150. bge end_top_up_for_left @ Branch to end of for if r2 >= r3
  151.  
  152. top_up_for_left:
  153. ldr r0, [fp, UPWARD_TRI_CHAR_OFFSET] @ Set r0 as up tri char
  154. bl printChar @ Call printChar
  155.  
  156. ldr r2, [fp, I_OFFSET] @ Get current value of i
  157. add r2, r2, 1 @ Increment i (++i)
  158. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  159. @ for (i < numUpwardChars) conditions
  160. ldr r2, [fp, I_OFFSET] @ Get current value of i
  161. ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
  162. cmp r2, r3 @ i (r2) < numUpwardChars (r3)
  163. blt top_up_for_left @ Branch to start of for if r2 < r3
  164.  
  165. end_top_up_for_left:
  166. mov r2, 0 @ Move 0 into r2
  167. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  168. @ for (i < numDownwardChars) conditions
  169. ldr r2, [fp, I_OFFSET] @ Get current value of i
  170. ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
  171. cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
  172. bge end_top_down_for @ Branch to end of for if r2 >= r3
  173.  
  174. top_down_for:
  175. ldr r0, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Set r0 as down tri ch
  176. bl printChar @ Call printChar
  177.  
  178. ldr r2, [fp, I_OFFSET] @ Get current value of i
  179. add r2, r2, 1 @ Increment i (++i)
  180. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  181. @ for (i < numDownwardChars) conditions
  182. ldr r2, [fp, I_OFFSET] @ Get current value of i
  183. ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
  184. cmp r2, r3 @ i (r2) < numDownwardChars (r3)
  185. blt top_down_for @ Branch to start of for if r2 < r3
  186.  
  187. end_top_down_for:
  188. mov r2, 0 @ Move 0 into r2
  189. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  190. @ for (i < numUpwardChars) conditions
  191. ldr r2, [fp, I_OFFSET] @ Get current value of i
  192. ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
  193. cmp r2, r3 @ i (r2) >= numUpwardChars (r3)
  194. bge end_top_up_for_right @ Branch to end of for if r2 >= r3
  195.  
  196. top_up_for_right:
  197. ldr r0, [fp, UPWARD_TRI_CHAR_OFFSET] @ Set r0 as up tri char
  198. bl printChar @ Call printChar
  199.  
  200. ldr r2, [fp, I_OFFSET] @ Get current value of i
  201. add r2, r2, 1 @ Increment i (++i)
  202. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  203. @ for (i < numUpwardChars) conditions
  204. ldr r2, [fp, I_OFFSET] @ Get current value of i
  205. ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
  206. cmp r2, r3 @ i (r2) < numUpwardChars (r3)
  207. blt top_up_for_right @ Branch to start of for if r2 < r3
  208.  
  209. end_top_up_for_right:
  210. mov r2, 0 @ Move 0 into r2
  211. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  212. @ for (i < numSpaceChars) conditions
  213. ldr r2, [fp, I_OFFSET] @ Get current value of i
  214. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  215. cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
  216. bge end_top_space_for_right @ Branch to end of for if r2 >= r3
  217.  
  218. top_space_for_right:
  219. mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
  220. bl printChar @ Call printChar
  221.  
  222. ldr r2, [fp, I_OFFSET] @ Get current value of i
  223. add r2, r2, 1 @ Increment i (++i)
  224. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  225. @ for (i < numSpaceChars) conditions
  226. ldr r2, [fp, I_OFFSET] @ Get current value of i
  227. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  228. cmp r2, r3 @ i (r2) < numSpaceChars (r3)
  229. blt top_space_for_right @ Branch to start of for if r2 < r3
  230.  
  231. end_top_space_for_right:
  232. mov r0, NEWLINE_CHAR @ Set r0 as NEWLINE_CHAR
  233. bl printChar @ Call printChar
  234.  
  235. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  236. add r0, r0, 1 @ Increment row (++row)
  237. str r0, [fp, ROW_OFFSET] @ Update row value on stack as r0
  238. @ while (row < halfsize) conditions
  239. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  240. ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
  241. cmp r0, r1 @ row (r0) < halfSize (r1)
  242. blt top_while @ Branch to "top_while" if r0 < r1
  243.  
  244. end_top_while:
  245. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  246. sub r0, r0, 1 @ Decrement row (--row)
  247. str r0, [fp, ROW_OFFSET] @ Update row value on stack as r0
  248. @ while (row >= 0) conditions
  249. cmp r0, 0 @ row (r0) < 0
  250. blt end_bot_while @ Branch to "end_bot_while" if r0 < 0
  251.  
  252. bot_while:
  253. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  254. ldr r1, [fp, HALF_SIZE_OFFSET] @ Get current value of halfSize
  255. @ numDownwardChars = DOUBLE_FACTOR * row + 1
  256. mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
  257. mul r2, r2, r0 @ temp (r2) = DOUBLE_FACTOR * row (r0)
  258. add r2, r2, 1 @ temp (r2) = temp (r2) + 1
  259. str r2, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Store # of down chars
  260. @ numSpaceChars = halfSize - row - 1
  261. sub r3, r1, r0 @ temp (r3) = halfSize (r1) - row (r0)
  262. sub r3, r3, 1 @ temp (r3) = temp (r3) - 1
  263. str r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Store # of ' ' chars
  264. @ numUpwardChars = DOUBLE_FACTOR * (halfSize - row - 1) + 1
  265. mov r2, DOUBLE_FACTOR @ Move DOUBLE_FACTOR into r2
  266. mul r3, r2, r3 @ temp (r3) = DOUBLE_FACTOR * temp (r3)
  267. add r3, r3, 1 @ temp (r3) = temp (r3) + 1
  268. str r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Store # of up chars
  269.  
  270. mov r2, 0 @ Move 0 into r2
  271. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  272. @ for (i < numSpaceChars) conditions
  273. ldr r2, [fp, I_OFFSET] @ Get current value of i
  274. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  275. cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
  276. bge end_bot_space_for_left @ Branch to end of for if r2 >= r3
  277.  
  278. bot_space_for_left:
  279. mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
  280. bl printChar @ Call printChar
  281.  
  282. ldr r2, [fp, I_OFFSET] @ Get current value of i
  283. add r2, r2, 1 @ Increment i (++i)
  284. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  285. @ for (i < numSpaceChars) conditions
  286. ldr r2, [fp, I_OFFSET] @ Get current value of i
  287. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  288. cmp r2, r3 @ i (r2) < numSpaceChars (r3)
  289. blt bot_space_for_left @ Branch to start of for if r2 < r3
  290.  
  291. end_bot_space_for_left:
  292. mov r2, 0 @ Move 0 into r2
  293. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  294. @ for (i < numDownwardChars) conditions
  295. ldr r2, [fp, I_OFFSET] @ Get current value of i
  296. ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
  297. cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
  298. bge end_bot_down_for_left @ Branch to end of for if r2 >= r3
  299.  
  300. bot_down_for_left:
  301. ldr r0, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Set r0 as down tri ch
  302. bl printChar @ Call printChar
  303.  
  304. ldr r2, [fp, I_OFFSET] @ Get current value of i
  305. add r2, r2, 1 @ Increment i (++i)
  306. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  307. @ for (i < numDownwardChars) conditions
  308. ldr r2, [fp, I_OFFSET] @ Get current value of i
  309. ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
  310. cmp r2, r3 @ i (r2) < numUpwardChars (r3)
  311. blt bot_down_for_left @ Branch to start of for if r2 < r3
  312.  
  313. end_bot_down_for_left:
  314. mov r2, 0 @ Move 0 into r2
  315. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  316. @ for (i < numUpwardChars) conditions
  317. ldr r2, [fp, I_OFFSET] @ Get current value of i
  318. ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
  319. cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
  320. bge end_bot_up_for @ Branch to end of for if r2 >= r3
  321.  
  322. bot_up_for:
  323. ldr r0, [fp, UPWARD_TRI_CHAR_OFFSET] @ Set r0 as up tri char
  324. bl printChar @ Call printChar
  325.  
  326. ldr r2, [fp, I_OFFSET] @ Get current value of i
  327. add r2, r2, 1 @ Increment i (++i)
  328. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  329. @ for (i < numUpwardChars) conditions
  330. ldr r2, [fp, I_OFFSET] @ Get current value of i
  331. ldr r3, [fp, NUM_UPWARD_CHARS_OFFSET] @ Get # of up chars
  332. cmp r2, r3 @ i (r2) < numDownwardChars (r3)
  333. blt bot_up_for @ Branch to start of for if r2 < r3
  334.  
  335. end_bot_up_for:
  336. mov r2, 0 @ Move 0 into r2
  337. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  338. @ for (i < numDownwardChars) conditions
  339. ldr r2, [fp, I_OFFSET] @ Get current value of i
  340. ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
  341. cmp r2, r3 @ i (r2) >= numDownwardChars (r3)
  342. bge end_bot_down_for_right @ Branch to end of for if r2 >= r3
  343.  
  344. bot_down_for_right:
  345. ldr r0, [fp, DOWNWARD_TRI_CHAR_OFFSET] @ Set r0 as down tri ch
  346. bl printChar @ Call printChar
  347.  
  348. ldr r2, [fp, I_OFFSET] @ Get current value of i
  349. add r2, r2, 1 @ Increment i (++i)
  350. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  351. @ for (i < numDownwardChars) conditions
  352. ldr r2, [fp, I_OFFSET] @ Get current value of i
  353. ldr r3, [fp, NUM_DOWNWARD_CHARS_OFFSET] @ Get # of down chars
  354. cmp r2, r3 @ i (r2) < numDownwardChars (r3)
  355. blt bot_down_for_right @ Branch to start of for if r2 < r3
  356.  
  357. end_bot_down_for_right:
  358. mov r2, 0 @ Move 0 into r2
  359. str r2, [fp, I_OFFSET] @ i = 0 (r2) and store on stack
  360. @ for (i < numSpaceChars) conditions
  361. ldr r2, [fp, I_OFFSET] @ Get current value of i
  362. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  363. cmp r2, r3 @ i (r2) >= numSpaceChars (r3)
  364. bge end_bot_space_for_right @ Branch to end of for if r2 >= r3
  365.  
  366. bot_space_for_right:
  367. mov r0, SPACE_CHAR @ Set r0 as SPACE_CHAR
  368. bl printChar @ Call printChar
  369.  
  370. ldr r2, [fp, I_OFFSET] @ Get current value of i
  371. add r2, r2, 1 @ Increment i (++i)
  372. str r2, [fp, I_OFFSET] @ Update i value on stack as r2
  373. @ for (i < numSpaceChars) conditions
  374. ldr r2, [fp, I_OFFSET] @ Get current value of i
  375. ldr r3, [fp, NUM_SPACE_CHARS_OFFSET] @ Get # of space chars
  376. cmp r2, r3 @ i (r2) < numSpaceChars (r3)
  377. blt bot_space_for_right @ Branch to start of for if r2 < r3
  378.  
  379. end_bot_space_for_right:
  380. mov r0, NEWLINE_CHAR @ Set r0 as NEWLINE_CHAR
  381. bl printChar @ Call printChar
  382.  
  383.  
  384. ldr r0, [fp, ROW_OFFSET] @ Get current value of row
  385. sub r0, r0, 1 @ Decrement row (--row)
  386. str r0, [fp, ROW_OFFSET] @ Update row value on stack as r0
  387. @ while (row >= 0) conditions
  388. cmp r0, 0 @ row (r0) >= 0
  389. bge bot_while @ Branch to "bot_while" if r0 >= 0
  390.  
  391. end_bot_while:
  392. @ Standard epilogue
  393. sub sp, fp, FP_OFFSET @ Set sp to top of saved registers
  394. pop {fp, pc} @ Restore fp; restore lr into pc for
  395. @ return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement