how do you write code that is going to be relocated ?

Other misc things
Post Reply
nippur72
New member
Posts: 9
Joined: Sat Sep 29, 2018 4:47 pm

how do you write code that is going to be relocated ?

Post by nippur72 »

I have an assembler routine that I need to move at the end of the memory after the program is being started.

Eg:

Code: Select all

int main() {
}

void routine() __naked {
#asm
#endasm;
}
How do I write it so that I get the correct destination addresses? I tried with ORG but it doesn't work because C programs already have an ORG.

Currently I'm doing the following workaround:

1) compile separately the assembler routine in a .ASM file with the destination ORG
2) turn the resulting bytes into a list of DEFB in another assembler file
3) link this file to the C program
4) relocate it at runtime

is there a better way?
User avatar
dom
Well known member
Posts: 1194
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

Funnily enough I've just been through this to get graphics working on the tiki100, so appmake got updated to join an extra section the end of the binary.

Create an assembler file as follows and add it to your project:

Code: Select all

       SECTION HIMEM
       ORG 45000

routine:
       ret
       SECTION HIMEM_END


       SECTION code_crt_init

       ; Code to move the HIMEM section to the right place in memory
       EXTERN  __BSS_END_tail
       EXTERN  __HIMEM_head
       EXTERN  __HIMEM_END_tail
       ld      hl,__BSS_END_tail
       ld      de,__HIMEM_head
       ld      bc,__HIMEM_END_tail - __HIMEM_head
       ldir
appmake will stitch together a binary with the HIMEM code appended to the file.

There are other ways to do this using PHASE, but I've not got it to work across files, but the following should work if your high memory code is located in a single file:

Code: Select all

       SECTION HIMEM
       PHASE 45000

routine:
       ret

       SECTION HIMEM_END

       SECTION code_crt_init

       ; Code to move the HIMEM section to the right place in memory
       EXTERN  __BSS_END_tail
       EXTERN  __HIMEM_head
       EXTERN  __HIMEM_END_tail
       ld      hl,__BSS_END_tail
       ld      de,_45000
       ld      bc,__HIMEM_END_tail - __HIMEM_head
       ldir
which will create a single binary without the need for appmake
nippur72
New member
Posts: 9
Joined: Sat Sep 29, 2018 4:47 pm

Post by nippur72 »

is there a way to avoid ORG 45000 and have instead ORG <end_of_memory - sizeof(routine)> ?
User avatar
dom
Well known member
Posts: 1194
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

nippur72 wrote:is there a way to avoid ORG 45000 and have instead ORG <end_of_memory - sizeof(routine)> ?
It looks like the following does what you want:

Code: Select all

       SECTION HIMEM
routine:
       ret
end:
        SECTION HIMEM

        org     45000 - (end - routine)

       SECTION HIMEM_END


       SECTION code_crt_init

       ; Code to move the HIMEM section to the right place in memory
       EXTERN  __BSS_END_tail
       EXTERN  __HIMEM_head
       EXTERN  __HIMEM_END_tail
       ld      hl,__BSS_END_tail
       ld      de,__HIMEM_head
       ld      bc,__HIMEM_END_tail - __HIMEM_head
       ldir
Post Reply