<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Wed, 27 Jul 1994 10:04:41 WET DST
From   : Bonfield James <jkb@...>
Subject: Assembly and C

Stephen Quan writes:

>James B writes :
>
>> Is there a document available (preferably online) that describes how to use
>> asm? It always struck me as odd that you can do:
>> 
>> _asm add %o1,len,%o1.
>
>This is SPARC assembly, I was demonstrating the point that it is possible
>to write portable assembly code.  The above has no meaning for a PC.
>I think it is add register %o1 with len and store the result back into
>%o1.  In general, I guess, you can have

I realise this. I once wrote a _tiny_ bit of sparc assembly to try and code
the smallest hello world binary (ok a shell script is shorter, but that
invokes the shell binary) :

#!/bin/sh
cd /tmp
cat << EOF > /tmp/$$.s
        .seg    "text"
        .global _main
ping:
        .ascii  "Hello World\n"
_main:
        mov     1,%o0                   ! 1st arg of write
        sethi   %hi(ping),%o1           ! 2nd arg
        mov     12,%o2                  ! 3rd
        mov     0x4, %g1
        ta      %g0                     ! call write
        mov     0x1, %g1
        ta      %g0                     ! call exit
EOF
as $$.s -o $$.o
ld -s -N -e _main $$.o -o $$out
$$out
rm -f $$.o $$.s
echo "program is /tmp/$$out"

This creates a 72 byte executable (of which 32 bytes are header) ;-) (the last
2 bytes aren't needed anyway.)

Anyway, I deviate from the topic at hand...

>> This uses the first of the output registers (%o1) for the addition. How do we
>> know which register to use!? Isn't that the compilers task and not ours? Is
>> there a standard way of requesting PC to be register %o1? (There is in gcc -
>> just).
>
>I am afraid, it means taking over control and leaving practically nothing
>in C.

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?

>From your examples, I can see no evidence for combining C and assembly being
preferable to programming in assembly alone.

       James
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>