Inline ASM and passing variables and pointers

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Inline ASM and passing variables and pointers

Post by Zetr0 »

Hello my fello z88dk'ers

I have a very strange one today and would like a little clarification before I start hacking z88dk output code.

As some might be aware I am currently prototyping a data-driven menu system for the +3e environment and I would like to optimise some of the simpler C code into ASM, I have achieved this with some functions, others I have had to... well mother would not approve.

So my question is this really -

Code: Select all

void asm_WriteByte( void *scr_ptr, unsigned char *byte)
{
    *asmstore = *byte;  // going to need to set this in a safe area

     #asm
         ld a,(50000)    ; should be the *byte value

         ld hl,4         ; 4 bytes on the stack
         add hl,sp       ; skip over the return address (Stack Pointer)

         ld e,(hl)
         inc hl
         ld d,(hl)       ; *scr_ptr

         ex de,hl        ; exchange registers

         ld (hl),a       ; put a byte into the screen file
         ret
     #endasm
}
Sadly as try as I might I couldn't access the second variable (pointer) in the passed variables in the stack frame.

The above works but I have had to move the value of the (char) Byte pointer into memory location 50000 - which to be honest could easily over-written later down the line with running data. I am wondering what fundamental knowledge I am missing to make this work properly.

I do so appreciate any insight or alternate method for doing things like this.



Thanks for reading.
User avatar
dom
Well known member
Posts: 1320
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

Have you taken a look at:

https://github.com/z88dk/z88dk/wiki/CallingConventions

and

https://github.com/z88dk/z88dk/wiki/The-Stack-Frame

For __smallc (or sccz80) functions, byte is at sp + 2, scr_ptr at sp +4. For __stdc (or sdcc) byte is at sp + 4, scr_ptr at sp + 2
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Post by Zetr0 »

@dom

Thank you for such an invaluable post!!

And also for putting up with my esoteric questions =)

I am very pleased to report a fully working set of functions!
Post Reply