Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 1.98 KB | None | 0 0
  1.     .syntax unified
  2.     .cpu cortex-m3
  3.     .thumb
  4.     .align 2
  5.     .global fir
  6.     .thumb_func
  7.  
  8. @ CG2028 Assignment, Sem 2, AY 2018/19
  9. @ (c) CG2028 Teaching Team, ECE NUS, 2019
  10.  
  11. @Register map
  12. @R0 - N, returns y
  13. @R1 - b (addr)
  14. @R2 - x_n
  15. @R3 - y_n
  16. @R4 - b (value)
  17. @R5 - x_store[j] (addr)
  18. @R6 - x_store[j] (value)
  19. @R7 - counter for circular buffer
  20. @R8 - curr (addr)
  21. @R9 - x_store (addr)
  22. @R10 - loop counter
  23. @R11 - curr (value)
  24.  
  25. fir:
  26. @ PUSH / save (only those) registers which are modified by your function
  27. @ parameter registers need not be saved.
  28. PUSH {R3-R11}
  29.  
  30. @ write asm function body here
  31. LDR R4, [R1]                    @ R4: b (value)
  32. MUL R3, R2, R4                  @ R3: x_n * b[0]
  33.  
  34. MOV R10, R0                     @ use R10 as loop counter, initialized to N
  35. LDR R8, =curr                  
  36. LDR R9, =x_store
  37.  
  38. LDR R11, [R8]                   @ R11: curr (value)
  39. MOV R7, R11                     @ R7: counter for circular buffer
  40. ADD R5, R9, R11, LSL #2         @ R5: x_store[curr] (addr)
  41.  
  42. loop:
  43.     LDR R4, [R1, #4]!           @ R4: b[++j] (value)
  44.     LDR R6, [R5], #4            @ R6: x_store[j++] (value)
  45.     MLA R3, R4, R6, R3          @ y_n += b * x_store[j]
  46.  
  47.     ADD R7, #1
  48.     CMP R7, R0
  49.     BEQ if1                     @ if R7 == N, reset R7 to 0 and R5 to x_store (addr)
  50.     return_from_if1:
  51.  
  52.     SUBS R10, #1                @ loop counter --
  53.     BNE loop
  54.  
  55. @LDR R4, [R8]
  56. SUBS R11, #1                    @ curr--, since we write to the left of the slot we are reading
  57. BMI if2                         @ if R11 < 0, wrap around to N-1
  58. return_from_if2:
  59.  
  60. ADD R5, R9, R11, LSL #2         @ R5: x_store[curr] (addr)
  61. STR R2, [R5]                    @ R2: x_n
  62. STR R11, [R8]                   @ R11: curr (value)
  63.  
  64. @ prepare value to return (y_n) to C program in R0
  65. MOVW R4, #10000
  66. UDIV R3, R4                     @ y_n/10000
  67. MOV R0, R3
  68.  
  69. @ POP / restore original register values. DO NOT save or restore R0. Why?
  70. POP {R3-R11}
  71.  
  72. @ return to C program
  73.         BX  LR
  74.  
  75. if1:
  76.     MOV R7, #0
  77.     LDR R5, =x_store
  78.     BAL return_from_if1
  79.  
  80. if2:
  81.     MOV R11, R0
  82.     SUB R11, #1
  83.     BAL return_from_if2
  84.  
  85. @label: .word value
  86. .equ N_MAX, 10
  87. @.lcomm label num_bytes
  88. .lcomm x_store N_MAX * 4
  89.  
  90. .section ".data"
  91.     .global curr
  92.     curr: .word 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement