Intel® ISA Extensions
Use hardware-based isolation and memory encryption to provide more code protection in your solutions.
1093 Discussions

Intel C++ Compiler (Linux x86_64) - "GNU-style Inline Assembly" Resources.

srimks
New Contributor II
1,522 Views
Hello,

Could anyone suggest some resources freely available or Intel documents/resources to - understand, interpret, analyze and write "GNU-style Inline Assembly" on Linux x86_64 using Intel C++ Compiler (v-11.0).

Looking for a flow having "basics-to-advance" level with some examples.

Also, some standard Assembly books which can make life easy after reading it.


~BR
0 Kudos
1 Solution
MarkC_Intel
Moderator
1,522 Views
Newlines and backslashes reqd. Or use separate strings as the howto page suggests.

int main(){
__asm__("
pushq %rbpn
movq %rsp, %rbpn
subq $16, %rspn
movl $3, -16(%rbp)n
movl $5, -12(%rbp)n
movl -12(%rbp), %eaxn
addl -16(%rbp), %eaxn
movl %eax, -8(%rbp)n
xorl %eax, %eaxn
leaven
retn
");
}

View solution in original post

0 Kudos
9 Replies
MarkC_Intel
Moderator
1,522 Views


Could anyone suggest some resources freely available or Intel documents/resources to - understand, interpret, analyze and write "GNU-style Inline Assembly" on Linux x86_64 using Intel C++ Compiler (v-11.0).



I use this as my reference for GNU inline assembly:

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

It is not specific to the Intel compiler.

However, I try to use intrinsics (to avoid inline asm) whenever possible since the same intrinsics are supported by more compilers (GNU, Microsoft, Intel).
0 Kudos
srimks
New Contributor II
1,522 Views


Could anyone suggest some resources freely available or Intel documents/resources to - understand, interpret, analyze and write "GNU-style Inline Assembly" on Linux x86_64 using Intel C++ Compiler (v-11.0).



I use this as my reference for GNU inline assembly:

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

It is not specific to the Intel compiler.

However, I try to use intrinsics (to avoid inline asm) whenever possible since the same intrinsics are supported by more compilers (GNU, Microsoft, Intel).
Hello Mark,

I have a sinerio both with Intel syntax & GNU-style Inline Assembly syntax using ICC-v11.0 on Linux x86_64. I have a cc code as below -

----hello.cc----
#include

using namespace std;

int main() {
int i, j, k;
i = 3, j = 5;

k = i + j;

return 0;
}
--

whose handwritten asm as below using Intel syntax compiles properly, the asm in Intel syntax is -
---
#include

int main(){
__asm {
push rbp
mov rbp, rsp
sub rsp, 16
mov [rbp-16], 3
mov [rbp-12], 5
mov eax, [rbp-12]
add eax, [rbp-16]
mov [rbp-8], eax
xor eax, eax
leave
ret
}
}
---

but above Intel syntax asm code does has SEG faults on execution which is not of concern currently as it compiles properly. But the same hello.cc code assembly if written in GNU-style Inline syntax as below -

---
#include

int main(){
__asm {
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $3, -16(%rbp)
movl $5, -12(%rbp)
movl -12(%rbp), %eax
addl -16(%rbp), %eax
movl %eax, -8(%rbp)
xorl %eax, %eax
leave
ret
}
}
--

The error generated for GNU-syle Inline syntax by ICC-v11.0 is as -
--
[test]$ icpc -g -fno-builtin -fasm-blocks hello-gcc.cc
hello-gcc.cc(7): error: label "$16" was referenced but not defined
subq $16, %rsp
^

hello-gcc.cc(8): error: label "$3" was referenced but not defined
movl $3, -16(%rbp)
^

hello-gcc.cc(9): error: label "$5" was referenced but not defined
movl $5, -12(%rbp)
^

compilation aborted for hello-gcc.cc (code 2)
--

Queries:

(a) AFAK, ICC assembler does support both Intel Assembly syntax (NASM) and GNU-style Inline (AT&T), therefore ICC assembler should identify both.

Please refer "Intel C++ Compiler User & Reference Guides" (304968-022US) Pg#1026, 1280, 1282 & 1283 which suggest ICC supporting GNU-style Inline Asm syntax too.

(b) How do I correct above errors as obtained while testing using same ICC-v11.0 with GNU-style Inline Asm code as above which I don't get when hello.cc is written in Intel asm syntax?

(c) When generating .S (icpc -g -fno-builtin hello.c -S) when compiled with ICC-v11.0, the .S file represntation is in GNU-style Inlne syntax but not in Intel syntax, could you clarify this behaviour?

I think, since the code (hello.cc) is small, possibly it could be replicated at your end to answer above (a) - (c).

~BR
0 Kudos
MarkC_Intel
Moderator
1,522 Views
Hi, to use the ATT SYSV syntax with icc/icpc, you must use the "asm(...)" or "asm volatile(" ... " ) syntax that GCC uses (as described in the link I provided above). AFAIK, you cannot use ATT SYSV syntax in the __asm { ... } statement which is used for the Intel-format assembly.

The .S files are in ATT SYSV format as you observed. I am guessing, but I believe that is the only format.
0 Kudos
TimP
Honored Contributor III
1,522 Views
Your gcc asm compiles fine here, at least after being adjusted into a reasonable format. I don't have -use-masm in my icpc.cfg; I don't know whether it can be overridden if it has been set as a local default.
I don't know what you mean by "ICC assembler." I haven't seen any assembler provided with icpc for i386 or x86_64. icpc -S generates code which should work with gnu as, so you would expect the default output to contain gcc att syntax.
0 Kudos
srimks
New Contributor II
1,522 Views
Quoting - tim18
Your gcc asm compiles fine here, at least after being adjusted into a reasonable format.
I don't know what you mean by "ICC assembler." I haven't seen any assembler provided with icpc for i386 or x86_64. icpc -S generates code which should work with gnu as, so you would expect the default output to contain gcc inline syntax.

No, the asm written in Intel syntax works well. I am not saying ICC assembler as I am still in doubt how to define when above .S which has been written in Intel syntax and compiles well when done using "icc -fno-builtin - fasm-blocks hello-intel.cc" but "hello-gcc.cc" which has been written using "GNU-style Inline asm syntax" fails to compile and generates an error.

Above code is very small which can be tried I guess.

I am really struggling to compile and execute atleast any sample small program(c/cc) having Intel Inline asm syntax with ICC-v11.0.

Could you generate above sinerio and suggest me something.

~BR
0 Kudos
srimks
New Contributor II
1,522 Views
Hi, to use the ATT SYSV syntax with icc/icpc, you must use the "asm(...)" or "asm volatile(" ... " ) syntax that GCC uses (as described in the link I provided above). AFAIK, you cannot use ATT SYSV syntax in the __asm { ... } statement which is used for the Intel-format assembly.

The .S files are in ATT SYSV format as you observed. I am guessing, but I believe that is the only format.

As qouted by u "The .S files are in ATT SYSV format as you observed. I am guessing, but I believe that is the only format"as this .S files in ATT syntax is being generated by ICC-v11.0. doesICC-v11.0 generates ATT syntax as default or one can obtained Intel syntax too as .S output.

~BR
0 Kudos
srimks
New Contributor II
1,522 Views
Hi, to use the ATT SYSV syntax with icc/icpc, you must use the "asm(...)" or "asm volatile(" ... " ) syntax that GCC uses (as described in the link I provided above). AFAIK, you cannot use ATT SYSV syntax in the __asm { ... } statement which is used for the Intel-format assembly.

The .S files are in ATT SYSV format as you observed. I am guessing, but I believe that is the only format.
Mark,

I modified above source asm file as GNU-style Inline assembly syntax as below -

--
#include

int main(){
__asm__("
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $3, -16(%rbp)
movl $5, -12(%rbp)
movl -12(%rbp), %eax
addl -16(%rbp), %eax
movl %eax, -8(%rbp)
xorl %eax, %eax
leave
ret
");
}
---

I get below errors as below -
---
$ icpc -fno-builtin -fasm-blocks hello-gcc-1.cc

hello-gcc.cc(4): error: missing closing quote
__asm__("
^

hello-gcc.cc(5): error: expected a ")"
pushq %rbp
^

hello-gcc.cc(16): error: missing closing quote
");
^

hello-gcc.cc(17): warning #12: parsing restarts here after previous syntax error
}
^

hello-gcc.cc(17): error: expected a ";"
}
^

compilation aborted for hello-gcc-1.cc (code 2)
---

Any clue please.

~BR
0 Kudos
MarkC_Intel
Moderator
1,523 Views
Newlines and backslashes reqd. Or use separate strings as the howto page suggests.

int main(){
__asm__("
pushq %rbpn
movq %rsp, %rbpn
subq $16, %rspn
movl $3, -16(%rbp)n
movl $5, -12(%rbp)n
movl -12(%rbp), %eaxn
addl -16(%rbp), %eaxn
movl %eax, -8(%rbp)n
xorl %eax, %eaxn
leaven
retn
");
}
0 Kudos
srimks
New Contributor II
1,522 Views
Newlines and backslashes reqd. Or use separate strings as the howto page suggests.

int main(){
__asm__("
pushq %rbpn
movq %rsp, %rbpn
subq $16, %rspn
movl $3, -16(%rbp)n
movl $5, -12(%rbp)n
movl -12(%rbp), %eaxn
addl -16(%rbp), %eaxn
movl %eax, -8(%rbp)n
xorl %eax, %eaxn
leaven
retn
");
}
Thanks Mark, it worked.

~BR
0 Kudos
Reply