Jump to content
IGNORED

How can an excecutable detect from where it's loaded?


bugbiter

Recommended Posts

I have a menu program which has to call other modules. The menu program shall be loaded manually in a dos environment from disk or a folder mounted in APE. The other programs are in that same location.

So I am looking for a way for the menu program to note which device and drive number is used to load it originally and to use that device to load the other modules later.

Is that possible? Maybe during an init routine while the loading process of the menu is still active and the iocb still open?

Link to comment
Share on other sites

In SpartaDOS-X, D: refers to the default drive\path. For example, if your program was in c:\myprog\myprog.exe, along with myprog.dll, and myprog.ovl, referencing D:MYPROG.DLL or D:MYPROG.OVL would work fine.

 

In most other DOSes, D: refers to D1:. In BASIC, you can peek 769 (DUNIT $301) to find out the last drive used.

 

Hope this helps.

Link to comment
Share on other sites

We went through this at least once before, it's a tricky thing and not a definitive answer to be had.

 

The device in the DCB you can usually check during an INIT segment, practically guaranteed it'll refer to the device your program is currently loading from.

But the problem is an absolute device number/unit number won't always map to the Dn: drive that you think it will.

That can cause a problem if you're using Dos.

 

If you're dealing with absolute device/unit number, doing the IO yourself rather than having a filing system then your disadvantage is just that - without a Dos you're left to dealing with raw sector data.

 

In an application your best bet is probably to do the deduction yourself but allow the user to change the drive details if necessary.

  • Like 1
Link to comment
Share on other sites

Easy in SDX since the command line buffer holds a copy of the command that launched the executable and if you re-parse that argument, DOS will resolve the device identifier or add the default if none was provided. Unfortunately other DOSes don't provide such convenience, and even under SDX, drive swap tables and further indirection via partition tables, etc, if hard disks are used present more obstacles.

Edited by flashjazzcat
Link to comment
Share on other sites

You're probably best off just OPENing the file and performing the binary load operation yourself.

The problem with binary/executable files is it's like they were a bit of an afterthought. No native support at all in the OS. Though some Doses are nice enough to have a special (XIO) command type that performs a binary load, Dos 2.x from Atari doesn't have any such thing. In fact I don't think there's support to do a binary load without having DUP present although AUTORUN.SYS is a binary file and is loaded by Dos without involvement from DUP.

 

Luckily the algorithm to perform a binary load including doing the Init/Run processing is pretty simple and could probably be done in under 150 bytes disregarding the Open file processing part...

 

sort of goes like this (assumes file has already been opened):

 

set $2E0 = <point to RTS>

Main_loop

set $2E2 = <point to RTS>

Get_start

get 2 bytes from file to form Segment_start

If Segment_start = $FFFF then goto Get_start

get 2 bytes from file to form Segment_finish

set Current_byte = Segment_start

Read_segment

get 1 byte from file, store to Current_byte address

if Current_byte = Segment_finish then goto Do_init

increment Current_byte, jump to Read_segment

Do_init

JSR indirect via $2E2

Jump to Main_loop

End_of_File

JSR indirect via $2E2

JMP indirect via $2E0

 

End_of_File gets executed once the file is exhausted. Note that the Init address should be reinitialized to point to RTS before each segment read starts to ensure it's never called more than once. Files can contain unlimited Init addresses and each one will be executed. Only the last Run address will be valid.

Edited by Rybags
  • Like 1
Link to comment
Share on other sites

Thank you guys, I'll try that.

One last question: How can I make Dos load and execute another program via my machine code?

 

use xB:

fname .byte c'MYFILE COM'

ldy <fname
ldx >fname
jsr xBIOS_LOAD_FILE

or

if you have opened file you can:

jsr xBIOS_LOAD_BINARY_FILE

from any point in file

INI/RUN vectors works as expected

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...