howdy....
plz take a look at the following codes, and tell me the reason.
1 #define swap(a,b) a=a^b;b=b^a;a=a ^b
2
3 int main(void){
4 register int a=4;
5 register int b=5;
6 swap(a,b);
7
8 return 0;
9 }
The assemble code of the above code.
(gdb) disassemble main
Dump of assembler code for function main:
0x08048334 <main+0>: push %ebp
0x08048335 <main+1>: mov %esp,%ebp
0x08048337 <main+3>: sub $0x8,%esp
0x0804833a <main+6>: and $0xfffffff0,%es p
0x0804833d <main+9>: mov $0x0,%eax
0x08048342 <main+14>: add $0xf,%eax
0x08048345 <main+17>: add $0xf,%eax
0x08048348 <main+20>: shr $0x4,%eax
0x0804834b <main+23>: shl $0x4,%eax
0x0804834e <main+26>: sub %eax,%esp
0x08048350 <main+28>: mov $0x0,%eax
0x08048355 <main+33>: leave
0x08048356 <main+34>: ret
End of assembler dump.
where is the XOR instruction?
if I remove the keyword "register" in my C program.
1 #define swap(a,b) a=a^b;b=b^a;a=a ^b
2
3 int main(void){
4 int a=4;
5 int b=5;
6 swap(a,b);
7 return 0;
8 }
I'll got the following "monster" asm code...
(gdb) disassemble main
Dump of assembler code for function main:
0x08048334 <main+0>: push %ebp
0x08048335 <main+1>: mov %esp,%ebp
0x08048337 <main+3>: sub $0x8,%esp
0x0804833a <main+6>: and $0xfffffff0,%es p
0x0804833d <main+9>: mov $0x0,%eax
0x08048342 <main+14>: add $0xf,%eax
0x08048345 <main+17>: add $0xf,%eax
0x08048348 <main+20>: shr $0x4,%eax
0x0804834b <main+23>: shl $0x4,%eax
0x0804834e <main+26>: sub %eax,%esp
0x08048350 <main+28>: movl $0x4,0xfffffffc (%ebp)
0x08048357 <main+35>: movl $0x5,0xfffffff8 (%ebp)
0x0804835e <main+42>: mov 0xfffffff8(%ebp ),%edx
0x08048361 <main+45>: lea 0xfffffffc(%ebp ),%eax
0x08048364 <main+48>: xor %edx,(%eax)
0x08048366 <main+50>: mov 0xfffffffc(%ebp ),%edx
0x08048369 <main+53>: lea 0xfffffff8(%ebp ),%eax
0x0804836c <main+56>: xor %edx,(%eax)
0x0804836e <main+58>: mov 0xfffffff8(%ebp ),%edx
0x08048371 <main+61>: lea 0xfffffffc(%ebp ),%eax
0x08048374 <main+64>: xor %edx,(%eax)
0x08048376 <main+66>: mov $0x0,%eax
0x0804837b <main+71>: leave
0x0804837c <main+72>: ret
End of assembler dump.
Another strange thing is the xor instruction.
Plz see the code:
0x0804835e <main+42>: mov 0xfffffff8(%ebp ),%edx
0x08048361 <main+45>: lea 0xfffffffc(%ebp ),%eax
0x08048364 <main+48>: xor %edx,(%eax)
0x08048366 <main+50>: mov 0xfffffffc(%ebp ),%edx
0x08048369 <main+53>: lea 0xfffffff8(%ebp ),%eax
0x0804836c <main+56>: xor %edx,(%eax)
why dont use
xor %edx,(%eax)
xor %eax,(%edx)
to replace the redundent
0x08048366 <main+50>: mov 0xfffffffc(%ebp ),%edx
0x08048369 <main+53>: lea 0xfffffff8(%ebp ),%eax
Thanx!!!!
Nov 14 '05
13 2097
Keith Thompson wrote: ... First, any operation can be eliminated if its result isn't used, which I think is what was happening with the OP's code.
Leaving that aside, there are a lot of cases where a multiplication by a constant can be replaced by some cheaper operation such as a shift ... That can happen if the compiler knows that both operands have the value 0 or 1 (but only if a non-equality check is cheaper than an xor ...
This is all great, but my point is, once again, that in general case one
should not expect the compiler to unconditionally translate C operators
into their "intuitive" counterparts in machine language (which is what
the OP seems to do). There are several reasons why this might not be the
case, regardless of whether the result of the expression is used in the
program or not. One thing worth mentioning is that often the seemingly
natural mapping between C operators and machine instructions is not
really that straightforward when one starts to look closer and notices
some little but important detail of C operator specification (like, for
example, the requirement of integral division to operate in accordance
"round towards zero" approach).
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich <an************ **@hotmail.com> writes: Keith Thompson wrote: ... First, any operation can be eliminated if its result isn't used, which I think is what was happening with the OP's code.
Leaving that aside, there are a lot of cases where a multiplication by a constant can be replaced by some cheaper operation such as a shift ... That can happen if the compiler knows that both operands have the value 0 or 1 (but only if a non-equality check is cheaper than an xor ...
This is all great, but my point is, once again, that in general case one should not expect the compiler to unconditionally translate C operators into their "intuitive" counterparts in machine language (which is what the OP seems to do). There are several reasons why this might not be the case, regardless of whether the result of the expression is used in the program or not. One thing worth mentioning is that often the seemingly natural mapping between C operators and machine instructions is not really that straightforward when one starts to look closer and notices some little but important detail of C operator specification (like, for example, the requirement of integral division to operate in accordance "round towards zero" approach).
I agree with your point. I just think you picked a poor example to
demonstrate it.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
In article <33************ *@individual.ne t>, Mi**********@in valid.invalid
says... Dave win wrote: howdy.... plz take a look at the following codes, and tell me the reason.
1 #define swap(a,b) a=a^b;b=b^a;a=a ^b
This macro will break when used in a loop. Use #define swap(a,b) do { \ a=a^b;b=b^a;a=a ^b;\ } while(0) to circumvent this problem.
It's still broken in that it does not work properly in all cases.
See the FAQ.
"Keith Thompson" <ks***@mib.or g> wrote One thing worth mentioning is that often the seemingly natural mapping between C operators and machine instructions is not really that straightforward when one starts to look closer and notices some little but important detail of C operator specification (like, for example, the requirement of integral division to operate in accordance "round towards zero" approach).
I agree with your point. I just think you picked a poor example to demonstrate it.
I sort of agree as well. One of the strengths of C is that the programmer
has a pretty good idea of what assembly will be generated, and thus an
inutitive feel for how processor-intensive his function will be. However to
extend this to assuming that "I have used a XOR therefore the instruction
XOR must appear in the machine code" is dangerous. Most of the time you will
be correct, but it might be that the instruction can be optimised away, as
happened to the OP, or it might be that an alternative is chosen by the
compiler for some obscure reason. So if the actual machine instructions are
vital, you should program in actual assembler. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Spiros Bousbouras |
last post by:
Has anyone found that declaring variables register affected
speed of execution ? If yes on what hardware and around
which year ?
|
by: sheldonlg |
last post by:
I did some coding on a site where register_globals is set to on. The
problem I encountered was that the session variable changed without my
changing it explicitly. I knew that in register globals being on, that
all the variables were global variables. What I didn't realize was that
it set up an equivalence such that the variable is an alias for the
session variable with the key name of that variable. That is,
$_SESSION is the same as...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |