Read two numbers and return a sum, in PIC24 Assembly .data
D0:  .byte 0x04; first number
D1:  .byte 0x03; second number
.bss
R0: .space 2;result
.txt
_main:   ..
         ..
         CALL mySUB
         ..
mySUB:   ...
         ....
         return
 .end


Given above, write a program that would read the numbers (in D0 and D1) and call a subroutine, mySUB, that adds the two number and returns the result. Then your main program saves the result in R0.

My attempt at this was

.data
D0:  .byte 0x04; first number
D1:  .byte 0x03; second number
.bss
R0: .space 2;result
.txt
_main:   mov.b #D0,W1
         mov.b #D1,W2
         CALL mySUB
         mov W3,[W0]; once the subroutine returns, it comes to this line, which stores the result to R0's address
mySUB:   mov #R0,W0
         add W1,W2,W3; W3 = D0+D1
         return; return back to where it was called
 .end


I feel confident that this is the right set of instructions but need confirmation.
As @Snowbody already pointed asked, what is your code supposed to after this line:

mov W3,[W0]


It seems as though the execution of your program will fall through and re-enter the mySUB subroutine again.

To fix this...

Create a second label after the mySUB. Then, after _main is done working with the return values of mySUB, you can jmp (or whatever it is called in PIC24) to the second label that you created.

Then, execution will jump over mySUB and hit the .end pragma at the end.



Your mySUB subroutine seems to be doing more than it's job.

The very first line of this subroutine is 

mov #R0, W0


This line is moving a pointer to a memory location into W0. However, this subroutine is supposed to add numbers together. If it was meant to add numbers together and then put the result in memory, then why isn't it doing that at the end of the subroutine?

In my opinion, you should move the first line out of the subroutine and to the first line after the subroutine call (and before mov W3,[W0]).

That way, the subroutine is doing it's job of adding two numbers together, and the _main routine is doing it's job of getting two numbers added together, and then putting that sum in memroy



In the future, I recommend that you add some sort of documentation for each subroutine you have. It doesn't have to be a lot; it can be something as simple as this:

; Adds two numbers
; Input: W1, W2
; Output: W3 = W1 + W2
mySUB: ...