EmbeddedRelated.com
Forums
Memfault Beyond the Launch

PIC18 Branch, simple question.

Started by Mats Sparr May 2, 2005
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
Mats Sparr wrote:
> 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
the PC is the address of the program counter, so if you modify it will execute the next instruction from that point.
"Mats Sparr" <prylbogen@home.se> wrote in message 
news:5b8c83ef.0505012322.79e1859d@posting.google.com...
> 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
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
Mats Sparr wrote:
> > 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.
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 "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
Hi Mats,

you understand the manual right, I think. I'm not so with the PIC18, but 
much more with PIC16 and some other &#4294967295;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

On 2 May 2005 00:22:06 -0700, the renowned prylbogen@home.se (Mats
Sparr) wrote:

>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
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" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com
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, prylbogen@home.se (Mats Sparr) wrote:

>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
"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
Mats Sparr wrote:

> 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
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
"Mats Sparr" <prylbogen@home.se> wrote in message
news:5b8c83ef.0505012322.79e1859d@posting.google.com...
> 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!
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]
Jet Morgan wrote:
> "Mats Sparr" <prylbogen@home.se> wrote in message > news:5b8c83ef.0505012322.79e1859d@posting.google.com... > > 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! > > 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".
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 * ?

Memfault Beyond the Launch