DEV Community

Joyce Wei
Joyce Wei

Posted on

SPO600 - Inline Assembler

Inline Assembly is the assembly language code which is being embedded into the program written in another programming language. The most commonly programming language is C.

To embedded assembly language code into a C program, we can use one of the following forms: asm(...); or __asm__ (...);.

We can use asm("..." "..."); noticed there is a space between or asm("...\n ...") to indicate the line break of the assembly code, and use 'asm("\t")' to indicate a tab.

Input and Output Operands

  • = - output-only register: discarded the previous content and replaced with output value
int x=10, y;
__asm__ ("mov %1,%0"  // "mov [destination], [source]"

   : "=r"(y)    // output register value is moved to y
                // register is called %0 in template

   : "r"(x)     // input value from x is placed in a register
                // register is called %1 in template
   :
);
Enter fullscreen mode Exit fullscreen mode
  • + - input and output register: the register can input the value to the assembly code and output the value from the assembly code.
int x=10, y;
__asm__ ("mov %1,%0"
   : "+r"(y)       // + indicates read/write register
   : "0"(x)        // output register is same as %0
   :
);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)