www.retrosoftware.co.uk
http://www.retrosoftware.co.uk/forum/

RESOLVED:Weird "Bad Immediate Constant" error when compiling
http://www.retrosoftware.co.uk/forum/viewtopic.php?f=71&t=808
Page 1 of 1

Author:  paulv [ Tue Nov 20, 2012 9:05 pm ]
Post subject:  RESOLVED:Weird "Bad Immediate Constant" error when compiling

I've got a real head scratcher with this and I'm not sure whether its a bug in the Assembler built into BASIC V or what?

I've got a piece of code that fails to assemble but when I comment out two lines further down the listing, the code above it that failed to compile previously is just fine... I took photo's as it's easier to explain with images...

The code as I want it...

Attachment:
1.jpg [243.82 KiB]
Downloaded 141 times


Assembler failure

Attachment:
2.jpg [106.89 KiB]
Downloaded 141 times


Commenting out the code

Attachment:
3.jpg [233.96 KiB]
Downloaded 141 times


Code compiles fine

Attachment:
4.jpg [169.56 KiB]
Downloaded 141 times


So the question is, what exactly is happening here? The code compiles down to a Relocatable Module that is less than 3KB in size.

I've got shed loads of strings that are all defined as follows:

.unknownMonitor
EQUS " unknown."
EQUB 0

These definitions are all at the end of the code.

Does anyone have any idea what the problem might be?

Paul

Author:  paulv [ Tue Nov 20, 2012 9:57 pm ]
Post subject:  Re: RESOLVED:Weird "Bad Immediate Constant" error when compi

So I've now resolved this. After playing about with it for a week and getting nowhere, I post here and then solve the problem.

It seems there's a bug/issue with the BASIC Assembler as I mentioned. I found a tool called "ExtBAS" which is meant to patch the BASIC module and fix the issue but I've just tried it and it had no effect.

So, after reading a few posts on Google, I had the idea that if I moved the string definitions closer to the pieces of code that was referencing them it may resolve the problem. Sure enough, that's exactly what it did.

Paul

Author:  paulv [ Tue Nov 20, 2012 11:52 pm ]
Post subject:  Re: RESOLVED:Weird "Bad Immediate Constant" error when compi

I wasn't using ExtBas properly. Re-arranging the code to how I want it and using ExtBas properly means that the code now compiles with the strings all at the end of the module rather than interspersed throughout.

Paul

Author:  RichTW [ Wed Nov 21, 2012 8:15 am ]
Post subject:  Re: RESOLVED:Weird "Bad Immediate Constant" error when compi

It's not a bug in BASIC V, but a limitation in the operation of the ADR pseudo-op. All ADR does is assembles to an ADD or a SUB; effectively like this:

Code:
ADR reg, addr:

offset = addr - (P% + 8)
IF offset > 0 THEN
   ADD reg, PC, #offset
ELSE
   SUB reg, PC, #offset
ENDIF

So therefore you're limited by the normal limits for immediate constants in ARM code (must be representable as an 8 bit number optionally rotated right an even number of times). Just as you couldn't write ADD r0, r0, #&1004, you must also ensure that the relative address will fit into an immediate constant without problems.

Edited to add: I don't know how ExtBas works (couldn't find any info from a quick Google), but I presume it allows ADRs to be synthesised using more than one ARM instruction. I would use it with caution, as it will either always generate more instructions than necessary, or it could break things in the case that an address is forward-referenced in the code (due to the address not being known in the first pass, and a different number of instructions generated from the first pass to the second).

Author:  paulv [ Wed Nov 21, 2012 2:29 pm ]
Post subject:  Re: RESOLVED:Weird "Bad Immediate Constant" error when compi

RichTW wrote:
I don't know how ExtBas works (couldn't find any info from a quick Google), but I presume it allows ADRs to be synthesised using more than one ARM instruction.


Reading the documentation for ExtBas, it appears to introduce several new features into the BASIC Assembler that circumvent some of the shortcomings of the BASIC Assembler. It goes a lot further than just fixing my issue but that's out of scope for this conversation.

In this case, according to the documentation ExtBas introduces ADRL, ADRX and ADRW. Where ADRL is fixed at two words, ADRX is fixed at three words and ADRW addresses data relative to a value in R12 and can be one to three words in length.

The new pseudo-operations provide the same conditional features too so they are written as ADR{condition}L etc.

The ADR pseudo-operation remains unaffected and attempting to assemble the source using ADR even when ExtBas successfully patches BASIC fails with the same error.

For my particular use, tweaking the code to use ADRL throughout was sufficient to allow it to assemble without error and up to now during testing of the module, it appears to be working as expected and outputting strings correctly.

I'll continue to test for a few days and keep an eye out for any odd behaviour before I unleash the module on others...

Just for completeness, my problem in getting ExtBas to work in the first place was due to the fact that on installing ExtBas, it attempts the equivalent of *RMFaster BASIC to move BASIC into RAM so it can be patched. Obviously, this can only happen if BASIC isn't in use at the time and it seems when I attempted to load the module initially, BASIC was already in use.

Paul

Author:  tautology [ Mon Dec 03, 2012 1:12 pm ]
Post subject:  Re: RESOLVED:Weird "Bad Immediate Constant" error when compi

There are quite a few bugs in the BASIC assembler (and the aforemention ADR problems cause a lot of issues, this is why I released my old assembler: viewtopic.php?f=19&t=617

Which resolves a lot of the problems. I recently found the documentation for it (on the hard disc that you help me get stuff back from), which I ought to get around to publishing!

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/