LinuxQuestions.org
Help answer threads with 0 replies.
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 05-09-2006, 09:42 AM   #1
RipClaw
LQ Newbie
 
Registered: Oct 2005
Distribution: PCQ Linux 2006
Posts: 19

Rep: Reputation: 0
Is there a C/C++ code --> ASM code chart ???


Hi,

I have seen a chart in this website,
http://www.keil.com/support/man/docs...51_le_ptrs.htm
which shows Assembly language equivalent of C code. In this case, it is for the 8051 CPU.

Is there any such list available for the x86 based CPUs ??? for all C instructions ...
 
Old 05-09-2006, 10:09 AM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
I should think it would depend on how the compiler writer implemented the compiler, eh?

If you're using gcc, just look at the source code (most likely the parser) to see how the code is converted into CPU instructions.

Of course, the actual conversion depends on the target CPU(s). That's why most compilers first compile to an "intermediate" code, then convert that to the actual hardware instructions. The first part can usually be done without reference to the target CPU. (I think that MS has done this with their C# compiler even more explicitly, making the first stage code exportable so the final implementation can be done "on the fly" by different CPUs as needed. [That, of course, is so they can "protect" the "Intelectual Property" of "owners" of the source code. But that's an issue for some other thread.])
 
Old 05-09-2006, 10:14 AM   #3
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 32
There is no single list because the code generated is dependant on (at least), the compiler, the compiler version, optimization flags used, the processor being compiled for (486, pentium, athlon etc), and probably a number of other things that don't come to mind offhand. Compiling lists for all possible permutations would be a huge and pointless task. If you want to see the assembler for some specific code, then assemble it with gcc -S.
 
Old 05-09-2006, 11:19 AM   #4
RipClaw
LQ Newbie
 
Registered: Oct 2005
Distribution: PCQ Linux 2006
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ioerror
then assemble it with gcc -S.
OK!, I saw the ASM in the generated *.s file.

...Then it has calls to "printf", for which the code is not present.
How can I make see them too



Code:
#include <stdio.h>
 main()
{
    long int a;
    for(a = 1; a<=10 ;a++)
      printf(" %d ", a);
    
}
transforms to
[HTML]
Quote:
.file "prog10.c"
gcc2_compiled.:
.section ".rodata"
.align 8
.LLC0:
.asciz " %d "
.section ".text"
.align 4
.global main
.type main,#function
.proc 04
main:
!#PROLOGUE# 0
save %sp,-120,%sp
!#PROLOGUE# 1
nop
mov 1,%o0
st %o0,[%fp-20]
.LL2:
ld [%fp-20],%o0
cmp %o0,10
ble .LL5
nop
b .LL3
nop
.LL5:
sethi %hi(.LLC0),%o1
or %o1,%lo(.LLC0),%o0
ld [%fp-20],%o1
call printf,0
nop
.LL4:
ld [%fp-20],%o1
add %o1,1,%o0
mov %o0,%o1
st %o1,[%fp-20]
b .LL2
nop
.LL3:
.LL1:
ret
restore
.LLfe1:
.size main,.LLfe1-main
.ident "GCC: (GNU) 2.7.2.3"
[/HTML]
 
Old 05-09-2006, 11:58 AM   #5
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 32
Quote:
...Then it has calls to "printf", for which the code is not present.
How can I make see them too
printf is in libc, so it won't show up in your own code. If you want to see the source, you have two choices:

1. Get the source for glibc and assemble it.

2. Compile your program and run it under gdb, linking to a glibc with debugging symbols, put a breakpoint on printf, then disassemble it.
 
Old 05-09-2006, 05:57 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
you can also just link it with -static and run objdump -d on your executable
 
Old 05-09-2006, 06:35 PM   #7
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 32
Doh! Fumbled it (again). Why do I always forget about objdump?

OK, you have 3 choices, and counting....

Good catch, aluser.
 
Old 05-09-2006, 06:36 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
RipClaw -

Book recommendation:
Professional Assembly Language, Richard Blum
http://www.bookpool.com/sm/0764579010

It covers all the basics of x86 assembler, has some great insights into "less travelled" roads like FPU instructions and MMX extensions ... and does it all from a Linux perspective.

Among other things, you'll learn how "printf" (a call into the standard C library) and actual system calls (like the "int 0x80" that corresponds to "open()" or "fork()") work surprisingly early on. Blum has written both a great primer, and a great reference text. I think you might like it.

IMHO .. PSM
 
  


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
LXer: Firefox Extension Draws Code Chart LXer Syndicated Linux News 0 01-14-2006 12:31 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Have you seen such asm code? snowing Programming 2 07-07-2005 05:13 AM
Source Code to Flow Chart Converter leibinitz Linux - Software 0 06-21-2005 02:20 AM
Using asm code in C program. grub Programming 2 04-03-2003 11:00 PM

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

All times are GMT -5. The time now is 11:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration