473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

@@@ A Serious Question @@@

Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077

#include <stdio.h>

int main(void) {
int a=0;
int b;

if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;

printf("%d\n",b);

return 0;
}

and after disassembled, it became

_main proc near ; modified version with my comments
nB = dword ptr -8
nA = dword ptr -4

push ebp
mov ebp, esp
sub esp, 8
mov [ebp+nA], 0 ; nA=0
cmp [ebp+nA], 0
jge short GE ; if(nA >= 0) then goto GE
mov [ebp+nB], -5
jmp short Handle
GE:
cmp [ebp+nA], 0
jle short LE ; if(nA <= 0) then goto LE
mov [ebp+nB], 5 ; here only can be nA>0 is true
jmp short Handle
LE:
mov [ebp+nB], 0 ; if (nA >= 0 && nA <= 0 ) then
Handle:
mov eax, [ebp+nB]
push eax
push offset format ; "%d\n"
call _printf
add esp, 8
xor eax, eax
mov esp, ebp
pop ebp
retn
_main endp

which using some kinda non-straightforward logic like this

if(a>=0) {
if(a<=0) b=0;
else b=5;
} else b=-5;

and after I rewrite the original C code in a more succinct way
only in one line statement like this

b=(a<0)?-5:((a>0)?5:0);

then the disassembled code changed accordingly like this

_main proc near
nB = dword ptr -0Ch
nTmp = dword ptr -8
nA = dword ptr -4

push ebp
mov ebp, esp
sub esp, 0Ch ; Set up stack frame
mov [ebp+nA], 0 ; nA=0
cmp [ebp+nA], 0 ; compare nA with 0
jge short GE ; if (nA >= 0) then goto GE
mov [ebp+nB], -5 ; if (nA < 0) then nB=-5
jmp short Handle
GE:
xor eax, eax ; IT'S KINDA TRICKY, ANY COMMENTS
cmp [ebp+nA], 0 ; OBSERVATIONS WOULD BE WELCOME !!!!
setle al ; if <= then AL=1
dec eax ; EAX-- ;ESPECIALLY ON HOW AND WHY
and eax, 5
mov [ebp+nB], eax
Handle:
mov ecx, [ebp+nB]
mov [ebp+nTmp], ecx ;WHY USE A TEMPORARY VARIABLE ?
mov edx, [ebp+nTmp] ; store nB to EDX
push edx ; pass nB as parameter of _printf
push offset aD ; "%d\n"
call _printf ; print out the value
add esp, 8 ; restore stack
xor eax, eax ; set return value to 0
mov esp, ebp ; restore stack pointer
pop ebp ; retore EBP with original value
retn ; return
_main endp

my question is: 1) refer to the comment parts
2) was it any good, probably for the optimization purpose ?
3) what kinda logic style would you prefer ?
4) if asked, how would you write this program in assembly ?
Nov 13 '05 #1
12 1323
sugaray wrote:
and after disassembled, it became


It seems that your questions would be more appropriate in
comp.lang.asm.x86 instead of comp.lang.c

Nov 13 '05 #2
sugaray <ru****@sohu.com> wrote:
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 #include <stdio.h> int main(void) {
int a=0;
int b;

if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;
This branch never can be taken, whatever value 'a' has.
printf("%d\n",b);

return 0;
}


You don't need to look at the disassembled code to optimize this
program, just write it as

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
puts( "5" );
return EXIT_SUCCESS;
}

which is all it boils down to. And that's what a clever compiler with
optimization should easily find out. But to what kind of machine code
the compiler translates your program to is off-topic on clc.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #3
sugaray wrote a question about micro-optimization with <ot>assembly code
inspection</ot>:
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077

#include <stdio.h>

