6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu May 09, 2024 2:58 am

All times are UTC




Post new topic Reply to topic  [ 16 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Sat Aug 20, 2022 11:52 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8177
Location: Midwestern USA
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.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: