Advertisement
Guest User

Untitled

a guest
Apr 5th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 5.54 KB | None | 0 0
  1. ; ****Your name goes here*******
  2. ;Yasir Choudhury
  3.  
  4. ; This is Exam2_ArrayofStruct
  5. ; EE319K Fall 2014 exam2, November 6, 2014
  6. ; You edit this file only  
  7.        AREA   Data, ALIGN=4
  8.  
  9.  
  10.        AREA    |.text|, CODE, READONLY, ALIGN=2
  11.        THUMB
  12.  
  13. ;***************** Linear****************************
  14. ; Calculate the result of a linear equation y = 16*x-50  
  15. ; Input parameter: x is unsigned 8 bits
  16. ; Output parameter: y is unsigned 8 bits
  17. ; Error conditions: implement ceiling on overflow
  18. ;                   implement floor on underflow
  19. ; Test Cases as (Input, Output) pairs:
  20. ; (0,0),(3,0),(4,14),(5,30),(11,126),
  21. ; (15,190),(19,254),(20,255),(100,255),(255,255)
  22. ; C prototype   uint8_t Linear(uint8_t x){
  23.        EXPORT Linear
  24. Linear
  25.     PUSH{R2, R3, R4, LR}
  26.     MOV R2, #16
  27.     MOV R3, #50
  28.     MUL R4, R0, R2
  29.     SUBS R4, R4, #50
  30.     BVS overflow
  31.     BMI underflow
  32.     CMP R4, #255
  33.     BHI overflow
  34.     MOV R0, R4
  35.     POP{R2, R3, R4, LR}
  36.     BX LR
  37.    
  38. overflow
  39.     MOV R0, #255
  40.     POP{R2, R3, R4, LR}
  41.     BX LR
  42.  
  43. underflow
  44.     MOV R0, #0
  45.     POP{R2, R3, R4, LR}
  46.     BX LR
  47.  
  48. ;******************Swap**************************
  49. ; You are given an 11-element 16-bit array.
  50. ; Your function should swap the order of the data in the array
  51. ; Input: pointer to array
  52. ; Output: none
  53. ; Error conditions: none
  54. ; Test Cases:
  55. ; 1. buf before: -5, 4, 7, 0,-1, 3, 4,-8, 2, 9, 9  
  56. ;    buf after:  9, 9, 2,-8, 4, 3,-1, 0, 7, 4,-5  
  57. ; 2. buf before: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11
  58. ;    buf after:  11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  59. ; 3. buf before: 1000,2,3,4,5,-1000,7,10000,9,10,0
  60. ;    buf after:  0,10,9,10000,7,-1000,5,4,3,2,1000
  61. ; C prototype   void Swap(int16_t buf[11]){
  62.        EXPORT Swap
  63. Swap
  64.     PUSH{R0, R1, R2, R3, R4, R5, R12, LR}
  65.     SUB SP, #16 ;Allocate
  66.     MOV R5, #22
  67.     MOV R4, #0
  68.     MOV R3, #0
  69.    
  70. loop
  71.     CMP R3, R5
  72.     BEQ switch
  73.     LDRH R2, [R0, R3]
  74.     PUSH{R2, R12} ;Access
  75.     ADD R3, R3, #2
  76.     B loop
  77.    
  78. switch
  79.     CMP R4, R5
  80.     BEQ done
  81.     POP{R2, R12}
  82.     STRH R2, [R0, R4]
  83.     ADD R4, R4, #2
  84.     B switch
  85.  
  86. done
  87.     ADD SP, #16 ;DeAllocate
  88.     POP{R0, R1, R2, R3, R4, R5, R12, LR}
  89.     BX LR
  90.  
  91.  
  92. ;struct LabGrades{
  93. ;  int32_t size;
  94. ;  int32_t score[10];
  95. ;};
  96. ;typedef struct LabGrades LabGrades_t;
  97. ; *******************Average******************
  98. ; You will write this function.
  99. ; The function should take a pointer to a lab grade structure and return the average.
  100. ; Average can be calculated only if the size is 1 to 10.
  101. ; Input:  Pointer to a lab grade structure
  102. ; Output: Average of the lab grades
  103. ; Error conditions: If the size is outside the range of 1 to 10, return 0.
  104. ; test data
  105. ; size  score                                  Average
  106. ; 10  |  90  90  90  90  90  90  90  90  90  90 | 90
  107. ; 5   |  90  91  92  93  94                     | 92
  108. ; 1   | 100                                     |100
  109. ; 5   |  85  90  100 70 -25                     | 64
  110. ; 8   |  -4  -5  -6  -7 -10   1   2   5         | -3
  111. ; 0   |                                         |  0
  112. ; 255 |                                         |  0
  113. ; C prototype   int32_t Average(LabGrades_t *pt){ ; debug this code
  114.        EXPORT Average
  115. Average
  116.  
  117.     PUSH{R1, R2, R3, R4, R5, R6, LR}
  118.     MOV R6, #0
  119.     MOV R5, #0
  120.     ADD R2, R0, #4
  121.     LDR R1, [R0] ;Load R1 with size
  122.     CMP R1, #10
  123.     BHI overflow1
  124.     CMP R1, #0
  125.     BEQ overflow1
  126.  
  127. loop1
  128.     CMP R5, R1
  129.     BEQ done1
  130.     LDR R3, [R2]
  131.     ADD R6, R6, R3
  132.     ADD R2, R2, #4
  133.     ADD R5, R5, #1
  134.     B loop1
  135.  
  136. done1
  137.     SDIV R0, R6, R1
  138.     POP{R1, R2, R3, R4, R5, R6, LR}
  139.     BX LR
  140. overflow1
  141.     MOV R0, #0
  142.     POP{R1, R2, R3, R4, R5, R6, LR}
  143.     BX LR
  144.  
  145. ; **************ClassAverage****************
  146. ;   Find the average of all the lab grades in the class
  147. ;   Sum up all grades and divide by the number of grades
  148. ;   Do not sum up student averages and divide by the number of students
  149. ;   if size is 255, it means end of list
  150. ;   When dividing, do not round, simply divide sum/count
  151. ; Each Labgrade structure is 44 bytes (4 bytes for size and 40 bytes for 10 grades)
  152. ; Input:  array of Grades_t data
  153. ; Output: the average lab grade
  154. ; Error conditions: if there are no students or no grades, return 0
  155. ;------------------------------------------------------------------
  156. ;Case 1: six students in the class
  157. ;{{5,{84,90,88,70,-25}},    
  158. ; {1,{70}},    
  159. ; {9,{90,90,90,90,-90,70,10,10,10}},
  160. ; {0,{}},
  161. ; {10,{80,80,80,80,80,80,80,80,80,99}},
  162. ; {2,{80,82}},
  163. ; {255,{}}
  164. ;}
  165. ;Class Average = 64; (see handout for explanation)
  166. ;------------------------------------------------------------------
  167. ;Case 2: three students in the class
  168. ;{{2,{100,100,}},
  169. ; {1,{95}},
  170. ; {2,{90,90}},
  171. ; {255,{0}}
  172. ;}
  173. ;Class Average = (100+100+95+90+90)/5 = 475/5 = 95
  174. ;-------------------------------------------------------------------
  175. ;Case 3: one student in the class
  176. ;{{4    ,{-1,-1,-1,-1}},
  177. ; {255,{0}}
  178. ;}
  179. ;Class Average = -1;
  180. ;
  181. ;Case 4:    no students at all
  182. ;{{255,{0}}
  183. ;}
  184. ;Class Average = 0;
  185. ;------------------------------------------------------------------
  186. ; C prototype   int32_t ClassAverage(LabGrades_t ee319k[]){
  187.        EXPORT ClassAverage
  188. ClassAverage
  189.     PUSH{R1, R2, R3, R4, R5, R6, R7, R8, R9, LR}
  190.     MOV R1, R0
  191.     MOV R4, #0
  192.     MOV R2, #0
  193.     MOV R6, #0
  194.     MOV R7, #0
  195.     MOV R8, #0
  196.    
  197. loop2
  198.     MOV R0, R1
  199.     LDR R3, [R0]
  200.     CMP R3, #255
  201.     BEQ done2
  202.     ADD R8, R1, #4
  203.     MOV R6, #0
  204.  
  205. addloop
  206.     CMP R6, R3 ;compare to size to see if done
  207.     BEQ next
  208.     LDR R4, [R8]
  209.     ADD R2, R2, R4
  210.     ADD R8, R8, #4
  211.     ADD R6, R6, #1 ;number of elemnts in particular labgrade section
  212.     ADD R7, R7, #1 ;number of elements total
  213.     B addloop
  214.  
  215. next
  216.     ADD R1, R1, #44
  217.     B loop2
  218.  
  219. done2
  220.     SDIV R0, R2, R7
  221.     POP{R1, R2, R3, R4, R5, R6, R7, R8, R9, LR}
  222.     BX LR
  223.  
  224.  
  225.        END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement