Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

labels vs. subroutines

Status
Not open for further replies.

NewGeek

New Member
I was reading a discussion in the archives about which is better and when to use each, but Im still confused.
Is the PIC able distinguish between a Label and Sub? How? Every sub starts with a label (right?).

Can you only use RETURN after a CALL? is this the difference?
 
NewGeek said:
I was reading a discussion in the archives about which is better and when to use each, but Im still confused.
Is the PIC able distinguish between a Label and Sub? How? Every sub starts with a label (right?).

Can you only use RETURN after a CALL? is this the difference?

Basically yes, a subroutine ends with a RETURN (or RETLW) which pops the previous address off the stack, and carries on executing from that previous address - the stack is very small in PIC's, you can only nest a very small number of subroutine calls.

A GOTO simply jumps to a new address, the old address isn't saved in any way - so it's a very different affair, you've no way of returning to where it started from.
 
Is the PIC able distinguish between a Label and Sub?
I can understand your confusion. First, you must understand what a PIC does and does not do.

A PIC executes a program encoded as machine code. In machine code, there are no labels. Labels are already removed and translated by the assembler or compiler that processed the source program (the one containing labels) to machine code. That procedure was done in your PC not at the PIC. Without labels in the machine code, the PIC cannot distinguish them.

Every sub starts with a label (right?).
Yes, it's good programming practice to tack a label at the start of every subroutine. But you don't have to.

If you define a subroutine to be a section of a program that ends with a return, lookup tables are implemented in the PIC using a series of RETLW instructions. You don't have to put a label on each and every one.
 
Status
Not open for further replies.

Latest threads

Back
Top