New to this - slightly confused about the C stubs

Other misc things
Post Reply
mich181189
New member
Posts: 5
Joined: Sat Sep 22, 2018 3:38 pm

New to this - slightly confused about the C stubs

Post by mich181189 »

I've been looking around the source tree and I noticed quite a few files like this: https://github.com/z88dk/z88dk/blob/mas ... m_bdos.asm

I understand the ones that adapt calling conventions and stuff, but this one seems to be entirely pointless since it pops the stack then immediately pushes it all back on again before calling the underlying function

What am I missing?
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

You will actually see they come in pairs. So in addition to that one there is a callee entry point:

standard linkage:
https://github.com/z88dk/z88dk/blob/mas ... m_bdos.asm

callee linkage:
https://github.com/z88dk/z88dk/blob/mas ... callee.asm

It is the callee linkage that will almost always be used and this is guaranteed by the header file. You can see there is a #define there that changes the program to use callee whenever that substitution is possible:

https://github.com/z88dk/z88dk/blob/mas ... /cpm.h#L67


The standard linkage stub that you are looking at is used by function pointers. Function pointers must use standard linkage which means the caller is responsible for both pushing the parameters on the stack *and* cleaning it up. This means the stub must not alter the stack frame so that's why you see the params gathered and then put back.


The cpm library in newlib isn't really fleshed out with regards to bdos and file io. If you see a better way, feel free to contribute :)
mich181189
New member
Posts: 5
Joined: Sat Sep 22, 2018 3:38 pm

Post by mich181189 »

Ahh of course - some how I forgot the registers would still contain the values after they've been put back! I understood the idea of leaving them on the stack but didn't connect the fact they'd also now be in the registers!

I just chose that file as an example - though I do have an RC2014 running CPM so it's possible I might do something with that at some point!

Thanks!
Post Reply