PIC18 Branch, simple question.

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I'm a beginner using assembler on PIC18 and I try to understand the
Branch (BRA) instruction. The Manual says that the parameter used with
BRA is ADDED to the Program Counter (PC), but trying this with simple
code makes me thing that the parameter overwrites PC instead.

PC = 0xBE

BRA 4h

This should add 4 + 2 to PC but the PC actually jumps to 0x4!
Am i interpreting the manual wrong? I thought i would be able to jump
forward or backwords relative to the current PC position.

Regards Mats

Re: PIC18 Branch, simple question.

Quoted text here. Click to load it
with

the PC is the address of the program counter, so if you modify it will
execute the next instruction from that point.


Re: PIC18 Branch, simple question.

Quoted text here. Click to load it

Your assembler is being clever and has done the maths to jump to absolute
location 0x4. If you look at the machine code generated you will find that
it has calculated the offset for you.

Peter



Re: PIC18 Branch, simple question.
Quoted text here. Click to load it

You are probably being confused between the actions of the chip and
the actions of the assembler.  The assembler probably (I am
guessing) treats the operand as the absolute destination, and
calculates the offset, which will then appear in the actual code
generated.  This action is usually much more convenient for the
user.

--
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on
We've slightly trimmed the long signature. Click to see the full one.
Re: PIC18 Branch, simple question.
Hi Mats,

you understand the manual right, I think. I'm not so with the PIC18, but
much more with PIC16 and some other µc.

I made an example with the instruction in MPLAB. It looks like the value
4h is read as an absolute adress, so MPLAB calculates the offset to, and
placed it into the code.

If you are unsure, could you post the hex code, which the assembler
created for the "BRA 4h"!?


Michael


Re: PIC18 Branch, simple question.
On 2 May 2005 00:22:06 -0700, the renowned snipped-for-privacy@home.se (Mats

Quoted text here. Click to load it


Yes, and you'll find out that it's implemented like that if you try to
branch too far from the PC position. Or if you look at the generated
op code. You're telling the assembler to branch to absolute address
0x04, and it's calculating the offset for your.

If you want to give it a relative address, you'd write something like:

    BRA  $+4




Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: PIC18 Branch, simple question.
The [BRA location] code is assembled as if it were [GOTO location].
It is used mainly as [BRA label] rather than [BRA literal].  The
assembler will automatically replace the label with the final address
of the label.  Don't forget that sometimes assembler directives and
whatnot will sometimes expand to more than one assembly instruction
upon assembly.  Banksel/BNC/SUBCF/ADDCF come to mind.  It becomes
awfully easy to make errors if you try to figure out these absolute
addresses yourself.  Plus, if you later change the program and slip in
or delete a line or two, you'll have to recalculate the addresses
every time before assembly.  You're making it too hard on yourself.
Let the assembler do all the grunge work for you.
  In the early days of programming prior to assembly language,
programmers would have to code in their own mnemonics, then convert it
into the ones and zeros of machine language.  Many good programmers
were lost due to suicide.
Autopsies invariably showed: "Death by gunshot wound to the head
preceded by total loss of scalp hair".
They had pulled out all their hair then blew their brains out!


On 2 May 2005 00:22:06 -0700, snipped-for-privacy@home.se (Mats Sparr) wrote:

Quoted text here. Click to load it

"There are known knowns.  These are things that we know we know.
There are known unknowns.  That is to say, there are some
things that we know we don't know.  But there are also unknown
unknowns.  These are things we don't know we don't know."
               -Secretary of Defense Donald Rumsfeld

Re: PIC18 Branch, simple question.

Quoted text here. Click to load it

This is your assembler. It expect BRA <address> where address is the program
adress you want to go to, so it works out the value to make this happen.
Most assemblers interpret . (dot) as 'here'. So to jump 4 addresses from
here you would use:

BRA .+4

Ian


Re: PIC18 Branch, simple question.

Quoted text here. Click to load it

Although "branch" instructions usually take as a parameter, the
*distance* to the target, every assembler that I have ever seen
takes *the address of the target itself*, and assembles that into
the "distance to jump".

I can't imagine it working any other way.

Richard [in PE12]



Re: PIC18 Branch, simple question.

Quoted text here. Click to load it
with
simple

For the good old M68K you have BRA dest or JMP dest. In both cases
dest is the address of the destination, but the machine code for
BRA has a pc relative displacement whereas for jump it has an
absolute address.

Ever used BRA * ?


Re: PIC18 Branch, simple question.

Quoted text here. Click to load it

The interesting question is relative to _which_ PC value ? For various
architectures there might be at lest the following cases:

1. The value of the PC instruction (I can't remember any)
2. The PC value between OPcode and displacement (especially with 8 bit
processors)
3. The PC value of the _next_ instruction (most common)

Quoted text here. Click to load it

Are you trying to destroy that memory location by overheating the
core? :-)

Paul


Re: PIC18 Branch, simple question.

Quoted text here. Click to load it
various
bit

That's how, in the case of a serious error, the device was reset
thanks to a watch dog who wasn't fed;)


Site Timeline