LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Is there a C/C++ code --> ASM code chart ??? (https://www.linuxquestions.org/questions/programming-9/is-there-a-c-c-code-asm-code-chart-443115/)

RipClaw 05-09-2006 09:42 AM

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

PTrenholme 05-09-2006 10:09 AM

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

ioerror 05-09-2006 10:14 AM

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.

RipClaw 05-09-2006 11:19 AM

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 :scratch:



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]

ioerror 05-09-2006 11:58 AM

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.

aluser 05-09-2006 05:57 PM

you can also just link it with -static and run objdump -d on your executable

ioerror 05-09-2006 06:35 PM

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

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

Good catch, aluser.

paulsm4 05-09-2006 06:36 PM

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


All times are GMT -5. The time now is 05:38 AM.