ML noob

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
toby405
Vic 20 Amateur
Posts: 46
Joined: Fri Dec 21, 2012 8:33 pm
Location: USA

ML noob

Post by toby405 »

I wrote this program that sits in the cassette buffer and prints the current BASIC pointer addresses. It's pretty fun, load it and then do NEW to reset SOV.

Do:
SYS828

Then to see pointers move:
10 A=1
RUN
SYS828

I know it wouldn't be any use if you had to load from tape.

My question is in the "print pointer address" section. Is this self-modifying code the way to go? I feel like I might be missing the obvious. Other comments also appreciated.

Code: Select all

; print BASIC pointers
; after this LOADs, SOV points within cassette buffer.
; NEW command will fix.

        PROCESSOR 6502
        ORG $033c       ; SYS828 (cassette buffer)

CHROUT  EQU $ffd2
CR      EQU $0d
ZPALO   EQU $fb
ZPAHI   EQU $fc
PTRCNT  EQU $6

PRTPTRS SUBROUTINE
        lda #<BASICPTRS
        sta ZPALO
        lda #>BASICPTRS
        sta ZPAHI
        
        ldx #0      ; array row

.1      ldy #0      ; array column
        
        ; print pointer name
.2      lda (ZPALO),y
        jsr CHROUT
        iny
        cpy #3
        bcc .2
        
        lda #":"
        jsr CHROUT
        lda #"$"
        jsr CHROUT
        
        ; print pointer address
.3      lda (ZPALO),y
        sta .+4
        lda $0
        jsr PRTBYTE
        iny
        cpy #5
        bcc .3
        
        lda #CR
        jsr CHROUT
        
        clc
        lda ZPALO
        adc #5
        sta ZPALO
        lda ZPAHI
        adc #0
        sta ZPAHI
        
        inx
        cpx #PTRCNT
        bcc .1

        rts
        
PRTBYTE SUBROUTINE
        pha
        lsr
        lsr
        lsr
        lsr
        jsr PRTHEX
        pla
        and #$0f
        jmp PRTHEX

PRTHEX  SUBROUTINE
        cmp #$0a
        bcc .1
        adc #$06
.1      adc #$30
        jmp CHROUT

BASICPTRS
        BYTE "SOB", $2c, $2b
        BYTE "SOV", $2e, $2d
        BYTE "SOA", $30, $2f
        BYTE "EOA", $32, $31
        BYTE "BOS", $34, $33
        BYTE "TOM", $38, $37
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

If you use the X register with the indexed ZP address mode, you can do:

Code: Select all

.3      lda (ZPALO),y 
        TAX
        LDA $00,X 
        jsr PRTBYTE 
        iny 
        cpy #5 
        bcc .3 
Instead of the X register, the outer loop then uses a free ZP address to count down:

Code: Select all

        [...]
ZPROW   EQU  $fd
        [...]
        LDA #PTRCNT        ; number of arrow rows
        STA ZPROW
.1
        [...]
        DEC ZPROW
        BNE .1
Nice tool! 8)
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Post by tlr »

Or do something like this (saving 21 bytes):

Code: Select all

; print BASIC pointers
; after this LOADs, SOV points within cassette buffer.
; NEW command will fix.

        PROCESSOR 6502
        ORG $033c       ; SYS828 (cassette buffer)

CHROUT  EQU $ffd2
CR      EQU $0d
ZPALO   EQU $fb
ZPAHI   EQU $fc

PRTPTRS SUBROUTINE
        lda #<BASICPTRS
        sta ZPALO
        lda #>BASICPTRS
        sta ZPAHI
       
.1      ldy #0      ; array column
       
        ; print pointer name
.2      lda (ZPALO),y
        jsr CHROUT
        iny
        cpy #3
        bne .2
       
        lda #":"
        jsr CHROUT
        lda #"$"
        jsr CHROUT
       
        ; print pointer address
        lda (ZPALO),y
        tax
        lda $1,x
        jsr PRTBYTE
        lda $0,x
        jsr PRTBYTE
        ;iny <--- compensated by sec below
	
        lda #CR
        jsr CHROUT

        tya
        sec       ; compenstation for missing iny
        adc ZPALO
        sta ZPALO
; msb never changes as we are within the same page

        cmp #<BASICPTRSEND
        bne .1

        rts
       
PRTBYTE SUBROUTINE
        pha
        lsr
        lsr
        lsr
        lsr
        jsr PRTHEX
        pla
        and #$0f
;       jmp PRTHEX
	;fall through 
PRTHEX  SUBROUTINE
        cmp #$0a
        bcc .1
        adc #$06
.1      adc #$30
        jmp CHROUT

BASICPTRS
        BYTE "SOB", $2b
        BYTE "SOV", $2d
        BYTE "SOA", $2f
        BYTE "EOA", $31
        BYTE "BOS", $33
        BYTE "TOM", $37
BASICPTRSEND
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Post by tlr »

Or even this (saving a further 12 bytes):

Code: Select all

; print BASIC pointers
; after this LOADs, SOV points within cassette buffer.
; NEW command will fix.

        PROCESSOR 6502
        ORG $033c       ; SYS828 (cassette buffer)

CHROUT  EQU $ffd2
CR      EQU $0d

PRTPTRS SUBROUTINE
       
        ldy #0      ; array column
       
.1
	; print pointer name
.2      lda BASICPTRS,y
        jsr CHROUT
        iny
        cmp #":"
        bne .2
       
        lda #"$"
        jsr CHROUT
       
        ; print pointer address
        ldx BASICPTRS,y
        jsr PRTZPBYTE
        dex
        jsr PRTZPBYTE
        iny

        lda #CR
        jsr CHROUT

        cpy #BASICPTRSLEN
        bne .1

        rts

