By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

intergrating asm in c++

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

"Cristian Tota" <cr***********@vion-software.ro> wrote in message
news:bp**********@ally.taide.net...
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
Jul 22 '05 #2

P: n/a
Mike Wahler wrote:
"Cristian Tota" <cr***********@vion-software.ro> wrote in message
news:bp**********@ally.taide.net...
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.)

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).

Jul 22 '05 #3

P: n/a
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

Jul 22 '05 #4

P: n/a
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

"Cristian Tota" <cr***********@vion-software.ro> wrote in message
news:bp**********@ally.taide.net...
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


Jul 22 '05 #5

P: n/a

"Cristian Tota" <cr***********@vion-software.ro> wrote in message
news:bp**********@ally.taide.net...
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
Jul 22 '05 #6

P: n/a
Cristian Tota wrote:
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.
Jul 22 '05 #7

P: n/a
> 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. :)
Jul 22 '05 #8

P: n/a
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" <so*****@microsoft.com> ha scritto nel messaggio
news:bp**********@phys-news1.kolumbus.fi...
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. :)

Jul 22 '05 #9

P: n/a
> 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.
Jul 22 '05 #10

P: n/a
On Sun, 23 Nov 2003 01:53:10 +0200, "Cristian Tota"
<cr***********@vion-software.ro> wrote:
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
}
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #11

P: n/a
On Sun, 23 Nov 2003 12:05:26 GMT, wouldnt_you_like@to_know.com (Bob
Hairgrove) wrote:
Wrap your __asm code like this:

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


I forgot that you also need to CLD if you changed it.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #12

P: n/a
> >Wrap your __asm code like this:

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


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/de...e_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.
Jul 22 '05 #13

P: n/a
On Sun, 23 Nov 2003 16:32:30 +0200, "wogston" <so*****@microsoft.com>
wrote:
>Wrap your __asm code like this:
>
>__asm {
> pushfd
> pushad
> ... your code ...
> popad
> popfd
>}
>

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.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.