# how to end an assembly code

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.

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?

• There is too few information. What do you expect the µC to do at the end of the code? Infinite loop? – M.Ferru Apr 3 '17 at 9:05
• Are you writing a function? If so branch back to where you came from? – Colin Apr 3 '17 at 9:05
• I added more information, I hope it's clear now – A.Bukhari Apr 3 '17 at 9:30
• @A.Bukhari It's still not very clear. How much experience do you have in microcontroller programming? Usually, if you have just one function, you don't end it, you run it in a loop. Please explain in detail what it is that you're trying to do, what is going to call that thing that you want to run, what you expect to happen and so on. – AndrejaKo Apr 3 '17 at 9:36
• I added a PS @AndrejaKo – A.Bukhari Apr 3 '17 at 9:45

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.

Just to make it clear: in microcontroller assembly code there usually isn't an end. There certainly isn't an end instruction. There isn't an OS to return control to, and there's no way to just "stop".

Microcontroller code nearly always contains an infinite loop.

When you work with a µC, you have to make an infinite loop in the end of your code. Otherwise, you won't control what the µC will do. If you forget such a loop, the µC will trigger it and block the execution of the program. (called ISR_TRAP in TI's MSP430)

In C, you will basically end your code with something like while(1) or for(;;). By doing so, the processor will do some idle task so what it remain active and still aware of interruption.

Same for assembly, you have to make your code loop so what it remains active.

loop:
jmp loop