.include "p24Hxxxx.inc"

            .global  __reset          ;The label for the first line of code.  

            .bss            ;unitialized data section
k:    .space   2
j:    .space   2
; i is just a loop count and will be stored in a register

;..............................................................................
;Code Section in Program Memory
;..............................................................................
;
;    uint16 i,j,k;
;
;    k=1;
;    for (i=0;i!=16;i++){    //do for 16 bits
;   
;        if((j & 0x0001)==0){
;             k=k<<1;
;        }
;        j=j>>1;        //looks at the next bit
;
;    }


            .text
__reset:
            mov      #__SP_init, w15       ;Initalize the Stack Pointer
            mov      #__SPLIM_init,W0  
            mov      W0, SPLIM             ;Initialize the stack limit register

; uint16 k;
; k=1
mov #1, w0
mov wreg, k       ; k=1

; uint16 j;
mov #0, w0
mov wreg, j       ; j is initialized to 0 although not done in the C code

; uint16 i;
mov #0, w1        ; use W1 as the loop counter, i, and set i=0

; for (i=0;i!=16;  ){   
topLoop:
cp w1, #16        ; calculate i-16 to set status register bits 
bra Z, endLoop    ; branch to end of loop if i equal 16

; j && 0x0001
mov j, wreg       ; store value of j in w0
and #1, w0        ; calculate j & 1, this sets the status register bits

; if((j && 0x0001)==0){
bra NZ, afterIf   ; using status bits set above, skip the if-body when j&1 != 0  

; k=k<<1;
sl k              ; shift the value of k left 1 bit, k = k << 1

afterIf:          ; end of if-body
lsr j             ; shift the value of j right 1 bit, j = j >> 1

; increment the loop counter and repeat loop-body
inc w1, w1        ; increment the loop counter stored in w1, i++
bra topLoop       ; go to the top of the loop

endLoop:

done:
    goto done
    .end
