BASIC strings from assembler on Vic
Moderator: Moderators
BASIC strings from assembler on Vic
I 'm working on a project for the Vic20 and want to be able to handle BASIC strings from assembler code. What addresses do I use for string-handling from an assembler program?
Re: BASIC strings from assembler on Vic
I just downloaded a ROM disassembly, but it has no addresses and is hard to navigate.
Re: BASIC strings from assembler on Vic
Never mind. I just found the Mapping the Vic PDF online. I was using an extra keyword that didn't appear in the listings.
- Mike
- Herr VC
- Posts: 4849
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
Re: BASIC strings from assembler on Vic
The machine code surely will employ sort of an own memory allocation (i.e. at least running protected from the BASIC interpreter), so if you set aside a small buffer, at address AD say, you can pass strings between BASIC and machine code like thus:
BASIC -> ML: POKE AD,LEN(A$):IF LEN(A$)>0 THEN FOR T=1 TO LEN(A$):POKE AD+T,ASC(MID$(A$,T,1)):NEXT
ML -> BASIC: A$="":IF PEEK(AD)>0 THEN FOR T=1 TO PEEK(AD):A$=A$+CHR$(PEEK(AD+T)):NEXT
Address AD contains the length of the string, which might be zero in case of an empty string. Addresses AD+1 upto AD+LEN(A$) contain the string data itself. The buffer needs to be big enough to hold the longest strings you expect to handle. As BASIC strings are 255 characters maximum, a buffer of 256 bytes length (AD .. AD+255) is sufficient in any case.
BASIC -> ML: POKE AD,LEN(A$):IF LEN(A$)>0 THEN FOR T=1 TO LEN(A$):POKE AD+T,ASC(MID$(A$,T,1)):NEXT
ML -> BASIC: A$="":IF PEEK(AD)>0 THEN FOR T=1 TO PEEK(AD):A$=A$+CHR$(PEEK(AD+T)):NEXT
Address AD contains the length of the string, which might be zero in case of an empty string. Addresses AD+1 upto AD+LEN(A$) contain the string data itself. The buffer needs to be big enough to hold the longest strings you expect to handle. As BASIC strings are 255 characters maximum, a buffer of 256 bytes length (AD .. AD+255) is sufficient in any case.
Re: BASIC strings from assembler on Vic
I thank you for your reply, but I want to handle strings using BASIC ROM routines from ML. Maybe I should create my own routines to handle strings.
- Mike
- Herr VC
- Posts: 4849
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
Re: BASIC strings from assembler on Vic
That was not 100% clear from your OP, but then so be it. The routines in the BASIC interpreter require quite some set-up to get them to do want you want, and any speed advantage compared to BASIC will be close to nil ...HarryP2 wrote:I thank you for your reply, but I want to handle strings using BASIC ROM routines from ML.
... so that is pretty much what you will end up with anyway.Maybe I should create my own routines to handle strings.
If you are so inclined, you might spare a look at my MG BROWSE utility. Besides the fact that it displays a scrolling soft 40 column text screen on the VIC-20, it reads in ASCII text from a file and formats it with word wrapping. I have included the source; the routines ".TFormat" and ".TWordWrap" do the heavy lifting. The source is supposed to be assembled with the inline assembler of Acorn BBC BASIC.
Re: BASIC strings from assembler on Vic
Thank you.
-
- Vic 20 Afficionado
- Posts: 350
- Joined: Tue Apr 14, 2009 8:15 am
- Website: http://wimbasic.webs.com
- Location: Netherlands
- Occupation: farmer
Re: BASIC strings from assembler on Vic
Below is the ML-code behind he INSTR-function in WimBasic. The function searches a substring in a larger string, with an optional starting position.
The result is 0 when not found, non-zero is the location of the substring in the larger string.
Regards,
wimoos
The result is 0 when not found, non-zero is the location of the substring in the larger string.
Regards,
wimoos
Code: Select all
;
; Perform INSTR
;
LA776 JSR $CEFA ; check bracket open
JSR $CD9E ; evaluate expression (A=($61), X=0, Y=$FF)
BIT $0D ; check datatype
BMI LC700 ; branch if string
JSR $D1AA ; convert to word in $64:$65 and in A:Y
TAX ; check if >255
BNE LA773 ;
TYA ; may not be zero
BEQ LA773
PHA
JSR $CEFD ; check comma
JSR $CD9E ; evaluate expression
JSR $CD8F ; check datatype to be string
PLA
TAX
DEX
LC700 LDA $64 ; save address of descriptor
PHA
LDA $65
PHA
TXA
PHA
JSR $CEFD ; check comma
JSR $CD9E ; evaluate expression
JSR $D6A3 ; stack string
JSR $FE49 ; save descriptor in $B7 and $BB:$BC
TAX
BEQ LA773
PLA
STA $A5
PLA
TAY
PLA
JSR $D6AA ; discard string: length in A and address in $22:$23
SEC
SBC $B7
BCC LA7E5
SBC $A5
BCC LA7E5
ADC #$00
TAX
LDA $A5
ADC $22
STA $22
BCC LA7CB
LA7C9 INC $23
LA7CB INC $A5
LDY $B7
LA7CF TYA
BEQ LA7E0
DEY
LDA ($22),Y
CMP ($BB),Y
BEQ LA7CF
DEX
BEQ LA7E5
INC $22
BNE LA7CB
BEQ LA7C9 ; branch always
;
LA7E5 LDY #$00
.BYTE $2C
LA7E0 LDY $A5
JSR $D3A2 ; return result
JMP $CEF7 ; check bracket close
;
LA773 JMP $D248 ; ILLEGAL QUANTITY
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic