Stack and Program counter boxes after the CALL DELAY and at MOVLW 0xAA?

Thread Starter

cikalekli

Joined Dec 10, 2020
103
How can I stack and Program counter boxes after the CALL DELAY and at MOVLW 0xAA?

Code:
#Define PORTB 0xF81
MYREG EQU 0X08
  ORG 0
BACK:
  MOVLW 0X55
  MOVWF PORTB
  CALL DELAY
  MOVLW 0XAA
  MOVWF PORTB
  CALL DELAY
  GOTO BACK

  ORG 300H
DELAY:
  MOVLW 0XFF
  MOVWF MYREG
AGAIN:
  NOP
  NOP
  DECF MYREG, F
  BNZ AGAIN
  RETURN
  END
Mod edit: code tags and formatting- JohnInTX

Here I need to fill in those boxes... I ned to fill in this boxes but I could not solve it correctly
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,444
This is an 18Fxxx part so the program counter increments by TWO for each instruction except GOTO, CALL, and MOVFF which increment the program counter by 4 for each one.
The easiest way to know what the PC will be is to assemble the program and look at the .lst file but you can do it by hand, too.

In the code above, the first MOVLW 0x55 (line 5) is at 0000h - following the ORG 0. MOVWF PORTB (line 6)is at 0002h and so on.

The stack pointer starts at 0 but there is no stack RAM at that location. The stack pointer values for your diagram above go from 1-4, top to bottom.
When you CALL delay, the stack pointer is incremented first then address of the instruction following the CALL (the return address) is placed on the stack (TOSU, TOSH, TOSL). When the RETURN instruction is executed, the contents of TOSU,TOSH and TOSL are copied to the program counter and the stack pointer is decremented by 1.

The program counter location at the first CALL DELAY instruction (line 7) is 0004h. The CALL instruction is 4 bytes so the location of the MOVLW 0xAA (line 8) is 0008h. Executing the CALL increments the stack pointer then copies 000008h to TOSU,TOSH and TOSL then sets the program counter to 000300h (the location of DELAY following the ORG on line 13).

When DELAY returns, the contents of TOSx (000080h) are copied to the program counter, the stack pointer is decremented and execution resumes at line 8.

The operation of the stack and program counter are covered in your chip's data book under PROGRAM MEMORY ORGANIZATION for most 18Fxxxx.

Hope that helps!
Edit: a screen shot of the code and stack just after calling DELAY.
1622576641611.png
 
Last edited:

Thread Starter

cikalekli

Joined Dec 10, 2020
103
This is an 18Fxxx part so the program counter increments by TWO for each instruction except GOTO, CALL, and MOVFF which increment the program counter by 4 for each one.
The easiest way to know what the PC will be is to assemble the program and look at the .lst file but you can do it by hand, too.

In the code above, the first MOVLW 0x55 (line 5) is at 0000h - following the ORG 0. MOVWF PORTB (line 6)is at 0002h and so on.

The stack pointer starts at 0 but there is no stack RAM at that location. The stack pointer values for your diagram above go from 1-4, top to bottom.
When you CALL delay, the stack pointer is incremented first then address of the instruction following the CALL (the return address) is placed on the stack (TOSU, TOSH, TOSL). When the RETURN instruction is executed, the contents of TOSU,TOSH and TOSL are copied to the program counter and the stack pointer is decremented by 1.

The program counter location at the first CALL DELAY instruction (line 7) is 0004h. The CALL instruction is 4 bytes so the location of the MOVLW 0xAA (line 8) is 0008h. Executing the CALL increments the stack pointer then copies 000008h to TOSU,TOSH and TOSL then sets the program counter to 000300h (the location of DELAY following the ORG on line 13).

When DELAY returns, the contents of TOSx (000080h) are copied to the program counter, the stack pointer is decremented and execution resumes at line 8.

The operation of the stack and program counter are covered in your chip's data book under PROGRAM MEMORY ORGANIZATION for most 18Fxxxx.

Hope that helps!
Edit: a screen shot of the code and stack just after calling DELAY.
View attachment 240181
Ah now I get it.. I'm considerably a lot appreciated for you detailed and sophisticated answer...
 
Top