Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm new to c language and want to establish the intuition to map c language into assembly language so that I can have a strong sense of how the stack, register, memory, code work when I see the c code. I have read several assembly language books, which is mainly focused on the assembly syntax rather than the relationship between c and assembly. Does anyone know where can I find such book about the map between c and assembly languages.
I don't know if such a book exists (if it does, it'll likely be a book about compilers). However, there's an easier solution: try it.
Write some C code, then compile it with debug symbols (these instructions assume linux):
gcc foo.c -o foo
Then, use a debugger:
gdb ./foo
break MyFunction
run
disass
This will set a breakpoint on MyFunction, then run the program until it reaches that breakpoint. disass will print the assembly for that function. You can use stepi to step one instruction at a time, or next to step one C line at a time.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 months ago.
Improve this question
I'm looking for some kind of C dialect that is as minimalistic as C but has built-in classes support. So I can (and encouraged to) use macros, pointers to arrays and manual memory management but also create classes, add fields and member functions to them etc. This question appeared when I tried to implement some kind of OOP in C and typedef struct and function pointers do something similar to what I want, but "member functions" require to manually pass a pointer to the object as a parameter to them, and that's not what I want to do. I know that I can just write on C++ as on "C with classes" and I would, however C++ encourages a different programming style and I'm curious if there is something that is exactly what I want.
I was searching for "C with classes" but I've only seen C++ in results, so I expect that the answer is "just use C++" and I'm OK with that, but I'm just curious.
C++ encourages a different programming style
You can write C++ in whatever style you like. Just choose not to use the features (and libraries) that don't suit your C-with-classes aesthetic.
"C with classes" was originally compiled to C by Cfront, but that's extremely dead AFAIK.
I doubt there's much demand for a resurrected Cfront when simply choosing a subset of C++, and using a current C++ compiler, already does everything you actually require.
FWIW I have written object-oriented C in the past, and manually passing this isn't that much of a burden. Even in Python you have to declare the self parameter explicitly, and nobody seems upset about that. Having to pass it in explicitly as well isn't so bad.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
This may sound silly, but is there an optimizing assembly compiler? Like gcc or MSVC would optimize C.
Or at least is there a usable decompiler that produces compilable C? The code doesn't need to be readable. I just want to asm -> c -> gcc -O3 -> ELF/PE.
NOTE1: Maybe I should explain why I need this, after seeing a downvote. So there is a FreeBASIC compiler that produces the executable directly, but the compiler doesn't optimize well enough. I don't know why they didn't choose C as an intermediate representation. Perhaps I can rewrite the compiler to produce C, but not really now. It's trivial at least in Linux to produce the assembly from the executable, so I was thinking maybe there is a way to optimize this automatically.
Yes. The compiler infrastructure of the Plan 9 operating system uses assemblers and linkers that do various optimizing tasks usually performed by the compiler like:
instruction selection (unsupported instructions are emulated)
register allocation (partially)
dead code elimination
some peephole optimization
See here for an introduction into the Go flavour of Plan 9 assembly.
No, and it probably cannot exist in the general case (but it might exist, and did exist in the past -1990s- for some MIPS or Sparc workstations, if the assembler code is somehow restricted). You might try to use some decompiler (but these cannot reliably work on every code, in particular, because not every assembler code is expressible in C, and because a compiler is losing some information from its source code), and then recompile with optimization the decompiled C code.
BTW, you really need some optimizing FreeBASIC compiler (e.g. code your FreeBASIC to C - or to LLVM - translator). Perhaps (if your FreeBasic program is not big) you might instead translate (by hand, perhaps with the help of some script) your FreeBASIC source into something else.
At last, FreeBASIC is free software (GPLv2 licensed). You could spend some (months or years) of effort to improve it, and make it generate either C, or LLVM code, or use LLVM or GCCJIT just-in-time compilation libraries.
I personally believe that it is not worth the effort, because BASIC is a nearly dead language (and there are lots of languages better than BASIC: Ocaml, Haskell, Common Lisp, Clojure, Scala, Scheme, Go, D, ....; and the existing living source code base in BASIC is not that big today)
It looks like the FreeBASIC-1.02.1-source/ (as provided by the FreeBASIC-1.02.1-source.tar.gz archive) contains a inc/llvm-c.bi & a src/compiler/ir-llvm.bas files, so probably already has some work to generate LLVM. You should investigate (and ask LLVM to optimize more).
Notice that I never used FreeBASIC and probably never will. But I do have the habit of looking inside free software source code, and you should get that habit too...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a backtrace of a process. and I want determine the value of a argument of one function called in the call trace, I have the .o file and I disassemble it, So I have the assembly procedure of that particular function, How can I calculate the value of the function's passing argument through the backtrace and the assembly code? It's on the ARM platform, I'm not quite familar with the ARM call trace, and assembly code.
If the code is written in C, this information is not available solely from the executable image, for the simple reason that C does not mangle function name symbols in order to encode the function parameter types.
Brief experimentation with gdb shows that if C code is compiled with the debugging flag, -g, gcc does put sufficient information into the executable's debug records for gdb to be able to figure out the function parameter types, and display function arguments in the backtrace.
But, if the executable is not compiled with -g, all that's in the executable are the function names, and their addresses, and that's all that gdb can show, in a backtrace from a coredump.
So, if you're working with .o files containing C code, without any debug stuff in it, there's nothing that can show you what the function parameters are,
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to know how to execute step by step for my c program. I'm not getting answer as expected. So please tell me how to see step by step execution. I'm new to linux
Compile the program with '-g' option
example as
Compile the program
gcc test.c -g
which will generate a.out pass it with gdb
gdb a.out
then set breakpoint to main
gdb) break main
Then run your program in gdb
gdb) run
then break point hits use 'n' or 'next' to step to different lines
gdb) n
Use 's' for stepping into function and 'p' printing var value
Example :
gdb) s <fun_name>
gdb) p x
More convenient than gdb (especially for a beginer) may be some of IDEs. I'd suggest qtcreator.
At least a makefile (for ready project) will be required. You can also create a new project in qtcreator and import your files.
There are many debugging tools in Linux. I prefer gdb.
Usage:
while compiling use -g flag with. Ex:
gcc -g *.c
to see step by step execution use gdb tool:
ex: gdb ./a.out
Then give start command to start main function.
Then press s and continuosly press enter it will execute step by step.
Note: if it's a library function please press n instead of s.
to quit from execution press q.
Please refer –-help on debugging time to know more info. There are many instructions you have to know like run, break, next, info breakpoints..........
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I want to find the C libraries' source code to find out more about the functions used.Is GCC the only resource I can count on?I couldn't use the doxygen html version of the GCC libraries,to tell the truth it seems complex to me,for example I couldn't find the printf function's source code(Was I looking in the wrong place?).
Thanks in advance.
OTOH, in addition to glibc:
uclibc
dietlibc
BSD libc
Reading the source code is one thing. Reading a good book that includes source code is another thing entirely. And I'm not sure you can do better than The Standard C Library, by PJ Plauger. It's 20 years old, but for me it's still a page-turner.
Man, I feel old.
The GNU version is here:
Info:
http://www.gnu.org/software/libc/
Download:
http://ftp.gnu.org/gnu/glibc/
You may need to narrow down your question a bit. The implementation varies. Not everything related to implementation details (perhaps pretty much all of it - someone with more standard knowledge can chip in) is prescribed by the C/C++ standard.
In the end you may understand how a particular library decided to do it. It's still useful knowledge, but not THE answer.
The simplest and cleanest standard C library I've ever seen is Minix's standard library.
I ported it on at least 3 toolchains with virtually no effort.
I actually grew up with that library.
I couldn't find the printf function's source code
Here is the source code for this function, it seems to be calling to __vbprintf_internal, which is found glibc/stdio-common/vfprintf-internal.c. Glibc source code is also available in bootlin, I think it is more readable and convenient code.
int __printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = __vfprintf_internal (stdout, format, arg, 0);
va_end (arg);
return done;
}
glibc/stdio-common/printf.c