3 ms·
C is the 'portable assembly' of the world. I find it easiest when learning some CPU architecture to compile very simple C programs and then see what the resulti
by MichaelBurge 11y ago
C is the 'portable assembly' of the world. I find it easiest when learning some CPU architecture to compile very simple C programs and then see what the resulting assembly looks like.
Some questions I look at:
* When I cast a float to an int, what actually happens behind the scenes? Sometimes there are floating point instructions that get generated. Sometimes the CPU doesn't have floating points, so it emits calls to a 'float2int()' function that implements it in software.
* What is the calling convention? Just write a function with 1,2,3,4 and 20 arguments and do something like add them together. Then see how the resulting function handles those arguments.
* What do some simple array accesses or pointer arithmetic generate?
* How are large structs passed to functions at higher optimization levels?
* If I write a loop to compute a dot product, do higher optimization levels emit vectorized instructions?
You can actually go pretty far with this. Just use tools like objdump or gcc to probe or create object files, and then start writing assembly once you understand what C code you'd write and what it would compile down to.