intergrating asm in c++

C

Cristian Tota

If I am using inline asm in functions, like this:

void func(){
__asm{
// asm code
};
};

is it guaranteed that this does not mess up the registries? (As opposed to
using inline asm in middle of C++ code.)

Cristian
 
M

Mike Wahler

Cristian Tota said:
If I am using inline asm in functions, like this:

void func(){
__asm{
// asm code
};
};

is it guaranteed that this does not mess up the registries? (As opposed to
using inline asm in middle of C++ code.)

C++ has no notion of 'registries', so your query
doesn't make much sense.

-Mike
 
L

lilburne

I'd be wary about using asm in any code, are you absolutely
sure its needed.
C++ has no notion of 'registries', so your query
doesn't make much sense.

The OP is talking about registers, not registries (not all
posters from .ro have English as a first language).
 
C

Cristian Tota

Sorry, I ment 'registers' (my first language is Romanian).

<<I'd be wary about using asm in any code, are you absolutely sure its
needed.>>

Could there be problems even if the asm code is isolated in functions?

I have to do a lot of bit operations, like decoding a 32 bit variable into
values represented by groups of bits, (for example the value of
bits[12:15]), swapping the bytes, etc. These would be done very nicely using
the asm instructions.

Cristian
 
M

Moonlit

Hi,

Check your compiler docs. I believe you can change most registers. However
(for x86 based machines) sp should probably be the same at the end of your
routine since it is used for building the stack frame. From the top of my
head I also believe you have to restore si and di but I am not completely
sure about that.

Regards, Ron AF Greve
 
M

Mike Wahler

Cristian Tota said:
Sorry, I ment 'registers' (my first language is Romanian).

Then yes you will need to ensure that your usage of registers
does not interfere with that of your compiler. Your documentation
should have information about this.

-Mike
 
L

lilburne

Cristian said:
Sorry, I ment 'registers' (my first language is Romanian).

<<I'd be wary about using asm in any code, are you absolutely sure its
needed.>>

Could there be problems even if the asm code is isolated in functions?

I have to do a lot of bit operations, like decoding a 32 bit variable into
values represented by groups of bits, (for example the value of
bits[12:15]), swapping the bytes, etc. These would be done very nicely using
the asm instructions.

They can also be done portably and just as easily with
expressions that use the operators >>, <<, |, &, and ~. The
compiler usually creates pretty efficient code for these
sort of expressions too. I check that first before resorting
to asm.
 
W

wogston

I have to do a lot of bit operations, like decoding a 32 bit variable into
values represented by groups of bits, (for example the value of
bits[12:15]), swapping the bytes, etc. These would be done very nicely using
the asm instructions.

Be sure to write longer snips or assembly than 1-10 lines, and write it
well, otherwise it might have negative effect on the performance. Visual C++
6 doesn't optimize "over" assembly blocks, so your "optimization" might make
your code run slower, not faster. Be sure you are doing the right thing
whipping assembly mnemonics out in the open.

For your example, check what compiler does first for:

int v = (a >> 11) & 0x0f;

... or whatever it is that you wanted to do in your example! Can you do it
faster in assembly, and more importantly, can you fuse it together with rest
of the generated code as tirelessly and correctly as machine can? That kind
of boring details are what the machines are best at, human mind is wasted on
trivial details like that, concentrate on the big picture first, that's
where the God's Creation is at it's best. You have a powerful, abstractly
thinking mind: the computer doesn't, it is your slave, use it!

Also look into realtime code generators and JIT-like compiler technology.
Those are the near-future for high-performance low-level bitfucking software
engineering. Shaders in GPU's and shader compilers by nVidia and Microsoft
show the way the technologies like JIT from Sun have made popular in the
past decade or so.

Human operators writing assembly is backwards development. Just my opinion,
not the ultimate truth. :)
 
G

Giuseppe Scrivano

Cristian be sure that at the end you restore the value of the registers that
you use in your asm code. You can change the value of registers with your
asm code.


