The second example shows a macro that implements a simple maximum function and a sample driver program.
// int maximum (int a, int b) //
// Action: return max(a,b) //
#define maximum(a,b)
__asm("bis %0,%0,v0;" // assume result:a
"cmplt v0,%1,t0;" // a < b ?
"cmovne t0,%1,v0",a,b) // yes - result:b
void main()
{
int i = 1,j = 2;
printf("max(i,j) = %d\n", maximum(i,j));
}
The code generated for main
in this example looks like the following:
0000 main::
47E03410 0000 mov 1,a0 // load i
46003400 0004 bis a0,1,v0 // asm body
400059A1 0008 cmplt v0,2,t0 // asm body
23DEFFF0 000C lda sp,-16(sp)
442054C0 0010 cmovne t0,2,v0 // asm body
261F0000 0014 ldah a0,h^.data(zero) // printf: “”
B75E0000 0018 stq ra,(sp)
47E00411 001C mov v0,a1 // printf: %d
22100000 0020 lda a0,l^.data(a0) // printf: “”
D3400000 0024 bsr ra,j^printf // printf()
A75E0000 0028 ldq ra,(sp)
47FF0400 002C clr v0
23DE0010 0030 lda sp,16(sp)
6BFA8001 0034 ret ra
This example illustrates several additional points about _ _asm statements:
a
and b
. These arguments are referred to in the assembly-language instructions as %0
and %1
. The same two arguments also happen to be the arguments to the maximum
macro itself, although such agreement in argument lists is not required.j
(2) directly into the compare less-than (cmplt) and conditional move not-equal (cmovne) instructions specified in the body of the _ _asm statement.