Change execution address of binary file

By BartH

Supporter (5)

BartH's picture

19-09-2021, 11:36

Hi,

Hopefully I didn't overlook a similar topic on this forum: I have a binary file, which I'd like to run with the 'bload"'",r' command. The problem is that its execution address is set to &H8800, while it should be set to &HC800. If I load the file using 'bload""' and then save it with 'bsave"",&H8800,&HC819,&HC800' then it doesn't work properly. What would be the easiest way to change the execution address?

Login or register to post comments

By Sandy Brand

Master (243)

Sandy Brand's picture

19-09-2021, 12:02

Well, it completely depends on the content of what is in the binary.

Theoretically, what you are trying to do could be possible, if the code inside only uses relative jump Z80 opcodes and does not make all sorts of other assumptions about memory layout and where to store its variables.

In reality though, that is virtually never the case. It is actually quite hard to write working code that can be move around in memory like that.

So your only option is to somehow obtain the original source code and try to recompile it with a different starting address.

By BartH

Supporter (5)

BartH's picture

19-09-2021, 13:06

Thanks for your response, Sandy!

I guess my question wasn't clear enough. The binary currently loads at the same addresses (&H8800 - &HC819), but it requires a basic file to load it and then execute the commands 'defusr=&HC800:a=usr(0)'. Maybe my assumption is wrong, but I thought that if I could set the execution address of the binary file to &HC800, then the basic file could be replaced by executing 'bload"filename",r'.

By Metalion

Paragon (1442)

Metalion's picture

19-09-2021, 13:13

Your assumption is indeed correct.

By Sandy Brand

Master (243)

Sandy Brand's picture

19-09-2021, 13:29

Ah, sorry for the misunderstanding Smile

Then yes, what you are doing with the bsave command should work perfectly fine?

Some speculation of what could be going wrong:

  • Are you sure you can safely use memory up to &HC819?
  • Pure speculation from my side: I am not sure what kind of guarantee MSX Basic gives in terms of context when calling into assembly code. Strictly speaking, calling if from Basic using defusr means the assembly is called with a fully initialized (and running) Basic interpreter. Whereas using bload could just mean: load it in memory and immediately start running it, and only once we get back to the Basic interpreter will we do some cleanup?

Maybe first check that your .BIN header is actually correct? The first 7 bytes of the file should look like what is documented here: MSX Basic Binary Files

By Grauw

Ascended (10055)

Grauw's picture

19-09-2021, 13:40

Although it would be good to know why the BSAVE command doesn’t work properly for you, you can also hex edit the execution address into the BIN file.

By BartH

Supporter (5)

BartH's picture

19-09-2021, 21:34

The bsave command turned out to work as expected after all. My problems seems to be caused by Disk Manager. In order to save files from the BlueMsx emulator, I have to write to a .dsk image, as the Windows file system is not supported for writing. But apparently files can become corrupted either in the .dsk image after importing them with Disk Manager or when extracting them to the Windows file system ('bad file mode' error when loading with bload). But I guess that is a completely new topic.
Thanks for your help, everyone!

By BartH

Supporter (5)

BartH's picture

19-09-2021, 21:36

Grauw wrote:

Although it would be good to know why the BSAVE command doesn’t work properly for you, you can also hex edit the execution address into the BIN file.

Is it possible to do this directly in the Windows file system or should it be done from a real MSX computer or an emulator?

By Grauw

Ascended (10055)

Grauw's picture

19-09-2021, 21:38

You can use any hex editor on any modern operating system or on MSX itself.

If you follow Sandy Brand’s link you can see the execution address is encoded as a little-endian word in byte 5-6 of the BIN file.

By BartH

Supporter (5)

BartH's picture

20-09-2021, 08:24

Grauw wrote:

You can use any hex editor on any modern operating system or on MSX itself.

Yes, I guess that question was a bit silly. Smile

Grauw wrote:

If you follow Sandy Brand’s link you can see the execution address is encoded as a little-endian word in byte 5-6 of the BIN file.

Will do. Thanks again for your time!