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

codewarrior hcs12 The array sample contains 7 8-bit signed numbers as shown belo

ID: 3591946 • Letter: C

Question

codewarrior hcs12

The array sample contains 7 8-bit signed numbers as shown below. Write a program which computes the sum of negative numbers, the sum of positive numbers, and the sum of the absolute values of all the numbers, and stores them in psum, nsum, and tsum, respectively. Note that each sum is represented in a word (16 bits). You must use a loop which is iterated 7 times. Outside the loop, there should be only the initialization. Your program should work for any 7 numbers stored in samples. RAMStart-$0800 and ROMStart-$4000. Add a comment on cach instruction samples DC.B $27,$95,$75,SE2,SCB,$41,$44 nsum DC.W 0 psum tsumDC. 0 DC.W 0

Explanation / Answer

loopCount       DC.W 0                                  // to iterate over samples 7 times

LOOP :

            cmp     samples[loopCount], #0          // compare with 0

            jg         POS                                         // if samples[loopCount] >0, positive number

            addi     nsum, samples[loopCount]      // add samples[loopCount] to nsum

            addi     tsum, samples[loopCount]      // add samples[loopCount] to tsu,

            inc       loopCount                               // increment loopCount

            cmp     loopCount, #7                         // Check if loop has executed 7 times or not

            je         EXIT                                       // if above condition is true then exit

            jmp      LOOP                                     // otherwise loop again

POS:

            addi     psum, samples[loopCount]   // add samples[loopCount] to psum

            addi     tsum, samples[loopCount]   

            inc       loopCount

            cmp     loopCount, #8

            je         EXIT

            jmp      LOOP

EXIT:

            // assuming R0, R1 & R2 are 16-bit registers

            mov     R1, psum                                 // store psum in register R1

            mov     R2, nsum                                 // store nsum in register R2

            mov     R0, tsum                                  // store tsum in register R0