bv.chrix

Anything QL Software or Programming Related.
User avatar
dilwyn
Mr QL
Posts: 1644
Joined: Wed Dec 01, 2010 10:39 pm

bv.chrix

Postby dilwyn » Sat Jun 18, 2016 5:40 pm

Having a blank moment writing a BASIC extension.

Need to reserve space for a string on the RI stack and can't remember if the allocation of space on the stack using bv.chrix (vector $11A) should always be an even value or not?

Probably a bug elsewhere in my extension, seems to be going wrong with odd length strings, so I don't know if this is a red herring or not. The guides seem to make no mention of a requirement for the allocation to be an even number, for what it's worth.


--
All things QL - http://www.dilwyn.me.uk
tcat
Super Gold Card
Posts: 619
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: bv.chrix

Postby tcat » Sat Jun 18, 2016 7:33 pm

Hi Dilwyn,

Not sure I have the right answer, I looked in the Technical guide pg.50.

A string is stored as a word character count, followed by the characters of the
string. The string storage always takes a multiple of two bytes.


I realised strings may have to be aligned on even boundary as I once tested UT.CSTR utility function, it only works for strings with even alignment and padding.

Tom


User avatar
tofro
Font of All Knowledge
Posts: 2015
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: bv.chrix

Postby tofro » Sat Jun 18, 2016 9:33 pm

What you are actually looking for ist the paragraph on page 55:

Note that strings must be aligned on the arithmetic stack so that the character count is on a word boundary. All entries on the stack must be a multiple of two bytes long, so that a string of odd length has one byte at the end which contains no information


Allocating an odd length as such is not explicitly forbidden. As (a6,a1) points to the end, though, I would guess all of your stack could end up unaligned. (allocating an odd length would, however, not make a lot of sense, because you would never be able to use the last byte anyhow.)

Defining an odd length data space for a job is not forbidden as well by the manuals - But makes the QL crash anyhow ;)

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 1125
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: bv.chrix

Postby NormanDunbar » Sat Jun 18, 2016 9:58 pm

Dilwyn,

Have a gander at DJTOOLKIT source. There is a need to keep a6,a1 even so asking for an odd allocation will most likely work, but when you return to SuperBasic, it will most likely crash.

I'm sure I read somewhere that you should always allocate an even number of bytes on the stack, so I've always tried to do that.

HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
janbredenbeek
Gold Card
Posts: 395
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: bv.chrix

Postby janbredenbeek » Tue Jun 21, 2016 11:08 pm

Hi Dilwyn,

There is no need to round BV.CHRIX requests to an even value since it will always be rounded up to the nearest multiple of 16 bytes (JS) or 64 bytes (Minerva). This mechanism is explained in the Minerva manual and source code (see bv\chstk.asm).

That said, strings on the RI stack should of course be word aligned since they start with a word containing its length. What I always did was take the length of the string, add 3 (2 for length word + possible padding byte) and then clear bit 0 which yields the total amount of bytes required on the stack, which will always be even.

regards, Jan.


User avatar
dilwyn
Mr QL
Posts: 1644
Joined: Wed Dec 01, 2010 10:39 pm

Re: bv.chrix

Postby dilwyn » Wed Jun 22, 2016 8:57 am

janbredenbeek wrote:Hi Dilwyn,

There is no need to round BV.CHRIX requests to an even value since it will always be rounded up to the nearest multiple of 16 bytes (JS) or 64 bytes (Minerva). This mechanism is explained in the Minerva manual and source code (see bv\chstk.asm).

That said, strings on the RI stack should of course be word aligned since they start with a word containing its length. What I always did was take the length of the string, add 3 (2 for length word + possible padding byte) and then clear bit 0 which yields the total amount of bytes required on the stack, which will always be even.

regards, Jan.

Thank you Jan. As it happens, the problem was elsewhere in the code, as I had ensured the stack space allocated was even "just in case". Took me a long time to work out what I'd done wrong though (like many such silly typo mistakes).


--
All things QL - http://www.dilwyn.me.uk

Who is online

Users browsing this forum: No registered users and 4 guests