Fall 2013 CSCI 255 Homework 8

This assignment is due 10:00 AM on 2 November at the HW8 moodle page.

This is a rather contrived programming assignment. But then, that is often the case with assembly programming.

The setup

Assume that your code is “called” under the following conditions, which is very similar to the startup code distributed for the Introducing MPLAB X & PIC assembly lab.

Or, more precisely, start with this:

          .include  "p24Hxxxx.inc"
          .global   __reset
          .equiv    vSize,100
i:        .space    2
j:        .space    2
V:        .space    2*#vSize
x:        .space    2
ugh:      .space    2	
          .equ      initX,255
;;; initialize V to the first 100 square numbers
          mov       #vSize,W5       ;; countdown
          mov       #V,W6           ;; pointer into the array
          clr       W7              ;; evolving square
          mov       #1,W8           ;; odd numbers
          mov       W7,[W6++]       ;; moving W7 to elements of V
          add       W7,W8,W7        ;; W7 will be 0, 1, 4, 9, ...
          inc2      WREG8           ;; w8 will be 1, 3, 5, ....
          dec       WREG5           ;; W5 will count down from 100
          btss      SR,#Z           ;; skip next instruction when Z set
          bra       initloop

brkpnt:   mov       x               ;; just a place to put a break point

;;;     Replace this line with your name

;;;     Start writing your PIC Assembly here

;;;     Stop writing your PIC Assembly here

          bra       bigloop

Your job

Your job is to replace the middle section of the program with PIC assembly code that does the equivalent of the following C/Java code:

  x = 0 ;
  for (i=0; i<100; ++i) {
    j = V[i] ;
    while (j != 0) {
      x = x + (j & 0x3) ;
      j = j >> 2 ;

In other words, add up all the pairs of bits in V and store the sum in x.

You are allowed to rearrange the Java statements or use registers to store temporary values. You can also “unroll” the inner loop. However when the outer loop is finished, x must contain the correct answer and V must contain its original values.