Quantcast

Basic question: bra vs goto

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Basic question: bra vs goto

Matthew Mucker-3
What are the differences between the bra opcode and the goto opcode on the
Pic18F chips?

Both appear to be an unconditional jump statement; my read is that one is a
relative jump (bra) and the other is an absolute jump (goto).

Is the only (practical) difference that GOTO specifies a 20-bit absolute
address while BRA specifies an 11-bit relative address?

When should I use one over the other?  For instance, in the template .asm
files, I see that the reset vector uses a GOTO to jump to main code, while
the interrupt vectors use BRA.  Why?




--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Basic question: bra vs goto

Richard Prosser
Matthew,

Added [PIC} label

O'm not sure on the intricacies of the different PIC families but in
general a goto or jump may use more memory and clock periods than a
branch. YMMMV dependent on the exact microcontroller family involved.

RP

2008/12/17 Matthew Mucker <[hidden email]>:

> What are the differences between the bra opcode and the goto opcode on the
> Pic18F chips?
>
> Both appear to be an unconditional jump statement; my read is that one is a
> relative jump (bra) and the other is an absolute jump (goto).
>
> Is the only (practical) difference that GOTO specifies a 20-bit absolute
> address while BRA specifies an 11-bit relative address?
>
> When should I use one over the other?  For instance, in the template .asm
> files, I see that the reset vector uses a GOTO to jump to main code, while
> the interrupt vectors use BRA.  Why?
>
>
>
>
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
ivp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Basic question: bra vs goto

ivp
> > When should I use one over the other?

BRA does use less memory. Look at a disassembly and you'll see that
GOTO (and CALL) are padded with an NOP. On a couple of occassions
I've ported 16F code to an 18F and saved valuable memory changing
GOTOs to BRAs

It can be a helpful indicator, inasmuch as when you see it you know that
where flow is going is not too far away

> For instance, in the template .asm files, I see that the reset vector
> uses a GOTO to jump to main code, while the interrupt vectors use
> BRA.  Why?

Must just be that example. Vector addresses are spaced far enough
apart to accomodate a GOTO (there'd be trouble if they weren't)

I've never used BRA for the IRQ vector. Sometimes I put the ISR at
the bottom of memory, sometimes I don't. GOTO will find it wherever
it is. Similarly with main code. It may start at 0x100, perhaps there's a
table below it. A BRA won't reach

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Basic question: bra vs goto

Olin Lathrop
In reply to this post by Richard Prosser
Richard Prosser wrote:
> O'm not sure on the intricacies of the different PIC families but in
> general a goto or jump may use more memory and clock periods than a
> branch.

GOTO and BRA are both two cycle instructions because both force the
prefetched instruction to be discarded.  The difference, as clearly
explained in the data sheet, is that GOTO goes to a full absolute address
and takes 2 words, whereas BRA jumps to a limited range relative to the
current location and takes 1 word.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Basic question: bra vs goto

William "Chops" Westfield

On Dec 16, 2008, at 1:10 PM, Olin Lathrop wrote:

> GOTO and BRA are both two cycle instructions because both force the
> prefetched instruction to be discarded.

How come GOTO doesn't take a third cycle to fetch the second word of  
the instruction? Does it actually manage to USE the pre-fetched  
version without any additional penalty?

BillW

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Basic question: bra vs goto

Bob Ammerman
> How come GOTO doesn't take a third cycle to fetch the second word of
> the instruction? Does it actually manage to USE the pre-fetched
> version without any additional penalty?
>
> BillW

The second cycle for a BRA instruction is because it can't use the
prefetched instruction following the BRA.

The second cycle for a GOTO/CALL instruction is because it uses the second
word of the instruction.  Apparently the second word of the instruction is
available soon enough to change the PC in time to fetch the target of the
GOTO/CALL. I remember being impressed with this when I first saw the 18F
instruction set.

Bob Ammerman
--- RAm Systems

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Basic question: bra vs goto

William "Chops" Westfield

On Dec 16, 2008, at 6:37 PM, Bob Ammerman wrote:
>> How come GOTO doesn't take a third cycle ?

> Apparently the second word of the instruction is available soon  
> enough to change the PC in time to fetch the target of the GOTO/
> CALL. I remember being impressed with this when I first saw the 18F  
> instruction set.

I'm impressed too.  Usually an extra word in the instruction is good  
for an extra cycle in execution time, and you still lose a additional  
cycle on branches from needing to get the next instruction from  
somewhere other than your prefetch buffer...

I wonder if they had to exert effort to make this work, or whether it  
fell out of the way that prefetching worked on the PIC16 anyway?

BillW

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Basic question: bra vs goto

WH Tan
In reply to this post by Matthew Mucker-3
2008/12/17 Matthew Mucker <[hidden email]>:

> When should I use one over the other?  For instance, in the template .asm
> files, I see that the reset vector uses a GOTO to jump to main code, while
> the interrupt vectors use BRA.  Why?

I suppose you can use BRA whenever, wherever you like.  Change it to
GOTO if the linker complains.

Or

If you're doing relocatable mode programming, use BRA for jump between
module, and GOTO for inter-module jump.


Best regards,

--
WH Tan
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Loading...