Enterprise Forever

:UK => Programming => Topic started by: gflorez on 2014.April.16. 12:03:14

Title: Where am I?
Post by: gflorez on 2014.April.16. 12:03:14
How can I make a Z80 relocatable code aware of its position in memory?

In the "Machine Code for Beginners" program header there is an error:

1 ALLOCATE 40
2 LET B$="F7,18.............
3 LET C$="F7,01.............
4 LET D$="00,CA.............
5 CODE MC=HEX$(B$&C$&D$)&"MACHINEC"
6 CALL USR(MC,0)

Then, in memory, the code is allocated on different position depending of the configuration of the EP:



exos 24          !Allocate segment
ld A,C
ld (0xBFF8), A
out (0xB2),A
ld A,0x6A        !channel 106 (file io)
ld DE,0x12EA   !            <----------------Here the programmer assumes the allocated area is immovable.
exos 1          !open channel
ld A,0x6A
ld BC,0x18B2
ld DE,0x8151
exos 6            !read block
jp z,0x87B5
di
hang: jp hang

defstring:8,"MACHINEC"  ! here points the buggy instruction

Is easy to search the string in the Basic program and then modify the code before the USR call, but I have though if I know the value of PC inside the code I only need to add the direction of the string. Then the basic header would work on all the configurations of the EP...
Title: Re: Where am I?
Post by: Zozosoft on 2014.April.16. 12:21:02
The second parameter of USR placed to HL.
CALL USR(MC,MC) then you got the address of your routine and it can be used for address calculations.
Title: Re: Where am I?
Post by: gflorez on 2014.April.16. 16:30:37
Thanks Zozo!


This is the debugged code:


Start#:      ld BC, 0x0024   !filename#-start#.  HL must contain the entry point
                add HL,BC        
                Exos 24          !Allocate segment
                ld A,C
                ld (0xBFF8), A
                out (0xB2),A
                ld A,0x6A        !channel 106 (file io)
                ld D,H            !<----------------Here we put the correct address in DE.
                ld E,L            !<----------------/
                Exos 1          !open channel
                ld A,0x6A        !channel 106 (file io)
                ld BC,0x18B2
                ld DE,0x8151
                Exos 6            !read block
                jp z,0x87B5
                di
hang#:       jp hang#
filename#:  8,"MACHINEC"


And the corrected header and program:
Title: Re: Where am I?
Post by: gflorez on 2014.April.16. 16:37:14
Another equally transcendental question:

How do I save a Basic program in ASCII? I did it lots of times on some distant past, but don't remember how, and the manual doesn't help very much mentioning the LIST command.
Title: Re: Where am I?
Post by: Zozosoft on 2014.April.16. 16:39:38
Quote from: gflorez
How do I save a Basic program in ASCII?
OPEN #1:"LIST.TXT" ACCESS OUTPUT
LIST #1
CLOSE #1
Title: Re: Where am I?
Post by: gflorez on 2014.April.16. 17:00:39
Thanks again Zozo.