int main(void) {
int a=0;
int b;

if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;


I cannot believe that someone who would write the above gives a rat's ass
about optimization. If !(a<0) then (a>=0). That means that the above
conditionals collapse to
if (a < 0) b = -5; else b = 5;
or
b = (a < 0) ? -5 : 5 ;

The second 'if' is useless, as is the second 'else'.
--
Martin Ambuhl

Nov 13 '05 #4
Martin Ambuhl wrote:
sugaray wrote a question about micro-optimization with <ot>assembly code
inspection</ot>:
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077

#include <stdio.h>

int main(void) {
int a=0;
int b;

if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;

I cannot believe that someone who would write the above gives a rat's
ass about optimization. If !(a<0) then (a>=0). That means that the
above conditionals collapse to
if (a < 0) b = -5; else b = 5;
or
b = (a < 0) ? -5 : 5 ;

The second 'if' is useless, as is the second 'else'.


One could simplify even futher.
Given that a==0 and not altered in the main() function,
the result is:
b = 5
So, the program can be simplified to:
int main(void)
{
int b = 5;
printf("%d\n",b);

return 0;
}

or just:
int main(void)
{
printf("5\n", b);
return 0;
}

Since none of printf's formatting functions are used,
this is simplified as:
int main(void)
{
puts("5");
return 0;
}

Hmm, is this what the compiler finally generated???

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #5
sugaray wrote:
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077

#include <stdio.h>

int main(void) {
int a=0;
int b;

if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;

printf("%d\n",b);

return 0;
}


Let's talk optimization here.
Since the variable 'a' is not changed after its assignment,
the compiler is free to set b to 5.

Otherwise, note that the opposite of "<" is ">=", so
that the final else clause (b = 0;) will never be executed.
Thus your program is reduced to:
int main(void)
{
int a = 0;
int b;

if (a < 0)
b = -5;
else
b = 5;
printf("%d\n", b);
return 0;
}

The 'if' statement may be replaced by a condition statement:
b = (a < 0) ? -5 : 5;
Plugging in the above replacement, the program is reduced to:
int main(void)
{
int a = 0;
b = (a < 0) ? -5 : 5;
printf("%d\n", b);
return 0;
}

Since the variable 'b' is not modified between the assignment
and the printf, the right-hand side of the assignement can
be substituted into the printf call:
int main(void)
{
int a = 0;
printf("%d\n", ((a < 0) ? -5 : 5));
return 0;
}

As you can see, there are various stages in the optimization
process. Your compiler is allowed to generate any one of
them (or any other equivalent functionality) before generating
the assembly language listing.

The variable 'a' is not altered in the program, so here is
my final optimization:
int main(void)
{
printf("%d\n", 5);
return 0;
}

The printf statement can be replaced by a call to the simpler
function puts since the argument to "printf" is constant and
the formatting string does not change. But I think that is
becoming a bit too pedantic.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #6
Thomas Matthews wrote:
.... snip ...
The variable 'a' is not altered in the program, so here is
my final optimization:
int main(void)
{
printf("%d\n", 5);
return 0;
}

The printf statement can be replaced by a call to the simpler
function puts since the argument to "printf" is constant and
the formatting string does not change. But I think that is
becoming a bit too pedantic.


Nah. However I prefer reduction to:

#include <stdio.h>
int main(void) {putchar('5'); putchar('\n'); return 0}

which might well generate a significantly smaller load module.
Try it and see.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #7

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:7M*******************@newssvr32.news.prodigy. com...
Martin Ambuhl wrote:
sugaray wrote a question about micro-optimization with <ot>assembly code
inspection</ot>:
Hi folks, I wrote the following C code, and compiled using
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077

#include <stdio.h>

int main(void) {
int a=0;
int b;

if(a<0) b=-5;
else if(a>=0) b=5;
else b=0;

I cannot believe that someone who would write the above gives a rat's
ass about optimization. If !(a<0) then (a>=0). That means that the
above conditionals collapse to
if (a < 0) b = -5; else b = 5;
or
b = (a < 0) ? -5 : 5 ;

The second 'if' is useless, as is the second 'else'.


One could simplify even futher.
Given that a==0 and not altered in the main() function,
the result is:
b = 5
So, the program can be simplified to:
int main(void)
{
int b = 5;
printf("%d\n",b);

return 0;
}

or just:
int main(void)
{
printf("5\n", b);
return 0;
}

Since none of printf's formatting functions are used,
this is simplified as:
int main(void)
{
puts("5");


putchar('5');

:-)

return 0;
}


-Mike
Nov 13 '05 #8
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message news:<7M*******************@newssvr32.news.prodigy .com>...

<snipped>

or just:
int main(void)
{
printf("5\n", b);


itym
printf ("5\n");

<snipped>

goose,
Nov 13 '05 #9
"Mike Wahler" <mk******@mkwahler.net> wrote:

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote:

int main(void)
{
puts("5");
putchar('5');

:-)


followed by:

putchar( '\n' );

or

fflush( stdout );

;-)

return 0;
}


--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10
On Tue, 18 Nov 2003 11:41:20 +0100, Irrwahn Grausewitz
<ir*******@freenet.de> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote:

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote:
>
> int main(void)
> {
> puts("5");


putchar('5');

:-)


followed by:

putchar( '\n' );

or

fflush( stdout );

;-)

> return 0;
> }


Exiting the program will surely flush the buffer. Even if the standard
doesn't say so :-)
--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #11
In <93********************************@4ax.com> Alan Balmer <al******@att.net> writes:
Exiting the program will surely flush the buffer. Even if the standard
doesn't say so :-)


The standard does say so.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12
Alan Balmer <al******@att.net> wrote:
On Tue, 18 Nov 2003 11:41:20 +0100, Irrwahn Grausewitz wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote:
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote:
> int main(void)
> {
> puts("5");
putchar('5');
followed by:
putchar( '\n' );
or
fflush( stdout );
> return 0;
> }


Exiting the program will surely flush the buffer.


Err, stupid me; yes, you're right.
Even if the standard
doesn't say so :-)


Oh, it does:
in ISO/IEC 9899:1999 5.1.2.2.3, by reference to 7.20.4.3.

However, there's still the problem that it's implementation defined
if the last line of output requires a terminating new-line character
for text streams. Reference: 7.19.2#2

IMHO that makes

putchar( '5' ); putchar( '\n' );
or
puts( "5" );
or
printf( "5\n" );

portable alternatives here.

;-D

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: VlaR | last post by:
As I got no reply to my first post ("Serious bug in ngen.exe") within few business days but having MSDN Universal Subscription I posting this problem again (with just registered nospam alias). I...
142
by: Herr Lucifer | last post by:
As the founder of .NET framework, Microsoft claims that it invention will be the next best platform for programming in a near future. Now it is 2005, ..NET is 5 years old, and can talk and walk for...
1
by: iceColdFire | last post by:
Hi, I am into C++ compiler validation since past couple of years. Lately I have grown with a strange feeling...while on the course of my work, I start getting bored and very bored...I have...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
5
by: Colonel Kernel | last post by:
I've found what seems to be a very serious (although obscure) bug in VC++.NET 2003 when using Loki's class hierarchy-generation facilities. The compiler seems to generate an invalid v-table for my...
7
by: Ioannis Vranos | last post by:
Fellows there is probably a serious implementation bug of C++/CLI indexed property in VC++ 2005, it looks like it is implemented the opposite way than the C++/CLI draft says! At first the...
2
by: WJ | last post by:
This post is a follow up from the original post dated Oct 16, 2004 "I have this problem, pls help!" created by Paul FI. These bugs are rather serious and we would like to know how to get around. ...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
1
by: Guy Macon | last post by:
Serious Security Flaw in Google Chrome: http://www.readwriteweb.com/archives/security_flaw_in_google_chrome.php -- Guy Macon <http://www.GuyMacon.com/>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.