2
\$\begingroup\$
.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.

Snowbody
8,66225 silver badges50 bronze badges
asked May 4, 2015 at 7:32
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Have you tested your programme? Does it work? \$\endgroup\$ Commented May 4, 2015 at 8:12
  • \$\begingroup\$ What's the program supposed to do after it puts the result into W0? Certainly not continue on to execute mySUB again, right? \$\endgroup\$ Commented May 4, 2015 at 14:33

1 Answer 1

4
\$\begingroup\$

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: ...
answered May 4, 2015 at 21:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.