Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; ****Your name goes here*******
- ;Yasir Choudhury
- ; This is Exam2_ArrayofStruct
- ; EE319K Fall 2014 exam2, November 6, 2014
- ; You edit this file only
- AREA Data, ALIGN=4
- AREA |.text|, CODE, READONLY, ALIGN=2
- THUMB
- ;***************** Linear****************************
- ; Calculate the result of a linear equation y = 16*x-50
- ; Input parameter: x is unsigned 8 bits
- ; Output parameter: y is unsigned 8 bits
- ; Error conditions: implement ceiling on overflow
- ; implement floor on underflow
- ; Test Cases as (Input, Output) pairs:
- ; (0,0),(3,0),(4,14),(5,30),(11,126),
- ; (15,190),(19,254),(20,255),(100,255),(255,255)
- ; C prototype uint8_t Linear(uint8_t x){
- EXPORT Linear
- Linear
- PUSH{R2, R3, R4, LR}
- MOV R2, #16
- MOV R3, #50
- MUL R4, R0, R2
- SUBS R4, R4, #50
- BVS overflow
- BMI underflow
- CMP R4, #255
- BHI overflow
- MOV R0, R4
- POP{R2, R3, R4, LR}
- BX LR
- overflow
- MOV R0, #255
- POP{R2, R3, R4, LR}
- BX LR
- underflow
- MOV R0, #0
- POP{R2, R3, R4, LR}
- BX LR
- ;******************Swap**************************
- ; You are given an 11-element 16-bit array.
- ; Your function should swap the order of the data in the array
- ; Input: pointer to array
- ; Output: none
- ; Error conditions: none
- ; Test Cases:
- ; 1. buf before: -5, 4, 7, 0,-1, 3, 4,-8, 2, 9, 9
- ; buf after: 9, 9, 2,-8, 4, 3,-1, 0, 7, 4,-5
- ; 2. buf before: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11
- ; buf after: 11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- ; 3. buf before: 1000,2,3,4,5,-1000,7,10000,9,10,0
- ; buf after: 0,10,9,10000,7,-1000,5,4,3,2,1000
- ; C prototype void Swap(int16_t buf[11]){
- EXPORT Swap
- Swap
- PUSH{R0, R1, R2, R3, R4, R5, R12, LR}
- SUB SP, #16 ;Allocate
- MOV R5, #22
- MOV R4, #0
- MOV R3, #0
- loop
- CMP R3, R5
- BEQ switch
- LDRH R2, [R0, R3]
- PUSH{R2, R12} ;Access
- ADD R3, R3, #2
- B loop
- switch
- CMP R4, R5
- BEQ done
- POP{R2, R12}
- STRH R2, [R0, R4]
- ADD R4, R4, #2
- B switch
- done
- ADD SP, #16 ;DeAllocate
- POP{R0, R1, R2, R3, R4, R5, R12, LR}
- BX LR
- ;struct LabGrades{
- ; int32_t size;
- ; int32_t score[10];
- ;};
- ;typedef struct LabGrades LabGrades_t;
- ; *******************Average******************
- ; You will write this function.
- ; The function should take a pointer to a lab grade structure and return the average.
- ; Average can be calculated only if the size is 1 to 10.
- ; Input: Pointer to a lab grade structure
- ; Output: Average of the lab grades
- ; Error conditions: If the size is outside the range of 1 to 10, return 0.
- ; test data
- ; size score Average
- ; 10 | 90 90 90 90 90 90 90 90 90 90 | 90
- ; 5 | 90 91 92 93 94 | 92
- ; 1 | 100 |100
- ; 5 | 85 90 100 70 -25 | 64
- ; 8 | -4 -5 -6 -7 -10 1 2 5 | -3
- ; 0 | | 0
- ; 255 | | 0
- ; C prototype int32_t Average(LabGrades_t *pt){ ; debug this code
- EXPORT Average
- Average
- PUSH{R1, R2, R3, R4, R5, R6, LR}
- MOV R6, #0
- MOV R5, #0
- ADD R2, R0, #4
- LDR R1, [R0] ;Load R1 with size
- CMP R1, #10
- BHI overflow1
- CMP R1, #0
- BEQ overflow1
- loop1
- CMP R5, R1
- BEQ done1
- LDR R3, [R2]
- ADD R6, R6, R3
- ADD R2, R2, #4
- ADD R5, R5, #1
- B loop1
- done1
- SDIV R0, R6, R1
- POP{R1, R2, R3, R4, R5, R6, LR}
- BX LR
- overflow1
- MOV R0, #0
- POP{R1, R2, R3, R4, R5, R6, LR}
- BX LR
- ; **************ClassAverage****************
- ; Find the average of all the lab grades in the class
- ; Sum up all grades and divide by the number of grades
- ; Do not sum up student averages and divide by the number of students
- ; if size is 255, it means end of list
- ; When dividing, do not round, simply divide sum/count
- ; Each Labgrade structure is 44 bytes (4 bytes for size and 40 bytes for 10 grades)
- ; Input: array of Grades_t data
- ; Output: the average lab grade
- ; Error conditions: if there are no students or no grades, return 0
- ;------------------------------------------------------------------
- ;Case 1: six students in the class
- ;{{5,{84,90,88,70,-25}},
- ; {1,{70}},
- ; {9,{90,90,90,90,-90,70,10,10,10}},
- ; {0,{}},
- ; {10,{80,80,80,80,80,80,80,80,80,99}},
- ; {2,{80,82}},
- ; {255,{}}
- ;}
- ;Class Average = 64; (see handout for explanation)
- ;------------------------------------------------------------------
- ;Case 2: three students in the class
- ;{{2,{100,100,}},
- ; {1,{95}},
- ; {2,{90,90}},
- ; {255,{0}}
- ;}
- ;Class Average = (100+100+95+90+90)/5 = 475/5 = 95
- ;-------------------------------------------------------------------
- ;Case 3: one student in the class
- ;{{4 ,{-1,-1,-1,-1}},
- ; {255,{0}}
- ;}
- ;Class Average = -1;
- ;
- ;Case 4: no students at all
- ;{{255,{0}}
- ;}
- ;Class Average = 0;
- ;------------------------------------------------------------------
- ; C prototype int32_t ClassAverage(LabGrades_t ee319k[]){
- EXPORT ClassAverage
- ClassAverage
- PUSH{R1, R2, R3, R4, R5, R6, R7, R8, R9, LR}
- MOV R1, R0
- MOV R4, #0
- MOV R2, #0
- MOV R6, #0
- MOV R7, #0
- MOV R8, #0
- loop2
- MOV R0, R1
- LDR R3, [R0]
- CMP R3, #255
- BEQ done2
- ADD R8, R1, #4
- MOV R6, #0
- addloop
- CMP R6, R3 ;compare to size to see if done
- BEQ next
- LDR R4, [R8]
- ADD R2, R2, R4
- ADD R8, R8, #4
- ADD R6, R6, #1 ;number of elemnts in particular labgrade section
- ADD R7, R7, #1 ;number of elements total
- B addloop
- next
- ADD R1, R1, #44
- B loop2
- done2
- SDIV R0, R2, R7
- POP{R1, R2, R3, R4, R5, R6, R7, R8, R9, LR}
- BX LR
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement