PIC18 Branch, simple question.

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

Reply to
Mats Sparr
Loading thread data ...

with

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

Reply to
cbarn24050

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

Reply to
Peter

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
Reply to
CBFalconer

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

Reply to
Michael Lange

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
Reply to
Spehro Pefhany

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!

"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

Reply to
Charles Jean

This is your assembler. It expect BRA 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

Reply to
Ian Bell

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]

Reply to
Jet Morgan

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 * ?

Reply to
Lanarcam

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)

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

Paul

Reply to
Paul Keinanen

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;)

Reply to
Lanarcam

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.