Date : Thu, 28 Jul 1994 09:12:16 EST
From : Stephen Quan <quan@...>
Subject: Re: Assembly and C
James B writes :
> So in other words there is no portable method of combining C and assembly,
> even on systems of the same architecture!? I've quite often seen C programs do
> a little bit of _asm. The linux kernel for one...
>
> I think you must have missed my point. Consider the linux kernel case. It's
> 99% C, with a few tiny bits of assembly. How can the assembly make use of
> registers without knowing the internal register allocation used by the C
> compiler? If we just say 'I will use %o1 as my PC always' then how do you tell
> the C compiler not to use %o1 too?
A C variable sometimes seems to have exclusive access to a register when
you make use of the 'register' keyword, but which register you actually
get is under the compiler's control. Without looking at the assembly
code, there would be no way to know which register you got.
Also, I think the compiler is careful in distributing it's registers,
and although it has AX,BX,CX,DX as general purpose registers on a 80x86,
you rarely seem to get access to all of them, as the compiler has
reserved, (at least) AX for the return code of a C function.
One of my early attempts at producing this emulator, I had something like :
register unsigned char A;
register unsigned char X;
register unsigned char Y;
register unsigned short PC;
register unsigned char SR;
But I found that the compiler refused to allocate all a register,
eventhough it had enough for them. For some of my variables it
still used a variable from the stack. When I had
register unsigned char *PC;
The most logical assignment for PC would be an index register
such as DS:SI or ES:DI, but again the compiler refuses to give up
these registers. In order to force it, would be to take control
of all the registers by going assembly.
> From your examples, I can see no evidence for combining C and assembly being
> preferable to programming in assembly alone.
Yes, I guess not. My examples only goes as far as using the C compiler
as an assembly compiler, much like how you can write assembly in beeb
BASIC. I guess that's where the analogy lies. Normally, you write
complete functions (eg. PHP:PHA .... PLA:PLP:RTS) or complete programs
in beeb assembly but can call them from BASIC. So I guess the imbedded
assembly in C is usually complete functions or complete programs. It
just happens that in this case that the entire emulator is one function.
--
Stephen Quan (quan@... ), SysAdmin, Analyst/Programmer.
Centre for Spatial Information Studies, University of Tasmania, Hobart.
GPO BOX 252C, Australia, 7001. Local Tel: (002) 202898 Fax: (002) 240282
International Callers use +6102 instead of (002).