Studying assembly output

After a few excellent weeks playing with zig, I feel like going one step further and looking at assembly output, comparing design choices.

Do you have suggestions for tools and workflows ? I’ve used godbolt a bit, but it is really slow and rather tedious to use on my small laptop screen …

Well zig build-exe, zig build-lib, etc have -femit-asm which is pretty useful.

Yes, I tried that yesterday but I got lost in the huge amount of code generated. Are you using tools to make the analysis easier ? For example matching a function with its aim counterpart ?

As an option:

zig build-exe file.zig -fstrip
objdump -d file > file.s

Below is a comparison of output files’ sizes (in bytes)
depending on compilation options (just out of curiosity):

                                          h       h.s
-femit-asm -O ReleaseSafe          : 2_030_376 5_784_599
-femit-asm                         : 1_926_728 5_696_145
-femit-asm -O ReleaseFast          : 1_787_672 4_473_742
-femit-asm -fstrip                 :    53_912   320_732
-femit-asm -O ReleaseSafe  -fstrip :    17_048    60_478
-femit-asm -O ReleaseFast  -fstrip :    12_952    27_023
-femit-asm -O ReleaseSmall         :     8_856    32_355
-femit-asm -O ReleaseSmall -fstrip :     8_856    32_355

h.zig:

const std = @import("std");
pub fn main() void {
    std.debug.print("Hello!\n", .{});
}

h - executable
h.s - asm

2 Likes

Note that strip is enabled by default for -OReleaseSmall, and you can use -fno-strip in combination with -OReleaseSmall.

3 Likes

I usually compile with ReleaseFast, with .pdb enabled, and put a breakpoint in the function that I want to analyze. Depending on what it is you’re trying to examine, ReleaseSmall might be better, simply because the code is smaller, but then you have to take into consideration what Andrew mentioned above.
I usually do this with VSCode, but binary debugging is an afterthought there, it’s not very ergonomic. When I need to get serious with disassembly, I use x64dbg. It’s intended for debugging with no source code, so the way it displays binary information is excellent.

2 Likes