wogston said:
I have to do a lot of bit operations, like decoding a 32 bit variable into
values represented by groups of bits, (for example the value of
bits[12:15]), swapping the bytes, etc. These would be done very nicely using
the asm instructions.

Be sure to write longer snips or assembly than 1-10 lines, and write it
well, otherwise it might have negative effect on the performance. Visual C++
6 doesn't optimize "over" assembly blocks, so your "optimization" might make
your code run slower, not faster. Be sure you are doing the right thing
whipping assembly mnemonics out in the open.

For your example, check what compiler does first for:

int v = (a >> 11) & 0x0f;

.. or whatever it is that you wanted to do in your example! Can you do it
faster in assembly, and more importantly, can you fuse it together with rest
of the generated code as tirelessly and correctly as machine can? That kind
of boring details are what the machines are best at, human mind is wasted on
trivial details like that, concentrate on the big picture first, that's
where the God's Creation is at it's best. You have a powerful, abstractly
thinking mind: the computer doesn't, it is your slave, use it!

Also look into realtime code generators and JIT-like compiler technology.
Those are the near-future for high-performance low-level bitfucking software
engineering. Shaders in GPU's and shader compilers by nVidia and Microsoft
show the way the technologies like JIT from Sun have made popular in the
past decade or so.

Human operators writing assembly is backwards development. Just my opinion,
not the ultimate truth. :)
 
W

wogston

Cristian be sure that at the end you restore the value of the registers
that
you use in your asm code. You can change the value of registers with your
asm code.

I recall that VC++ (on some versions) need user only to keep state of EBP
consistent (and not touch ESP, since the local variables are relative to
ESP). I could remember wrong, but that's besides the point, inline asm
blocks distrupt the optimizations the compiler can do, and usually the
resulting code is worse-performance, especially if the "optimizations" user
is doing are few-liners.

I'd go to assembly only if there are SIMD possibilities beyond the scope of
comp.lang.c++, and in those cases I propably use realtime code generation
and if I really want to type the mnemonics by my own hand into textfile, I
prefer .asm filename extension and NASM for compiling the binary for later
linking. SoftWire is a good search keyword in sourceforge/google for getting
started.
 
B

Bob Hairgrove

Sorry, I ment 'registers' (my first language is Romanian).

<<I'd be wary about using asm in any code, are you absolutely sure its
needed.>>

Could there be problems even if the asm code is isolated in functions?

I have to do a lot of bit operations, like decoding a 32 bit variable into
values represented by groups of bits, (for example the value of
bits[12:15]), swapping the bytes, etc. These would be done very nicely using
the asm instructions.

Cristian

Wrap your __asm code like this:

__asm {
pushfd
pushad
... your code ...
popad
popfd
}
 
W

wogston

Wrap your __asm code like this:
Quote #1, MSDN:

When using __asm to write assembly language in C/C++ functions, you don't
need to preserve the EAX, EBX, ECX, EDX, ESI, or EDI registers.

Quote #2, MSDN:

You should preserve other registers you use (such as DS, SS, SP, BP, and
flags registers) for the scope of the __asm block. You should preserve the
ESP and EBP registers unless you have some reason to change them (to switch
stacks, for example).

http://msdn.microsoft.com/library/d...d_preserving_registers_in_inline_assembly.asp

While this is specific to Microsoft Visual C++ implementation of the
language, I don't think this is topical to this newsgroup. I could be wrong.
 
B

Bob Hairgrove

Quote #1, MSDN:

When using __asm to write assembly language in C/C++ functions, you don't
need to preserve the EAX, EBX, ECX, EDX, ESI, or EDI registers.

...."unless you are using __fastcall calling convention."

Also,

"...by using EBX, ESI or EDI in inline assembly code, you force the
compiler to save and restore those registers in the function prologue
and epilogue..."

There is no guarantee that other compilers will do this.
Quote #2, MSDN:
[snip]

While this is specific to Microsoft Visual C++ implementation of the
language, I don't think this is topical to this newsgroup. I could be wrong.

It's definitely OT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top