Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. .text
  2. .global _start
  3.  
  4. _start:
  5. LDR R7, =TEST_NUM // Address of the first word '10'
  6. MOV R2, #0 // Whether swapped or not through the list (0 if no swap)
  7. LDR R6, =TEST_NUM // Address of the first word '10'
  8. MOV R1, #0 // Counter initially set to 0
  9.  
  10. MAIN:
  11. MOV R8, #0
  12. MOV R11, #0
  13. LDR R8, [R6] // Load the first word (number of items in list)
  14. SUB R8, #1 // Number of loops in a list
  15. CMP R8, R1 // Compare whether we looped the entire list
  16. BEQ CHECK_SWAPPED
  17. MOV R11, #4
  18. MUL R12, R1, R11 // Multiply 4 by element counter to find address of current element
  19. ADD R5, R6, #4 // Address of the first word after word count
  20. LDR R0, [R5, R12] // Access current element
  21. ADD R1, #1 // Increase current element counter by 1
  22. ADD R0, R7, #4
  23. B SWAP
  24. END:
  25. B END
  26.  
  27. /* SWAP Subroutine */
  28. SWAP:
  29. ADD R3, R0, #4
  30. LDR R8, [R3]
  31. LDR R10, [R0]
  32. CMP R10, R8 // R0 - R3 >= 0 -> (need swap if false)
  33. BPL L_END // Keep the order of R0 and R3 as is (change R0 to 0 and exit SWAP)
  34. STR R8, [R0] // Temporary register for the next value
  35. STR R10, [R3] // Swapping R3 and R0
  36. MOV R0, #1 // Overwriting R0 = 1 ("Swap is performed")
  37. ORR R2, R0 // Or-ing the swapped status to the general status of the list
  38. B S_END
  39. L_END:
  40. MOV R0, #0 // "Swap is not performed"
  41. S_END:
  42. ADD R7, #4
  43. MOV R0, R7
  44. B MAIN // Return back to MAIN
  45.  
  46. CHECK_SWAPPED:
  47. CMP R2, #0 // Check if any elements have been swapped
  48. BEQ END // If yes, stop executing program
  49. MOV R2, #0 // Reset swap
  50. MOV R1, #0 // Reset current element counter
  51. MOV R7, R6
  52. B MAIN
  53.  
  54. TEST_NUM:
  55. .word 0xC // Number of words in list
  56. .word 0x3
  57. .word 0x578
  58. .word 0x2d
  59. .word 0x4
  60. .word 0x5
  61. .word 0x2
  62. .word 0x32
  63. .word 0x33
  64. .word 0x34
  65. .word 0x35
  66. .word 0x600
  67. .word 0x700
  68.  
  69. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement