including assembly language routines in c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Cloud

    #1

    including assembly language routines in c

    Is it possible to include assembly language routines in
    C if I'm using a compiler which has an assembler such as gcc? could I
    include them in a main function or would I have to write a seperate
    function? what kind of privilege level would be required to execute
    a program with assembly in it? how would I denote that the following
    code is assembly and thus shouldn't be checked for syntax errors?

    ------------------------------
    Robert Cloud

    rcloud@uab.edu
  • Tim Prince

    #2
    Re: including assembly language routines in c

    Robert Cloud wrote:
    Is it possible to include assembly language routines in
    C if I'm using a compiler which has an assembler such as gcc? could I
    include them in a main function or would I have to write a seperate
    function? what kind of privilege level would be required to execute
    a program with assembly in it? how would I denote that the following
    code is assembly and thus shouldn't be checked for syntax errors?
    maybe difficult to find out, if your search facility is broken.


    et al.
    Also, consider things like SSE intrinsics, or equivalent for your targets.
    Then, if you have questions, sign up for gcc-help mailing list.

    Comment

    • jacob navia

      #3
      Re: including assembly language routines in c

      Robert Cloud wrote:
      Is it possible to include assembly language routines in
      C
      Yes. You have just to know exactly the calling conventions of
      your compiler and operating system. Do not forget that C is translated
      directly into assembly in most cases.

      if I'm using a compiler which has an assembler such as gcc?
      You can use another assembler or gas, as you wish. All assemblers
      should generate the same code.
      could I
      include them in a main function or would I have to write a seperate
      function?
      You can call your assembly routines from anywhere. Suppose you have
      just wrote an assembly file with

      int myAdditionFunct ion(int a,int b)

      You just call it with
      int result = myAdditionFunct ion(2,2);

      In some compilers (such as gcc) you can also add
      inline assembler with the asm() pseudo function.
      Read your compiler documentation to know how to do
      this. You will have to learn the gcc inline assembler
      language something that is far beyond my failing
      neurons :-)

      what kind of privilege level would be required to execute
      a program with assembly in it?
      None. Unless you use processor instructions that are privileged.
      If you use those, you will have to write a device driver to
      run your program.
      how would I denote that the following
      code is assembly and thus shouldn't be checked for syntax errors?
      >
      Use the asm() pseudo instruction in the C code. But beware... it is
      not very easy because you will want to avoid destroying the assembler
      code that the compiler generates, so you can't just add instructions
      like that withing a function, unless you save all the registers
      and restore them later before leaving.
      ------------------------------
      Robert Cloud

      rcloud@uab.edu

      --
      jacob navia
      jacob at jacob point remcomp point fr
      logiciels/informatique
      http://www.cs.virginia.edu/~lcc-win32

      Comment

      • Gordon Burditt

        #4
        Re: including assembly language routines in c

        >Is it possible to include assembly language routines in
        >C if I'm using a compiler which has an assembler such as gcc? could I
        You can write an assembly-language code file, often with a name of
        the form *.s or *.asm, so the front end will pass it directly to
        the assembler, and then link it to C code. The details are highly
        system-specific, if it's possible at all.

        Or you could try to use the highly system-specific asm() extension
        of your compiler, if there is one. Note that the answers to questions
        like "How do I get the value of this local variable into a register?"
        and "What register do I return the return value in to have C see
        it?" are highly system-specific.
        >include them in a main function or would I have to write a seperate
        >function?
        Why would the function have to have a specific name? *IF* you can
        add assembly language to a function, you can probably add it to any
        function you have source code for.
        >what kind of privilege level would be required to execute
        >a program with assembly in it?
        Compiled programs (C or otherwise) have assembly in them.

        Probably the same privilege level you need to execute C. The two
        are about equally dangerous. On some machines you can write machine
        code into an array, put the address of it into a function pointer,
        and call it as though it were a function, using only C but unportable
        C. Buffer overflow attacks tend to work similarly to this.
        >how would I denote that the following
        >code is assembly and thus shouldn't be checked for syntax errors?
        Assembly language needs to be checked for syntax errors (just different
        syntax).

        Comment

        • Ian Collins

          #5
          Re: including assembly language routines in c

          jacob navia wrote:

          Have you time travelled?

          From: jacob navia <jacob@nospam.c om>
          Newsgroups: comp.lang.c
          Subject: Re: including assembly language routines in c
          Date: Sun, 20 Jul 2008 22:18:57 +0200

          --
          Ian Collins.

          Comment

          • jacob navia

            #6
            Re: including assembly language routines in c

            Ian Collins wrote:
            jacob navia wrote:
            >
            Have you time travelled?
            >
            From: jacob navia <jacob@nospam.c om>
            Newsgroups: comp.lang.c
            Subject: Re: including assembly language routines in c
            Date: Sun, 20 Jul 2008 22:18:57 +0200
            >
            Changed my motherboard... MMMMM
            I think I just forgot something :-)

            --
            jacob navia
            jacob at jacob point remcomp point fr
            logiciels/informatique
            http://www.cs.virginia.edu/~lcc-win32

            Comment

            • Walter Roberson

              #7
              Re: including assembly language routines in c

              In article <g5ti8i$ps6$2@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
              >Do not forget that C is translated
              >directly into assembly in most cases.
              What is "most cases" ??

              It's been a couple of decades since I used a C compiler that translated
              directly into assembly. My experience since then has been with
              compilers that translate into a language-independant intermediate code
              representation, the results of which were optimized. Assembly was
              generated after that stage for some of the (older) compilers but
              optimized intermediate code directly to object code is what I see these
              days.

              Working with manipulated C as an intermediate form is rather clumsy,
              and by the time you have reached assembly you have lost a lot of
              the opportunities for intra-procedural optimization, so an
              intermediate form is common, especially in companies that make
              multiple compilers, the more so if the compilers can be targetted
              to multiple architectures. For example gcc goes through GENERIC and
              GIMPLE on its way to RTL (Register Transfer Language).
              --
              "He wove a great web of knowledge, linking everything together,
              and sat modestly at a switchboard at the center, eager to help."
              -- Walter Kerr

              Comment

              • CBFalconer

                #8
                Re: including assembly language routines in c

                Robert Cloud wrote:
                >
                Is it possible to include assembly language routines in C if
                I'm using a compiler which has an assembler such as gcc? could
                I include them in a main function or would I have to write a
                seperate function? what kind of privilege level would be
                required to execute a program with assembly in it? how would I
                denote that the following code is assembly and thus shouldn't
                be checked for syntax errors?
                Remember that the C language runs on many different machines, cpus,
                operating systems, etc. However assembly code is automatically cpu
                (and system) specific, and thus is not compatible. Some compilers
                have an 'asm' instruction to make generating cpu/system specific
                code easy, but this is NOT portable. Therefore you should ignore
                anyone who tells you just to use it.

                The appropriate mechanism is to have a separate module, which is to
                be compiled (or assembled) for the actual destination system. This
                isolates the non-portable code, and makes porting to another system
                easier (just write the appropriate module). Some systems include
                such a module written in portable C (and thus probably considerably
                slower) so that the program can be installed anywhere, and then
                optimized.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.


                Comment

                • CBFalconer

                  #9
                  Re: including assembly language routines in c

                  jacob navia wrote:
                  Robert Cloud wrote:
                  >
                  >Is it possible to include assembly language routines in C
                  >
                  Yes. You have just to know exactly the calling conventions of
                  No. The results will not be C, but something cpu/system specific.

                  However, the point of this answer is to correct your clock. Your
                  message was dated 2008-07-20 22:18 +0200 and was received here on
                  2008-07-18 before 0600 -0400. I suspect you have am/pm misset.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.

                  Comment

                  • Keith Thompson

                    #10
                    Re: including assembly language routines in c

                    jacob navia <jacob@nospam.c omwrites:
                    [...]
                    You can call your assembly routines from anywhere. Suppose you have
                    just wrote an assembly file with
                    >
                    int myAdditionFunct ion(int a,int b)
                    >
                    You just call it with
                    int result = myAdditionFunct ion(2,2);
                    "int myAdditionFunct ion(int a,int b)" is assembly language? What
                    assembler are you using?

                    In any case, the ability to call routines written in other languages
                    is very common, but still system-specific.

                    [...]
                    Use the asm() pseudo instruction in the C code.
                    *If* your compiler happens to have that non-standard feature.
                    But beware... it is
                    not very easy because you will want to avoid destroying the assembler
                    code that the compiler generates, so you can't just add instructions
                    like that withing a function, unless you save all the registers
                    and restore them later before leaving.
                    Again, this is entirely system-specific, and it may not even be
                    possible. (Perhaps jacob didn't emphasize that point because he knew
                    the rest of us would do so.)

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    Nokia
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • jacob navia

                      #11
                      Re: including assembly language routines in c

                      Walter Roberson wrote:
                      In article <g5ti8i$ps6$2@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
                      >Do not forget that C is translated
                      >directly into assembly in most cases.
                      >
                      What is "most cases" ??
                      >
                      It's been a couple of decades since I used a C compiler that translated
                      directly into assembly. My experience since then has been with
                      compilers that translate into a language-independant intermediate code
                      representation, the results of which were optimized. Assembly was
                      generated after that stage for some of the (older) compilers but
                      optimized intermediate code directly to object code is what I see these
                      days.
                      >
                      Working with manipulated C as an intermediate form is rather clumsy,
                      and by the time you have reached assembly you have lost a lot of
                      the opportunities for intra-procedural optimization, so an
                      intermediate form is common, especially in companies that make
                      multiple compilers, the more so if the compilers can be targetted
                      to multiple architectures. For example gcc goes through GENERIC and
                      GIMPLE on its way to RTL (Register Transfer Language).
                      Of course, with "directly" I mean that the *result* of the compilation
                      is a stream of opcodes, and not an interpreter, even if C interpreters
                      exist (that is why I said in most cases and not always)

                      --
                      jacob navia
                      jacob at jacob point remcomp point fr
                      logiciels/informatique
                      http://www.cs.virginia.edu/~lcc-win32

                      Comment

                      • Ian Collins

                        #12
                        Re: including assembly language routines in c

                        CBFalconer wrote:
                        >
                        However, the point of this answer is to correct your clock. Your
                        message was dated 2008-07-20 22:18 +0200 and was received here on
                        2008-07-18 before 0600 -0400. I suspect you have am/pm misset.
                        >
                        He did that an hour before you posted, so why bother?

                        --
                        Ian Collins.

                        Comment

                        • Chris Torek

                          #13
                          Re: including assembly language routines in c

                          >Robert Cloud wrote:
                          >Is it possible to include assembly language routines in
                          >C ...
                          >how would I denote that the following
                          >code is assembly and thus shouldn't be checked for syntax errors?
                          In article <g5ti8i$ps6$2@a ioe.orgjacob navia <jacob@nospam.o rg>
                          wrote (in part, and in reply to the second part):
                          >Use the asm() pseudo instruction in the C code. But beware... it is
                          >not very easy because you will want to avoid destroying the assembler
                          >code that the compiler generates, so you can't just add instructions
                          >like that withing a function, unless you save all the registers
                          >and restore them later before leaving.
                          If you use asm() at all, you will depend heavily on the
                          specific compiler.

                          If you choose to depend on GCC, what Jacob says here is not quite
                          correct, because gcc's asm() construct includes communication
                          between the programmer and the compiler as to what machine registers
                          and other features are modified by the inline assembly.

                          For instance, if you are inserting inline assembly on the Glorko
                          GX-47, you might write:

                          asm volatile("frob %1,%2,%0" : "=B"(result ) :
                          "g"(arg1), "I"(arg2) : "L");

                          to tell gcc:

                          - the instruction must not be moved or combined;
                          - the output must be in one of the "B" registers;
                          - arg1 can be in any register or memory ("g"eneral);
                          - arg2 must fit in an "I"ntermedi ate instruction field; and
                          - the "L"aziness register is clobbered by a "frob".

                          The B and L codes are specific to the Glorko machines, of course.
                          The letter B means something else on similar machines, and L is
                          not even valid. So be sure you do not attempt to use this on a
                          Frobozz series, which also has a frob instruction, but does not
                          have a laziness register.

                          (The above is meant as an illustrative example of just what you
                          are getting into when you start inserting inline assembly. Be
                          *very* sure this is a good idea before you attempt it.)
                          --
                          In-Real-Life: Chris Torek, Wind River Systems
                          Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                          email: gmail (figure it out) http://web.torek.net/torek/index.html

                          Comment

                          • Flash Gordon

                            #14
                            Re: including assembly language routines in c

                            jacob navia wrote, On 20/07/08 00:01:
                            Walter Roberson wrote:
                            >In article <g5ti8i$ps6$2@a ioe.org>, jacob navia <jacob@nospam.o rg>
                            >wrote:
                            >>Do not forget that C is translated
                            >>directly into assembly in most cases.
                            >>
                            >What is "most cases" ??
                            <snip>
                            >representation , the results of which were optimized. Assembly was
                            >generated after that stage for some of the (older) compilers but
                            >optimized intermediate code directly to object code is what I see these
                            >days.
                            <snip>
                            Of course, with "directly" I mean that the *result* of the compilation
                            is a stream of opcodes, and not an interpreter, even if C interpreters
                            exist (that is why I said in most cases and not always)
                            Having read and written assembler and also debugged at machine code
                            level (without the benefit of a disassembler) I can say with a fair
                            degree of certainty that assembly is *not* a stream of opcodes, it is a
                            language (or several languages) in text with its own syntax, often
                            macros and definitely things that don't make it in to the machine-code.

                            This may seem like nit-picking, but since the OP wanted to integrate
                            assembly and C it is not. C and assembly are different languages that
                            are *both* normally used to produce object files containing machine-code
                            and data (which is not normally used as opcodes). Assembly language
                            often has a lot more features than machine-code but by its very nature
                            you can in assembly write code that uses the same calling conventions of
                            whatever C compiler you happen to be using.
                            --
                            Flash Gordon

                            Comment

                            Working...