88

I remember back in the day with the old borland DOS compiler you could do something like this:

asm { mov ax,ex etc etc...}

Is there a semi-platform independent way to do this now? I have a need to make a BIOS call, so if there was a way to do this without asm code, that would be equally useful to me.

kristianp's user avatar
kristianp
6,01542 silver badges63 bronze badges
askedSep 14, 2008 at 13:55
Nathan's user avatar
1
  • See also the links in theinline-assembly tag wiki for how to get this right. It's hard, and the best suggestion isgcc.gnu.org/wiki/DontUseInlineAsm. For performance, whenever possible tweak your source code to help the compiler make better code, instead of using inline asm. (e.g. by looking at the compiler's asm output to see what it fails to optimize or what it does wrong.)CommentedOct 27, 2017 at 8:49

5 Answers5

95

UsingGCC

__asm__("movl %edx, %eax\n\t"        "addl $2, %eax\n\t");

UsingVC++

__asm {  mov eax, edx  add eax, 2}
answeredSep 14, 2008 at 14:05
Niall's user avatar
Sign up to request clarification or add additional context in comments.

4 Comments

C++Builder, from CodeGear, also supports the __asm keyword.
VS compiler does not support inline assembly ! I had burned my finger trying to do that a few years ago !
@JayD: the VS compiler supports inline assembly depending on the target. For example, 32-bit x86 is supported; 64-bit x86-64 is not.
Yes this is legal gcc syntax, but buggy unless you put that inside a function with__attribute__((naked)) which containsonly inline asm.If you modify registers, youmust use GNU C Extended Asm constraints / clobbers to tell the compiler about it; stepping on the compiler's registers can cause really weird results. (Testing one case with optimization disabled doesn't prove it's safe, either). Seegcc.gnu.org/onlinedocs/gcc/Using-Assembly-Language-with-C.html /stackoverflow.com/tags/inline-assembly/info /ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
22

In GCC, there's more to it than that. In the instruction, you have to tell the compiler what changed, so that its optimizer doesn't screw up. I'm no expert, but sometimes it looks something like this:

    asm ("lock; xaddl %0,%2" : "=r" (result) : "0" (1), "m" (*atom) : "memory");

It's a good idea to write some sample code in C, then ask GCC to produce an assembly listing, then modify that code.

answeredSep 15, 2008 at 0:10
Martin Del Vecchio's user avatar

Comments

13

A good start would be reading this article which talk about inline assembly in C/C++:

http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

Example from the article:

#include <stdio.h>int main() {    /* Add 10 and 20 and store result into register %eax */    __asm__ ( "movl $10, %eax;"                "movl $20, %ebx;"                "addl %ebx, %eax;"    );    /* Subtract 20 from 10 and store result into register %eax */    __asm__ ( "movl $10, %eax;"                    "movl $20, %ebx;"                    "subl %ebx, %eax;"    );    /* Multiply 10 and 20 and store result into register %eax */    __asm__ ( "movl $10, %eax;"                    "movl $20, %ebx;"                    "imull %ebx, %eax;"    );    return 0 ;}
answeredSep 14, 2008 at 13:57
Espo's user avatar

2 Comments

Those are all unsafe: you're modifying EAX and EBX without telling the compiler about it. Never use GNU C Basic Asm inside a function (except as the body of an__attribute__((naked)) function.) Only ever GNU C Extended asm likeasm("sub %1, %0" : "+r"(dst) : "ri"(20)); for example.gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html andstackoverflow.com/tags/inline-assembly/info The article you linked does go on to discuss Extended Asm, but is missing clobbers on some temporary registers in its larger example ofasm volatile.
The article also follows the anti-pattern of starting the asm template with amov %1, %%eax instead of asking for the input in a register in the first place.
3

For Microsoft compilers,inline assembly is supported only for x86. For other targets you have to define the whole function in a separate assembly source file, pass it to an assembler and link the resulting object module.

You're highly unlikely to be able to call into the BIOS under a protected-mode operating system and should use whatever facilities are available on that system. Even if you're in kernel mode it's probably unsafe - the BIOS may not be correctly synchronized with respect to OS state if you do so.

Spike0xff's user avatar
Spike0xff
1,2471 gold badge16 silver badges25 bronze badges
answeredSep 15, 2008 at 14:55
Mike Dimmick's user avatar

2 Comments

Mike, what do you mean by "Non-x86 Microsoft compilers do not support in-line assembly", of course you can have e.g. ARM (non-x86) inline assembly, just search for it.
"Inline assembly is not supported on the ARM and x64 processors." -learn.microsoft.com/en-gb/cpp/assembler/inline/inline-assembler .Inline assembler means use of__asm blocks, and I'm talking specifically about Microsoft's compilers. You can still create a separate .asm file, assemble that with the Microsoft assembler (ML/ML64/armasm) and link the resulting .obj with a C/C++ program using CL. The other answers indicate that you can do inline assembly with other compilers.
1

use ofasm or__asm__ function ( in compilers have difference )

also you can write fortran codes withfortran function

asm("syscall");fortran("Print *,"J");
answeredMar 28, 2021 at 15:55

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.