10 ms·
C Internals
- dannas 6y agoPer Vognsens description of how a compiler can lower C statements might be of interest! He lowers the code to RISC-V but the techniques and explanations is platform-independent. https://raw.githubusercontent.com/pervognsen/bitwise/master/notes/asm.txt https://raw.githubusercontent.com/pervognsen/bitwise/master/...
- z29LiTp5qUC30n 6y agoFor someone who wants to know how a C compiler converts C to assembly there is no better example than: https://github.com/oriansj/mescc-tools-seed/blob/master/x86/NASM/cc_x86.S https://github.com/oriansj/mescc-tools-seed/blob/master/x86/...
- deleted 6y ago[deleted]
- vmchale 6y agoThank you! This is very interesting.
- e19293001 6y agoFor those who are interested to hone their skills in translating C to assembly language just for the sake of grokking a C compiler, you might want to read this book: Assembly language and computer architecture using C++ and Java Book by Anthony J. Dos Reis From book's description: Students learn best by doing, and this book supplies much to do with various examples and projects to facilitate learning. For example, students not only use assemblers and linkers, they also write their own. Students study and use instruction sets to implement their own. The result is a book that is easy to read, engaging, and substantial. I'm not affiliated with the author though. This book helped a lot in my career as a hardware and firmware engineer. It talks about not only C language but C++. You'll be able to translate a C++ class into assembly language by hand. The drawback is that you'll learn a hypothetical CPU but the concepts are still the same with the real world CPU. C internals could supplement it as well.
- rrmm 6y agoImplementing a compiler for some language at some point in your life is a great project. It will really teach you a lot of deep down information on what's going on under the hood. I took a compilers course in grad school and it was a lot of fun. Especially the sort of frankenstein moment when you got it working and it actually did something, and its output would itself run. (Another fun thing is to design an instruction set and implement a simulator and asm for it. I have a lot of fond memories).
- thr0w__4w4y 6y agoI can’t improve upon your recommendation, but I’ll second it, from the point of a firmware / security / reverse engineer-er.
- jart 6y agohttps://gist.github.com/jart/fe8d104ef93149b5ba9b72912820282c https://gist.github.com/jart/fe8d104ef93149b5ba9b72912820282...
- porknubbins 6y agoThis sounds similar to Computer Systems: A Programmer’s Perspective which was great and taught basically x86 but was a little light on linkers. Any idea how these compare?
- mangamadaiyan 6y agoMinor quibble: The section on local variables assumes a downward-growing stack. This is completely fair, because the introduction specifies that the articles deal with an x86 world. What gets missed out is the fact that the direction of stack growth is determined by the processor :) This is not really a complaint ... it just seemed to me like a missed opportunity to mention something interesting.
- Someone 6y agoMinor quibble: The section on local variables assumes a stack. This is completely fair, because the introduction specifies that the articles deal with an x86 world and ABI. What gets missed out is the fact that the C standard doesn’t even prescribe (edited) the existence of a stack. (Just checked the final draft of ISO C2011. As far as I can tell, it doesn’t contain the word stack, and only uses push in the description of (w)ungetc. I also don’t think there’s wording about performance or ordering of addresses of local variables across frames that effectively enforces the use of a stack. I think it is still perfectly fine to use a linked list of environments for local variables) This is not really a complaint ... it just seemed to me like a missed opportunity to mention something interesting. :-)
- mangamadaiyan 6y agoPerhaps you meant "prescribe" instead of "proscribe"? :)
- Someone 6y agoYos :). Fixed. Thanks.
- userbinator 6y agoTry to name one processor with an upward-growing stack... I bet most people who are otherwise familiar with this material can't. (I'm not saying you're wrong --- I personally know two ;-)
- 6y ago
- staycoolboy 6y agoI think this is a smart way to introduce C.
- exmadscientist 6y agoThe use of AT&T syntax here is unfortunate. When the processor documentation and every single other toolchain out there uses a different syntax, you should use that syntax too. EDIT: Would any of the people downvoting this comment care to explain their affection for this historical mistake? I have never understood why anyone would choose it.
- jventura 6y agoI do agree with you on this! I tried to follow the tutorial, but it is not easy for me to decipher the syntax without checking line by line. Maybe I'll come back to this eventually, but would prefer Intel syntax for readability..
- hootbootscoot 6y agoI don't agree with you, but I find it rather absurd that your comment not only got downvoted, but flagged too! Weaponized comment flagging gone awry. but yes, Gnu tools are ubiquitous and will continue to be so.
- rrmm 6y agoI've been seeing some odd down vote choices too. Maybe there's just a lot of tension in the air for people these days?
- amedvednikov 6y agoAgreed, Intel syntax is much more readable.
- colejohnson66 6y agoAs an example, when I first looked at AT&T syntax, I saw movl 8(%ebx,%eax,2), %eax What does the 8 mean? How does the stuff in the parenthesis work? Why is there a type suffix? Here’s a general definition of AT&T syntax’s indirect form segment:offset(base,index,scale) But the equivalent in Intel syntax (for the above two is): mov eax, [eax*2+ebx+8] segment:[index*scale+base+offset] Idk, but the Intel syntax is just clearer to me. eax = *(eax*2+ebx+8); Like pseudo code almost. It honestly seems like AT&T syntax was created to facilitate easier parsing by computers, not humans.
- freecodyx 6y agoi learned those common patterns by using godbolt.org
- pjmlp 6y agowhile very educative, it just teaches one possible translation from C. Lets pick the "Translation of Arithmetic Operations" example and convert it to float instead of int int a = 2; int b= 3; int c = 24; a = a + b; a = a + b * c; http://www.avabodh.com/cin/arithmeticop.html http://www.avabodh.com/cin/arithmeticop.html And pack it into a function so that Golbolt can compile it. float hn_demo(void) { float a = 2; float b = 3; float c = 24; a = a + b; a = a + b * c; return a; } And now pick a CPU that isn't that good with floating point, like AVR, using GCC 4.5.4, and we get: ldi r24,lo8(0x40000000) ldi r25,hi8(0x40000000) ldi r26,hlo8(0x40000000) ldi r27,hhi8(0x40000000) std Y+1,r24 std Y+2,r25 std Y+3,r26 std Y+4,r27 ldi r24,lo8(0x40400000) ldi r25,hi8(0x40400000) ldi r26,hlo8(0x40400000) ldi r27,hhi8(0x40400000) std Y+5,r24 std Y+6,r25 std Y+7,r26 std Y+8,r27 ldi r24,lo8(0x41c00000) ldi r25,hi8(0x41c00000) ldi r26,hlo8(0x41c00000) ldi r27,hhi8(0x41c00000) std Y+9,r24 std Y+10,r25 std Y+11,r26 std Y+12,r27 ldd r22,Y+1 ldd r23,Y+2 ldd r24,Y+3 ldd r25,Y+4 ldd r18,Y+5 ldd r19,Y+6 ldd r20,Y+7 ldd r21,Y+8 rcall __addsf3 mov r27,r25 mov r26,r24 mov r25,r23 mov r24,r22 std Y+1,r24 std Y+2,r25 std Y+3,r26 std Y+4,r27 ldd r22,Y+5 ldd r23,Y+6 ldd r24,Y+7 ldd r25,Y+8 ldd r18,Y+9 ldd r19,Y+10 ldd r20,Y+11 ldd r21,Y+12 rcall __mulsf3 mov r27,r25 mov r26,r24 mov r25,r23 mov r24,r22 mov r18,r24 mov r19,r25 mov r20,r26 mov r21,r27 ldd r22,Y+1 ldd r23,Y+2 ldd r24,Y+3 ldd r25,Y+4 rcall __addsf3 mov r27,r25 mov r26,r24 mov r25,r23 mov r24,r22 std Y+1,r24 std Y+2,r25 std Y+3,r26 std Y+4,r27 Which includes calls to a floating point emulation library and quite different from the x86 example, with numbers using multiple registers. So more of an heads up, sometimes the C translation to Assembly isn't as direct as one might think.
- hoseja 6y agoPerhaps more telling/less nit-pickable is when you modify the function to be float hn_demo(float a, float b, float c) { a = a + b; a = a + b * c; return a; } and let the compiler actually optimize with -02.
- _kst_ 6y agohttp://www.avabodh.com/cin/programstructure.html http://www.avabodh.com/cin/programstructure.html ... void main() ... That's where I stopped reading. Since this comment is getting downvotes, I'll explain. The "void" keyword was introduced in the 1989 ANSI C standard. That same standard specified that the valid definitions for the main functions are: int main(void) { /* ... */ } and int main(int argc, char *argv[]) { /* ... */ } or equivalent (or implementations can support other forms). There has never been a version of C in which "void main()" is a valid way to define the main function. It's a small detail, and yes, many compilers will let you get away with it (the language doesn't require a diagnostic), but anyone writing about C should be aware of this, and should set a good example by writing correct code. Maybe the site is OK other than that, but it doesn't inspire confidence.
- DoingIsLearning 6y agoJust for some context, the standard specifies an int return type for the purpose of returning a meaningful value back to a hosting operating system. However, in the specific context of stand-alone or freestanding programs, the "void main()" definition would be absolutely nonconsequential, since there is no host to return a value to.
- tom_mellior 6y ago> However, in the specific context of stand-alone or freestanding programs, the "void main()" definition would be absolutely nonconsequential One possible consequence I can think of: If your program is safety-critical (or even otherwise) you might be interested in running static analysis or verification tools on it. These tools implement the C standard, so they would emit a diagnostic on the use of void main. Other than that, sure, the language police will not come and break down your door. If your compiler's docs say that it accepts this construct with the meaning you want, it is indeed an inconsequential, though also completely unnecessary, deviation from the standard.
- _kst_ 6y agoFor a freestanding implementation, the program entry point is entirely implementation-defined. It may or may not be called "main". In that kind of environment, "void main()" (or, better, "void main(void)") might be correct. You need to read the implementation's documentation. For hosted implementations, "void main()" might be valid, but "int main(void)" is always valid. A tutorial should not suggest "void main()" without mentioning any of this. Reference: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf sections 5.1.2.1 (freestanding environments) and 5.1.2.2.1 (hosted environments).