PRTZPBYTE
        lda $0,x
PRTBYTE SUBROUTINE
        pha
        lsr
        lsr
        lsr
        lsr
        jsr PRTHEX
        pla
        and #$0f
;       jmp PRTHEX
	;fall through 
PRTHEX  SUBROUTINE
        cmp #$0a
        bcc .1
        adc #$06
.1      adc #$30
        jmp CHROUT

BASICPTRS
        BYTE "SOB:", $2b+1
        BYTE "SOV:", $2d+1
        BYTE "SOA:", $2f+1
        BYTE "EOA:", $31+1
        BYTE "BOS:", $33+1
        BYTE "TOM:", $37+1
BASICPTRSLEN equ .-BASICPTRS
User avatar
toby405
Vic 20 Amateur
Posts: 46
Joined: Fri Dec 21, 2012 8:33 pm
Location: USA

Post by toby405 »

Thanks for the great suggestions. I've learned a lot.
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Applying more pressure we get this one below (download meminfo.prg):

It's already small enough (89 bytes) that it can be loaded to 673 instead, so it also works with a tape drive.

I added a blankspace between ':' and '$', the original savings were another 9 (84 versus 93) bytes.

Code: Select all

REM>MemInfoSrc

DIM code 256

FOR pass=4 TO 7 STEP 3
P%=&02A1:O%=code
[OPT pass
.MemInfo
 LDY #0
.MemInfo_00
 LDA Table,Y:BPL MemInfo_01:TAX
 LDA #ASC(":"):JSR &FFD2
 LDA #ASC(" "):JSR &FFD2
 LDA #ASC("$"):JSR &FFD2
 JSR Print:DEX:JSR Print:LDA #13
.MemInfo_01
 JSR &FFD2
 INY:CPY #End-Table:BNE MemInfo_00
 RTS

.Print
 LDA &80,X
 PHA:LSR A:LSR A:LSR A:LSR A
 JSR Print_00
 PLA:AND #&0F
.Print_00
 CMP #10:BCC Print_01:ADC #6
.Print_01
 ADC #ASC("0"):JMP &FFD2

.Table
 EQUS "SOB":EQUB &80+&2B+1
 EQUS "SOV":EQUB &80+&2D+1
 EQUS "SOA":EQUB &80+&2F+1
 EQUS "EOA":EQUB &80+&31+1
 EQUS "BOS":EQUB &80+&33+1
 EQUS "TOM":EQUB &80+&37+1
.End
]
NEXT
Last edited by Mike on Thu Feb 20, 2014 5:47 pm, edited 1 time in total.
FD22
Vic 20 Hobbyist
Posts: 148
Joined: Mon Feb 15, 2010 12:31 pm

Post by FD22 »

Might be an idea to include a note as to which assembler / language-extension you need to build this, as it's certainly not DASM... :wink:
User avatar
toby405
Vic 20 Amateur
Posts: 46
Joined: Fri Dec 21, 2012 8:33 pm
Location: USA

Post by toby405 »

Thanks for making my original look like such a pig. :)
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

FD22 wrote:Might be an idea to include a note as to which assembler / language-extension you need to build this, as it's certainly not DASM... :wink:
It's the inline assembler of HI BASIC, an implementation of BBC BASIC running on the BBC Micro expansion unit.
User avatar
buzbard
Vic 20 Devotee
Posts: 213
Joined: Sun Jul 03, 2005 9:10 am

Post by buzbard »

Mike wrote:It's already small enough (89 bytes) that it can be loaded to 673 instead, so it also works with a tape drive.
If you compile it for 679 ($2a7) it will also work on the C64 as is. It just fits with no room to spare (89 bytes).

2a1 - 2a6 is used on the C64.
Ray..
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Post by wimoos »

There is two bytes to save:

LDA #' ' / JSR $FFD2 can be replaced by a JSR $CB3F

There is two more bytes to save by turning the table around, do DEY instead of INY so you don't have to compare to table-end.

Regards,

Wim
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
FD22
Vic 20 Hobbyist
Posts: 148
Joined: Mon Feb 15, 2010 12:31 pm

Post by FD22 »

Mike wrote:
FD22 wrote:Might be an idea to include a note as to which assembler / language-extension you need to build this, as it's certainly not DASM... :wink:
It's the inline assembler of HI BASIC, an implementation of BBC BASIC running on the BBC Micro expansion unit.
Oh...kay.

So, in a thread started by a self-confessed VIC Assembler newbie, which is littered with DASM (or variant) Assembly examples, you've provided code in a language format that needs not only a completely different machine to assemble, but also a specific language extension to that machine... :lol:

I'm reminded of the scene in Galaxy Quest, when Captain Nesmith confesses to Kwan that the TV shows they've been watching are ficticious and not actually the 'documentaries' they thought they were.

Kwan: "But... WHY??"
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

FD22 wrote:Might be an idea to include a note as to which assembler / language-extension you need to build this, as it's certainly not DASM... :wink:
Mike wrote:It's the inline assembler of HI BASIC, an implementation of BBC BASIC running on the BBC Micro expansion unit.
FD22 wrote:Oh...kay.

So, in a thread started by a self-confessed VIC Assembler newbie, which is littered with DASM (or variant) Assembly examples, you've provided code in a language format that needs not only a completely different machine to assemble, but also a specific language extension to that machine... :lol:

I'm reminded of the scene in Galaxy Quest, when Captain Nesmith confesses to Kwan that the TV shows they've been watching are ficticious and not actually the 'documentaries' they thought they were.

Kwan: "But... WHY??"
*PLONK*
Post Reply