Next: Inlining Inline Assembler Code, Previous: Output Variables in Inline Assembler, Up: Inline Assembler
The example in this section illustrates how to specify the source operands for assembly language statements. The program simply increments its input value by 1:
with Interfaces; use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
with System.Machine_Code; use System.Machine_Code;
procedure Increment is
function Incr (Value : Unsigned_32) return Unsigned_32 is
Result : Unsigned_32;
begin
Asm ("incl %0",
Inputs => Unsigned_32'Asm_Input ("a", Value),
Outputs => Unsigned_32'Asm_Output ("=a", Result));
return Result;
end Incr;
Value : Unsigned_32;
begin
Value := 5;
Put_Line ("Value before is" & Value'Img);
Value := Incr (Value);
Put_Line ("Value after is" & Value'Img);
end Increment;
The Outputs parameter to Asm specifies
that the result will be in the eax register and that it is to be stored
in the Result variable.
The Inputs parameter looks much like the Outputs parameter,
but with an Asm_Input attribute.
The "=" constraint, indicating an output value, is not present.
You can have multiple input variables, in the same way that you can have more than one output variable.
The parameter count (%0, %1) etc, now starts at the first input statement, and continues with the output statements. When both parameters use the same variable, the compiler will treat them as the same %n operand, which is the case here.
Just as the Outputs parameter causes the register to be stored into the
target variable after execution of the assembler statements, so does the
Inputs parameter cause its variable to be loaded into the register
before execution of the assembler statements.
Thus the effect of the Asm invocation is:
Value into eax
incl %eax instruction
Result variable
The resulting assembler file (with -O2 optimization) contains:
_increment__incr.1:
subl $4,%esp
movl 8(%esp),%eax
#APP
incl %eax
#NO_APP
movl %eax,%edx
movl %ecx,(%esp)
addl $4,%esp
ret