As my text game is getting more advanced, I am running into a problem of appending numbers to strings. The Basic code I have works fine, but it is slow. I tried to make an Axe version, but could not GetCalc( Ans. I thought I could modify one of these routines, but all I could get was ram clears. I think the string needs to be added to the stack or buffer before the 9 byte slot is made and the number added. However, that is beyond me. The input string is in Ans, and the input number would probably go in one of the Basic vars that have there own _rcl bcall: Y, N, and X. Could you help me out?
What kinds of numbers will be input? Integers only? Is there a range on which you expect the numbers?
All of the numbers will be real integers, they are all in the range of 20,000 and -20,000. I am using the monochrome z80 series.
This code isn't exactly what you want, but it takes an integer in Ans and converts it to a string:

Code:

#define bcall(x) rst 28h \ .dw x
OP1         = 8478h
OP3         = OP1+22
rFindSym    = 10h
_RclAns     = 4AD7h
_AnsName    = 4B52h
_DelVar     = 4FC9h
_CreateStrng= 4327h
.db $BB,$6D
.org $9D95
    bcall(_RclAns)
    or a
    ret nz
    ld hl,OP1
    ld de,OP3
    ld a,(hl)
    inc hl
    add a,a
    jr nc,+_
    ld a,$B0    ;the negative sign
    ld (de),a
    inc de
_:
    ld a,(hl)
    sub 7Fh
    ret z       ;we're only accepting integers
    ret c
    inc hl
    ld b,a
    ld a,30h
_:
    rld \ ld (de),a \ inc de
    djnz $+4
    jr +_
    rld \ ld (de),a \ inc de
    inc hl
    djnz -_
_:
    push de
    bcall(_AnsName)
    rst rFindSym
    bcall(_DelVar)
    pop hl
    ld de,OP3
    or a
    sbc hl,de       ;the length of the string
    push hl
    bcall(_CreateStrng)
    pop bc
    ld a,b
    or c
    ret z
    inc de
    inc de
    ld hl,OP3
    ldir
    ret

This converts the real part of complex numbers, too.
Would it work to replace bcall(_RclAns) with bcall(_rclN) \ bcall(_convOP1)?
There is no need for _ConvOP1, that just makes more work. The TI floating point format is made to be easy to convert to a string since it is already in base 10, whereas _ConvOP1 turns it into a binary integer which would require divisions.

Note, however, that this routine returns the result in Ans, it doesn't append to Ans.
All right. If Ans cannot be appended to, it would also work if Str0 was set to Ans, N or Y was morphed into a string, and stored in Ans. This is a sample of how the basic version works right now.

Code:
10->N
"YOU PUNCH THE BUNNY - "
prgmAPPEND
Ans+" DAMAGE."

However, if Str0 was used, the code could look like this:

Code:
10->N
"YOU PUNCH THE BUNNY - "
Asm(prgmAPPEND)
Str0+Ans+" DAMAGE."

It would be shorter if Str0 was set in the Asm, and could possibly be faster than setting it with Basic.
This converts Y to a string and appends it to Ans (a string). It quits if there is not enough RAM available. As well, this version uses the OS call _FormBase, so it will convert any valid number.

Code:

#define bcall(x) rst 28h \ .dw x
OP1         = 8478h
OP3         = OP1+22
_RclY       = 4ADAh
_RclAns     = 4AD7h
_EnoughMem  = 42FDh
_InsertMem  = 42F7h
_FormBase   = 50AAh
flags       = 89F0h
fmtFlags    = 10
fmtOverride = 11
.db $BB,$6D
.org $9D95
    bcall(_RclY)
    ld a,32
    ld (flags+fmtOverride),a
    bcall(_FormBase)
    ld h,b
    ld l,c
    bcall(_EnoughMem)
    ret c       ;not enough RAM
    push de
   
    bcall(_RclAns)
    sub 4
    pop hl
    ret nz
    ex de,hl
    ld c,(hl)
    inc hl
    ld b,(hl)
    inc hl
    push hl     ;points to the start of the data
    add hl,bc   ;points to where to insert, DE is size
    push hl
    push de
    ex de,hl
    bcall(_InsertMem)
    pop bc
    pop de
    ld hl,OP3
    ldir
    pop hl
    or a
    ex de,hl
    sbc hl,de
    ex de,hl
    dec hl
    ld (hl),d
    dec hl
    ld (hl),e
    ret

As an example, I tested this code:

Code:

:"NUMS:
:For(Y,0,9
:Asm(prgmAPPEND
:Ans+",
:End

The result was "NUMS:0,1,2,3,4,5,6,7,8,9,"
Thank you so much! I implemented this into my battle system and it almost doubled the speed. I will be sure to add you to the credits.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
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 vote in polls in this forum

 

Advertisement