jeffythedragonslayer wrote:
Oh, ok thx, so that doesn't require fiddling with DB, but the 24-bit address does have to be on the stack and isn't hardcoded in the instruction stream like it is with absolute long addressing.
Like Proxy says.
Direct page can be anywhere in bank $00 RAM, which is also true with the stack. If the two are coincident, direct page addressing may be used to access stack elements, which means the stack can be used as an ephemeral direct page inside subroutines. The ability to do so is aided by the fact that SP (stack pointer) can be copied directly to the 16-bit accumulator and arithmetic performed on whatever address is therein. Here’s some simple code that demonstrates how it works:
Code:
;using the stack as temporary direct page
;
;registers: .C — 16-bit accumulator
; .Y — Y-index
; DP — direct page (16 bits)
; SP — stack pointer (16 bits)
;
;Entry SP = $B7FF in this example. We need 3 bytes for a long DP pointer.
;
target = $028000 ;target address for data fetch/store
;
rep #%00100000 ;16-bit accumulator
sep #%00010000 ;8-bit index
;
; Reserve stack space & relocate direct page...
;
phd ;save current DP (SP = $B7FD)
tsc ;copy SP to .C
sec ;reserve 3 stack bytes
sbc !#3 ;!# means 16-bit immediate
tcs ;SP is now at $B7FA
inc A ;starting address of the reserved space
tcd ;point DP to reserved space (DP = SP+1 or $B7FB)
;
; Set up direct page pointer...
;
lda !#target & $FFFF ;$8000
ldx #target >> 16 ;$02
sta $00 ;set up long...
stx $02 ;direct page address
;
; Do data fetch, process & store...
;
rep #%00010000 ;16-bit index
ldy !#$1234 ;some random index value
lda [$00],y ;fetch from target address + .Y...
;
; The above instruction is effectively LDA $028000,Y or
; LDA $029234, even though $028000,Y is not a supported
; addressing mode. Bytes will be loaded from $029234 &
; $029235.
;
; ...data gets processed...
;
iny
iny
sta [$00],y ;store processed data at $029236 & $029237
;
; Clean up stack & restore DP...
;
clc
tsc ;SP to .C ($B7FA)
adc !#3 ;relinquish reserved stack space
tcs ;SP is now $B7FD
pld ;restore DP (SP is now $B7FF)
;
...program continues...
The above illustrates a concept whose simplicity belies the flexibility it offers to the programmer.