PDA

View Full Version : Two statements from assembly - trying to convert the code in PBasic Pro



Megahertz
- 12th May 2012, 19:24
Can someone please advise what the following two statements mean (or doing) in the code? I have the code in assembly which I am converting to basic pro, but I don't have a clue what these statements are doing. They are extracted from the ISR routine.


movlw ((INTHAND) >> 8)
movwf PCLATH

SteveB
- 13th May 2012, 10:27
movlw ((INTHAND) >> 8) => Shift the constant (literal) 8 bits right (equivalent to dividing by 256) then put the value in WREG

movwf PCLATH => Move the value in WREG into PCLATH.

So, these two statement combined are putting INTHAND/256 into the the middle byte of the program counter.

Megahertz
- 7th June 2012, 23:28
Steve, but
1)INTHAND is not a variable, in fact it is mentioned in the statement
define INTHAND _ISR

2)What is the basic purpose of loading value in PCLATH

cheers

SteveB
- 13th June 2012, 19:23
Steve, but
1)INTHAND is not a variable, in fact it is mentioned in the statement
define INTHAND _ISR

Yes, you are right. But I didn't say it was a variable. ;)




2)What is the basic purpose of loading value in PCLATH

The PCLATH is used (along with PCLATU) to make changes to the Program Counter, which is akin to directing the the program to a new location in the code: a "Computed GOTO."

Megahertz
- 14th June 2012, 00:04
I am confused, you said
Shift the constant (literal) 8 bits right (equivalent to dividing by 256) then put the value in WREG Where is the constant here?

HenrikOlsson
- 14th June 2012, 06:20
I really suck at assembly programming but I'm going to say (write) what I think, then hopefully someone will correct me if I'm wrong.

The constant in this case is called INTHAND which is defined to contain the adress, in program memory, where the routine _ISR is located. What happens is that PCLATH (the high byte of the program counter) gets loaded with the 8 high bits of the adress of _ISR. I suspect there's also code to take the low 8 bits and put them in PCLATU. All in all, that would cause the program to jump to _ISR.

After reading SteveB's other posts that's my understanding of it but again I might very well be wrong.

/Henrik.

SteveB
- 14th June 2012, 17:33
I really suck at assembly programming but I'm going to say (write) what I think, then hopefully someone will correct me if I'm wrong.

The constant in this case is called INTHAND which is defined to contain the adress, in program memory, where the routine _ISR is located. What happens is that PCLATH (the high byte of the program counter) gets loaded with the 8 high bits of the adress of _ISR. I suspect there's also code to take the low 8 bits and put them in PCLATU. All in all, that would cause the program to jump to _ISR.

After reading SteveB's other posts that's my understanding of it but again I might very well be wrong.

/Henrik.

:D :D :D

Very good deduction Henrik!

Megahertz
- 14th June 2012, 17:40
Ok it has started making sense now. But 1 weird thought is there still, this line itself is part of the ISR routine, then what do you mean by
All in all, that would cause the program to jump to_ISR.

SteveB
- 14th June 2012, 17:56
In PBP, the label "ISR" will be annotated in assembly as "_ISR" as a literal (i.e. constant). It represents the address of the first instruction following the label in PBP. The address is a 21 bit address. The bottom 8 bits (low byte, bits 0-7) are directly readable/writeable. The high byte (bits 8-15) and upper byte (bits 16-20) can only be read/written through the registers PCLATH and PCLATU respectively

So... You will likely see something like this as well:

movlw ((INTHAND) >> 16)
movwf PCLATU

movlw INTHAND
movwf PCL


When PCL is written to, the contents of PCLATH and PCLATU are written to the Program Counter along with the value to the PCL. This will cause the instruction found at that address to be the next instruction executed.

One other thing. Since INTHAND is a constant, ((INTHAND) >> 16) and ((INTHAND) >> 8) will be evaluated when the program is compiled. So, the actual assembly will only see an 8 bit value.

Megahertz
- 8th July 2012, 18:24
Thanks for all the replies. Though I somewhat understood what you have said above.
I just to finally confirm what you have advised still applies as the following is the whole context saving code for PIC 16F676:


define INTHAND _ISR

' The main program starts here
goto main

ISR:
asm
movwf wsave ; Save WREG
swapf STATUS, W
clrf STATUS ; Point to bank 0
movwf ssave ; Save STATUS
movf FSR,w
movwf fsave ; save FSR
movf PCLATH, W ; Save PCLATH
movwf psave
; No problems in understanding up to here

; get ready to jump within the ISR page
movlw ((INTHAND) >> 8) ; Set PCLATH for jump
movwf PCLATH


There is no PCL related statement in the context saving. Correct me if I am wrong, does INTHAND has address 4 stored in it?

Mike, K8LH
- 8th July 2012, 22:03
The PBP instruction you're looking for might be "goto INTHAND", but, I wonder if there's really any need to jump to another program area for interrupt handler code?

Megahertz
- 9th July 2012, 16:02
Hello Mike, Yes you do need to go to another location of memory when interrupts get involved.
I read the datasheet of the 16f676 and it does say the whole memory is only one page, so I don't know why PCLATH needs touching, I also checked the ISR routine example in the datasheet and it does not mentions anything about the PCLATH. Only saves WREG & STATUS.:confused::confused:

Mike, K8LH
- 10th July 2012, 01:22
Hi Megahertz,

Was the original assembly language program written for a 16F676? Also, what is the source for the original assembly language program, please?

Cheerful regards, Mike

Megahertz
- 10th July 2012, 18:05
This was written for 16F676 only originally. The source I don't know but I got it from my friend at UNI, we both are trying to understand the context saving part of it.