LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2008, 04:49 AM   #1
Mr_Shameless
Member
 
Registered: Aug 2006
Location: Asia
Distribution: Ubuntu
Posts: 59

Rep: Reputation: 15
Observation of Assembly code produced by GCC 4.3.2 for various -march parameters


Hi,

I did this out of curiousity and found out something that I could not explain.

I tested this on Mandriva 2009, gcc 4.3.2

Here is the source code of the C program:
Code:
#include <stdio.h>

int main(void)
{
	int sum = 0;
	int i;
	for(i = 1; i < 5; ++i)
	{
		sum += i;
	}
	
	return 0;
}
I first tried compiling the code with
Code:
~$ gcc -g -march=i386 main.c
, then used gdb to observe the program:
Code:
~$ gdb -q a.out
(gdb) disassemble main
Here is what I obtained for i386: (notice the line in bold)
Code:
Dump of assembler code for function main:
0x08048344 <main+0>:    lea    ecx,[esp+0x4]
0x08048348 <main+4>:    and    esp,0xfffffff0
0x0804834b <main+7>:    push   DWORD PTR [ecx-0x4]
0x0804834e <main+10>:   push   ebp
0x0804834f <main+11>:   mov    ebp,esp
0x08048351 <main+13>:   push   ecx
0x08048352 <main+14>:   sub    esp,0x10
0x08048355 <main+17>:   mov    DWORD PTR [ebp-0xc],0x0
0x0804835c <main+24>:   mov    DWORD PTR [ebp-0x8],0x1
0x08048363 <main+31>:   jmp    0x804836e <main+42>
0x08048365 <main+33>:   mov    eax,DWORD PTR [ebp-0x8]
0x08048368 <main+36>:   add    DWORD PTR [ebp-0xc],eax
0x0804836b <main+39>:   inc    DWORD PTR [ebp-0x8]
0x0804836e <main+42>:   cmp    DWORD PTR [ebp-0x8],0x4
0x08048372 <main+46>:   jle    0x8048365 <main+33>
0x08048374 <main+48>:   mov    eax,0x0
0x08048379 <main+53>:   add    esp,0x10
0x0804837c <main+56>:   pop    ecx
0x0804837d <main+57>:   leave  
0x0804837e <main+58>:   lea    esp,[ecx-0x4]
0x08048381 <main+61>:   ret    
End of assembler dump.
Doing the same thing again with i686:
Code:
gcc -g -march=i686 main.c
, I obtained:
Code:
Dump of assembler code for function main:
0x08048344 <main+0>:    lea    ecx,[esp+0x4]
0x08048348 <main+4>:    and    esp,0xfffffff0
0x0804834b <main+7>:    push   DWORD PTR [ecx-0x4]
0x0804834e <main+10>:   push   ebp
0x0804834f <main+11>:   mov    ebp,esp
0x08048351 <main+13>:   push   ecx
0x08048352 <main+14>:   sub    esp,0x10
0x08048355 <main+17>:   mov    DWORD PTR [ebp-0xc],0x0
0x0804835c <main+24>:   mov    DWORD PTR [ebp-0x8],0x1
0x08048363 <main+31>:   jmp    0x804836f <main+43>
0x08048365 <main+33>:   mov    eax,DWORD PTR [ebp-0x8]
0x08048368 <main+36>:   add    DWORD PTR [ebp-0xc],eax
0x0804836b <main+39>:   add    DWORD PTR [ebp-0x8],0x1
0x0804836f <main+43>:   cmp    DWORD PTR [ebp-0x8],0x4
0x08048373 <main+47>:   jle    0x8048365 <main+33>
0x08048375 <main+49>:   mov    eax,0x0
0x0804837a <main+54>:   add    esp,0x10
0x0804837d <main+57>:   pop    ecx
0x0804837e <main+58>:   pop    ebp
0x0804837f <main+59>:   lea    esp,[ecx-0x4]
0x08048382 <main+62>:   ret    
End of assembler dump.
Notice that the line in bold is different for different CPUs I compile for. I tested with i386, i686, pentium3, pentium4, prescott, and core2. My result can be summarized below:
Code:
	i386:		0x0804836b <main+39>:   inc    DWORD PTR [ebp-0x8]
	i686:		0x0804836b <main+39>:   add    DWORD PTR [ebp-0x8],0x1
	pentium3:	0x0804836b <main+39>:   inc    DWORD PTR [ebp-0x8]
	pentium4:	0x0804836b <main+39>:   add    DWORD PTR [ebp-0x8],0x1
	prescott:	0x0804836b <main+39>:   add    DWORD PTR [ebp-0x8],0x1	
	core2:		0x0804836b <main+39>:   inc    DWORD PTR [ebp-0x8]
Here are my questions:
  1. What is the difference between the ADD and INC instructions? Is one faster than another?
  2. Assume there is a faster of the two, then why is INC used for i386, then replaced with ADD in i686, then used again for pentium3, then replaced in pentium4, then used again for core2?

Thank you very much
 
Old 10-25-2008, 05:54 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Hmm... that's amusing. To see if ADD (1) is faster than INC, you will need to look at the datasheet for each CPU (don't assume that there were no changes in the design). Other subtle differences may lie in how the flags (including overflow and carry) registers are affected by the two instructions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
generating mips assembly from c code with gcc? lilili Programming 4 05-11-2006 08:37 PM
gcc/Assembly code gigantas1985 Programming 0 02-28-2006 05:52 AM
doubts about assembly code generated from gcc morph_ind Programming 9 05-28-2005 12:08 AM
Can gcc cross-compile C code to M68K Assembly? lowpro2k3 Programming 1 03-25-2005 07:50 AM
difference between distro produced by group vs. produced by single person lostsoul Linux - General 2 04-08-2004 01:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration