Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Inline Assembler and Position Independent Code

Deleted_U_Intel
Employee
877 Views

I'm trying to use the Intel inline assembler in C++ code in a project in Mac OS/X. I try to convert the example code from

http://www.zathras.de/angelweb/blog-intel-assembler-on-mac-os-x.htm

AT-T syle code to Microsoft style.

I cannot find how to do it with this code

leal myHelloWorld-myAnchorPoint(%ebx), %eax

When I try:

leaeax, myHelloWorld-myAnchorPoint[ebx]

the compiler complains about the- operator. Could you please provide me an hint.

Thanks

GB

0 Kudos
7 Replies
Lars_Petter_E_1
Beginner
877 Views

Hi!

Have you tried the "-use-msasm" compiler switch?

Best Regards

Lars Petter

0 Kudos
Brandon_H_Intel
Employee
877 Views
My recommendation is to just split it out -

     lea ecx, myAnchorPoint
lea eax, myHelloWorld
add ecx, ebx
sub eax, ecx

and that will get you around your problem.
It is an illegal form of lea to use it in this way, I'm actually a little surprised the GNU or Mac assembler accepts this.

I'm also wondering why you're trying this code in inline assembly at all. It doesn't seem to be applicable to inline asm to me.
0 Kudos
levicki
Valued Contributor I
877 Views

Brandon, what you are suggesting is not position independent code anymore. In other words, it needs rebasing (relocation) in order to work. What he is asking is why Intel inline assembler doesn't perform simple mathematical operation (subtraction) and use that value as a displacement for the lea instruction.

	lea	eax, label2-label1[ebx]

If distance between label1 and label2 is 100 bytes then you would get:

	lea	eax, 100[ebx] ; syntax 1
	lea	eax, [ebx + 100] ; syntax 2

I am also confused as to why that isn't possible to do because it is really usefull for generating postition independent code (the code which can be loaded anywhere in memory without relocation). It is actually not a limitation of an instruction but the limitation of the preprocessor/assembler itself.

0 Kudos
jimdempseyatthecove
Honored Contributor III
877 Views

Igor,

Does Intel C++ inline assembler support parenthesis evaluations of constant expressions?

lea eax, (label2-label1)[ebx]

or

lea eax, [ebx + (label2-label1)]

If label1 and label2 are in the same module then the ASSEMBLER should be able to fix-up the address offset

If label1 and/or label2 are in the seperate modules then the LINKER should be able to fix-up the address offset.

Not sure if the user's Linker will do this.

Jim Dempsey

0 Kudos
Zia_A_Intel
Employee
877 Views

I'd have to look into it more, but I think this is just a limitation of our MS-style inline asm parser.

I'm guessing that if you used gnu-style asm (as it more natural on MAC & Linux), you shouldn't have any problems since these get dumped straight to the assembler as seen.

I would highly recommend, however, to leave it to the compiler to figure out all the PIC calculations for you, instead of trying to do them yourself. All it takes is a simple "locally_declared_var = global_var", and you won't have to mess with the PIC stuff.

Zia.

0 Kudos
levicki
Valued Contributor I
877 Views

Zia, could you give an example please? I am not sure I understand what are you talking about.

0 Kudos
Zia_A_Intel
Employee
877 Views

Sure.

For my comment on leaving it to the compiler to do the PIC, I meant doing something like this (if applicable):

int my_glob = 3;

main()

{

int my_local = my_glob;

__asm {

mov eax, my_local

.........

}

}

The glob -> local assignment will do all the necessary PIC to get my_glob, and you work on my_local as much as you want w/o worrying about PIC. Assignments back to my_glob will work the same way. Again, if this is applicable/efficient for the way you want to use PIC.

For gnu-asm, I simply meant something like this:

__asm("call LLL LLL: popl %redx movl _my_glob-LLL(%edx), %eax .......etc..");

(or any other combination of gnu-asm statements to move in/out values.

Hope this helps.

Zia.

0 Kudos
Reply