Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I don\'t understand this assembly subroutine program i need it commented Before

ID: 3891957 • Letter: I

Question

I don't understand this assembly subroutine program i need it commented
Before this part there was a part a which is checking the number of people on a list who has more than x points
and part b is to calculate the average points of all users

SUBSTRACT SP,SP #16  

STORE R2, 12(SP)

STORE R3, 8(SP)

STORE R4, 4(SP)

STORE R5, (SP)

Load R2, 16(SP)

LOAD R3, 20(SP)

CLEAR R4

CLEAR R5

LOOP LOAD R5, 4(R3) // i do not understand here what did he do

ADD R4,R4,R5

SUB R2,R2#1

ADD R3,R3,8 // i do not understand here neither

BRANCH IF_[R2]>0 LOOP

LOAD R2,16(SP)

DIVIDE R4,R4,R2

STORE R4,16(SP)

LOAD R2,12(SP)

LOAD R3, 8(SP)

LOAD R4, 4(SP)

LOAD R5, (SP)

ADD SP,SP #16

RETURN

Explanation / Answer

SUBSTRACT SP,SP #16 // Stack pointer is subtracted by 16 and made as the new stack pointer

STORE R2, 12(SP) // the value of R2 is stored at 12 + [SP]

STORE R3, 8(SP) // the value of R3 is stored at 8 + [SP]

STORE R4, 4(SP) // the value of R4 is stored at 4 + [SP]

STORE R5, (SP) // the value of R5 is stored at [SP]

Load R2, 16(SP) // Load the value present in 16 + [SP] to R2

LOAD R3, 20(SP) // Load the value present in 20 + [SP] to R3

CLEAR R4 // the value of R4 is cleared and set to 0

CLEAR R5   // the value of R5 is cleared and set to 0

LOOP

LOAD R5, 4(R3) // the value at [R3] + 4 memory location is loaded to R5

ADD R4,R4,R5 // R4 and R5 are added and stored back at R4

SUB R2,R2#1 // R2 is subtracted by 1

ADD R3,R3,8 // R3 is added with 8 and stored back at R3

BRANCH IF_[R2]>0 LOOP // It checks whether R2 is greater than 0, If it is greater, then it enters a loop otherwise it will execute the next instruction in sequence

LOAD R2,16(SP) // The value present at 16 + SP memory location is loaded to R2

DIVIDE R4,R4,R2 // R4 is divided by R2

STORE R4,16(SP) // R4 is stored at 6 + [SP] memory location

LOAD R2,12(SP) // value at 12 + [SP] memory location is loaded to R2

LOAD R3, 8(SP)   // value at 8 + [SP] memory location is loaded to R3

LOAD R4, 4(SP)   // value at 4 + [SP] memory location is loaded to R4

LOAD R5, (SP)   // value at [SP] memory location is loaded to R5

ADD SP,SP #16 // Stack pointer is incremented by 16

RETURN // returns the control

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote