On Feb 8, 9:39 pm, "Alexei A. Frounze" <
alexfrun...@gmail.com> wrote:
...
> > > I've traditionally thought you write kernel code in C. Maybe I'm
> > > getting old and sadistic, but I find myself having to work around the
> > > compiler more than the advantage of being able to express complex logic
> > > easily seems to offer.
>
> > I have found similar. Having got used to writing in assembly I find
> > that C - or maybe the compilers I have tried - tend to get in the way
> > of what I have been trying to do.
>
> How so? Examples?!
How long have you got...? :-(
> > Of course, the kernel could be a mix of assembly and C but I find the
> > interfacing between assembly and C to be frustrating. Compilers cannot
> > usually adapt their calling conventions much. This means that the
> > assembly side always has to change or at least conform to include the
> > interfacing code - and that if I change compiler I have to change the
> > assembly code. That shouldn't be the case.
>
> I think this is the wrong approach. If you're using C and assembly, it
> makes sense to do most in C (preferably, in standard C) and in
> assembly only the parts that can't be done in C (e.g. accesses to
> system registers, segment registers, etc etc). If you're writing
> assembly code, it's already custom. Make it a tad more custom, suit it
> to the C compiler, not the other way around.
From a high level I would agree. In practice, though, I'm no longer
sure. If I wrote something in C I wouldn't want to tie myself to a
specific compiler. C should be C, IMHO, not custom C. ISTM I should be
able to change compiler without having to change any assembly code the
C interfaces with. Surely a compiler should be able to adapt its code
to suit the libraries it is using. A compiler is a code generator
after all.
Perhaps another way of looking at this, because of the tie-in to a
particular compiler, is to use assembly to provide an environment for
C to run in - including providing calls for system functions like
special register changes - and then let the C code run as an entity. I
have become a bit disillusioned about the idea of writing a single app
in multiple languages.
> > Of the compilers I have tried Open Watcom seems to allow a lot of
> > customisations of the calling mechanisms. However, it seems to require
> > me to write non-standard C. If I write in C I want it to be completely
> > standard C. Maybe I haven't learned enough about different compilers
> > but I would have thought it best to have standard C source code and
> > have ways to tell the compiler how to interface to different modules.
>
> You're doing something wrong, if you think the compiler is forcing you
> to write non-standard C code. Perhaps, you're trying to do in C
> something that truly belongs to assembly code?
Don't forget this is 16-bit code. Open Watcom has *lots* of non-
standard features to support it. In fairness I could probably omit
most of them but some like the "far" keyword might remain. Also, I
found its name mangling annoying. IIRC some labels have an underscore
appended. Others have an underscore prepended. This is all fine for C
talking to C in the same compiler but I don't like the way the
compiler goes its own way and expects anything interfacing with it to
change.
> > Anyway, the jury is still out. I have been looking at 16-bit C and
> > assembly which makes things worse because of the different pointer
> > modules. 32-bit or 64-bit should be a bit easier.
>
> What pointer modules?
Sorry. I think I meant pointer "modes" - i.e. near and far. I had been
thinking about coding 16-bit programs in modules.
> Are you mixing near and far pointers in your C code?
Yes. That's maybe a nub of the issue. I would want to write the C code
and *not* have the code itself specify pointer types but to just
specify a pointer. IMHO the compiler should generate the correct type
of pointer to suit its use, taking into account any relevant factors.
> Btw, by choosing one of the available memory models you can make your
> C pointers (code and data independently):
> - near (16-bit offsets only)
> - far (16-bit segment + 16-bit offset)
> - huge (32-bit)
> I don't know exactly how huge pointers are implemented (as far or as
> 32-bit physical addresses), but they have the advantage that you can
> add to them indices larger than 64KB using regular pointer arithmetic
> in C. This may alleviate some of your problems.
Thanks. I saw some info about huge pointers - updated by subroutine
call, IIRC, so that they remain valid on carry from the low 16 bits.
> Also, it may be a good idea to keep 16-bit parts of the system as
> small and simple as possible, so you don't need to wrestle with the
> various limitations of the 16-bit underworld.
What I am writing at the mo is a purely 16-bit piece of code. It will
start small, for sure, and may not get very large. However, I wanted
to allow for it to grow in case it needed to. I thought to write the
code (whether C or assembly or a mixture of both) into modules which
each had less than 64k code.
I still want to write it in modular form. Am trying out some ideas
before I commit to one particular way of coding it.
James