Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. /*
  2. -------------------------------------------------------
  3. swap_array.s
  4. Working with stack frames and local variables.
  5. -------------------------------------------------------
  6. Author: david djukic
  7. ID: 160692920
  8. Email: djuk2920@mylaurier.ca
  9. Date: 23-03-17
  10. -------------------------------------------------------
  11. */
  12. .equ SWI_Exit, 0x11 @ Terminate program code
  13. .equ SWI_Open, 0x66 @ Open a file
  14. @ inputs - R0: address of file name, R1: mode (0: input, 1: write, 2: append)
  15. @ outputs - R0: file handle, -1 if open fails
  16. .equ SWI_Close, 0x68 @ Close a file
  17. @ inputs - R0: file handle
  18. .equ SWI_RdInt, 0x6c @ Read integer from a file
  19. @ inputs - R0: file handle
  20. @ outputs - R0: integer
  21. .equ SWI_PrInt, 0x6b @ Write integer to a file
  22. @ inputs - R0: file handle, R1: integer
  23. .equ SWI_RdStr, 0x6a @ Read string from a file
  24. @ inputs - R0: file handle, R1: buffer address, R2: buffer size
  25. @ outputs - R0: number of bytes stored
  26. .equ SWI_PrStr, 0x69 @ Write string to a file
  27. @ inputs- R0: file handle, R1: address of string
  28. .equ SWI_PrChr, 0x00 @ Write a character to stdout
  29. @ inputs - R0: character
  30.  
  31. .equ inputMode, 0 @ Set file mode to input
  32. .equ outputMode, 1 @ Set file mode to output
  33. .equ appendMode, 2 @ Set file mode to append
  34. .equ stdout, 1 @ Set output target to be Stdout
  35.  
  36. @-------------------------------------------------------
  37. @ Main Program
  38.  
  39. MOV R0, #stdout
  40.  
  41. @ Print the array before the swap
  42. LDR R2, =_Data
  43. LDR R1, =Data
  44. SUB R2, R2, R1
  45. STMFD SP!, {R2} @ Push length of list
  46. STMFD SP!, {R1} @ Push address of list
  47. BL print_array
  48. ADD SP, SP, #8
  49. BL PrintLF
  50.  
  51. @ Swap the array data
  52. MOV R1, #1
  53. STMFD SP!, {R1} @ Push j
  54. MOV R1, #7
  55. STMFD SP!, {R1} @ Push i
  56. LDR R1, =Data
  57. STMFD SP!, {R1} @ Push a
  58. BL swap_array
  59. ADD SP, SP, #12
  60. BL PrintLF
  61.  
  62. @ Print the array after the swap
  63. LDR R2, =_Data
  64. LDR R1, =Data
  65. SUB R2, R2, R1
  66. STMFD SP!, {R2} @ Push length of list
  67. STMFD SP!, {R1} @ Push address of list
  68. BL print_array
  69. ADD SP, SP, #8
  70. BL PrintLF
  71.  
  72. SWI SWI_Exit
  73.  
  74. @-------------------------------------------------------
  75. PrintLF:
  76. /*
  77. -------------------------------------------------------
  78. Prints the line feed character (\n)
  79. -------------------------------------------------------
  80. Uses:
  81. R0 - set to '\n'
  82. (SWI_PrChr automatically prints to stdout)
  83. -------------------------------------------------------
  84. */
  85. STMFD SP!, {R0, LR}
  86. MOV R0, #'\n' @ Define the line feed character
  87. SWI SWI_PrChr @ Print the character to Stdout
  88. LDMFD SP!, {R0, PC}
  89.  
  90. @-------------------------------------------------------
  91. swap_array:
  92. /*
  93. -------------------------------------------------------
  94. Swaps location of two values in list. swap(a, i, j)
  95. Parameters passed on stack:
  96. Address of list
  97. Index of first value (i)
  98. Index of second value (j)
  99. Local variable
  100. temp
  101. -------------------------------------------------------
  102. Uses: R0- Address of array
  103. R1- First Index
  104. R2- Second Index
  105. R3- Swap register (value being swapped)
  106.  
  107. -------------------------------------------------------
  108. */
  109. STMFD SP!, {FP, LR} @ push frame pointer and link register onto the stack
  110. MOV FP, SP @ save current stack top to frame pointer
  111. SUB SP, SP, #4 @ set aside space for temp
  112. STMFD SP!, {R0-R3} @ preserve other registers
  113.  
  114.  
  115. LDR R0, [FP, #8] @ load address of fp into R0
  116. LDR R1, [FP, #12] @ loads first Index
  117. LDR R2, [FP, #16] @loads second Index
  118.  
  119. LDRB R3, [R0,R1] @loads first index & bytes from the address of r0 into R3
  120. STRB R3, [FP,#-4] @ stores bytes of R3 into -1 address of frame pointer
  121. LDRB R3, [R0,R2] @loads second index & bytes from r0 into R3
  122. STRB R3, [R0,R1] @stores bytes of R3 into address r0 + first index
  123. LDRB R3, [FP,#-4] @loads the bytes that was stored in -1 memory
  124. STRB R3, [R0,R2] @ store #bytes into the second index+address of r0
  125.  
  126.  
  127. LDMFD SP!, {R0,R3} @ pop preserved registers
  128. ADD SP, SP, #4 @ remove local storage
  129. LDMFD SP!, {FP, PC} @ pop frame pointer and program counter
  130.  
  131. @-------------------------------------------------------
  132. print_array:
  133. /*
  134. -------------------------------------------------------
  135. Prints all integer values comma-separated in an array of bytes.
  136. (i.e. 2,4,7,1,6,...) with no trailing comma.
  137. Parameters passed on stack:
  138. Address of list
  139. Length of list
  140. -------------------------------------------------------
  141. Uses: R0- values for output
  142. R1- value being printed
  143. R2- First address (current)
  144. R3 - Length
  145.  
  146. -------------------------------------------------------
  147. */
  148.  
  149. STMFD SP!, {FP, LR} @load frame pointer and link register to stack
  150. MOV FP, SP @save stack top as FP
  151. STMFD SP!, {R0-R3} @preserve other registers
  152. MOV R0, #stdout @set r0 to stdout
  153.  
  154. LDR R2,[FP,#8] @load starting address to R2
  155. LDR R3,[FP,#12] @load length of the list
  156. CMP R3,#0 @is the length = 0?
  157. BEG _Loop @if so go to _Loop (end)
  158.  
  159. Loop:
  160. LDRB R1, [R2],#1 @load memory in current address
  161. SWI SWI_PrInt @print
  162. SUB R3,R3,#1 @sub 1 from length
  163. CMP R3, #0 @is the length=0?
  164. BEQ _Loop @if so go to end
  165.  
  166. MOV R0, #',' @else add coma,
  167. SWI SWI_PrChr
  168. MOV R0, #' ' @ add space
  169. SWI SWI_PrChr
  170.  
  171. MOV R0, #stdout @output value
  172. B Loop @looooooop
  173.  
  174. _Loop:
  175. LDMFD SP!, {R0,R3} @pop registers
  176. LDMFD SP!, {FP,PC} @pop frame pointer and program counter
  177.  
  178.  
  179.  
  180.  
  181. @-------------------------------------------------------
  182. .data
  183. Data: .byte 4,5,9,0,8,0,8,7,1 @ The list of data
  184. _Data: @ End of list address
  185. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement