Emitting Assembly

What is the best way to see assembly from Zig?

I’m new to Zig so this may be a simple oversight on my part. I have read quite a few articles/threads about it as well as the source code. At least some of the material was probably pretty close but seems to be outdated compared with the latest dev builds, and I wasn’t able to determine the new equivalents to previous approaches by reading the standard lib.

When I tried using -femit-asm via CLI (like here), Zig didn’t resolve imported libraries correctly in the build. I’m not sure that I was formatting the command correctly though (in terms of providing additional arguments to correctly resolve the deps. I have a build.zig that does link the deps and builds fine, but I’m not sure how to bridge the gap between what I’ve got in my build.zig and what I’m trying to do on the CLI).

I’m basically grasping at straws and figured someone here can point me in the right direction. Any help is greatly appreciated! :slight_smile:

Welcome Selkie!

Personally when I want to look at assembly I either just use a debugger and set a breakpoint and let the debugger show me code/assembly or I use https://zig.godbolt.org/.

You also probably could use a general disassembler program or reverse engineering tools to just look at the executable directly, but I don’t have a lot of experience with those. I have used this a tiny bit and it was pretty nice GitHub - radareorg/iaito: Official QT frontend of radare2, it can probably do way more then I am aware of. (It seems GitHub - rizinorg/cutter: Free and Open Source Reverse Engineering Platform powered by rizin is a fork of it, but I don’t know which one is better / actively developed, I have only used iaito via extra/iaito package on manjaro, but I think both of them are available for other platforms)

You can use zig build <step> --verbose to see the commands and then remove the --listen=- and add -femit-asm to the build-exe command. Doing this it created two files in my directory <exe>.o and <exe>.s the .s file contains the readable asm style print.

I think I would prefer a tool like iaito, because with that it can help you jumping around in the code and visualize/show connections. Apparently it also has plugins, so who knows what else might be possible.

1 Like

Thank you, I used your instructions on the CLI commands and was able to get the .s file. That’s definitely miles ahead of where I was, already! I’ll look into some of the other options and tools that you mentioned as well.

I will also say re the debugger, I’m using cppvsdbg with VSCode and I don’t see any assembly, maybe I’m not configured correctly or it’s just not great through VSCode. What debugger are you using?

1 Like

For vscode disassembly, right click in you zig file and select “Open Disassembly View”.
I am using lldb from extension: “CodeLLDB”

1 Like