Electronic – how to end an assembly code

assemblypic

I'm trying to write a code for pic16f877 by translating a simple algorithm into assembly code.

The instructions set doesn't have such an instruction.

enter image description here

Here is what I wrote:

MOVF 0X40,W
MOVWF 0X50
SUBWF 0X41,W
BTFSS Status,C
GOTO label
MOVF 0X41 ,W
MOVWF 0X50
label END

My question is: is there a way to "end" an algorithm when programming a microcontroller using assembly code?

Best Answer

Stop and actually think about it.

What do you want the microcontroller to do after it is done with the little bit of logic you show? Surely there is something. Should perform the operation again? Should it go back to some larger task? Should it wait for something external to happen, then do it again? Even if it's supposed to do "nothing" until power is removed, that's actually something you have to tell it to do.

The point is, that there is no "end". It doesn't make sense. Everything requires some action. Even powering itself down via some external switch requires activating that switch.

On a separate topic, flow charts should really indicate the next level up logic, not the details of how the processor will perform that logic. You are specifying details about things like the W register in your flowchart. Instead, it should show something like a comparison block between A and B (or whatever), then two or more choices to take depending on the result. When you code this in assembly, then you figure out the actual instructions to realize the higher level logic described by the flowchart.

It's also bad to refer to variables by their addresses. Refer to variable symbolically, then let the linker decide where to stick them in memory. That's really none of your business, other than perhaps the bank they should be in. Your flowchart refers to three different variables at 40h, 41h, and 50h. Give them names and use the names in the flowchart. Good names also helps illuminate the logic. Even when you code this in assembler, you still give variables names and refer to them by names in the code.