I wanted to write a regex crossword solver for fun in Zig, and so I’ve been doing all sorts of wacky stuff with like using a u256 to represent the set of candidates for a particular character.
So, I was curious about how this is actually implemented. For example, if I wrote:
const std = @import("std");
pub noinline fn foobarbaz() void {
const x: u1024 = 1 << 1021;
const y: u1024 = 1 << 1022;
const z = x + y;
std.debug.print("{}\n", .{z});
}
pub fn main() void {
foobarbaz();
}
What would the assembly look like? I am not an assembly pro, but I would guess that we would “somehow allocate 1024 bits on the stack for x,y,z” and “somehow add them” but the “somehow add them” is the part I want to actually try and understand!
So with some help from the Zig discord I built my file like this:
zig build-obj main.zig -fstrip -O ReleaseSmall -femit-asm=main.a
and when I look at the assembly I see
main.foobarbaz:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset rbp, -16
mov rbp, rsp
.cfi_def_cfa_register rbp
push r15
push r14
push r13
push r12
push rbx
sub rsp, 2024
.cfi_offset rbx, -56
.cfi_offset r12, -48
.cfi_offset r13, -40
.cfi_offset r14, -32
.cfi_offset r15, -24
lea rbx, [rbp - 960]
lea rsi, [rbp - 1024]
push 64
pop rdx
mov rdi, rbx
call debug.lockStderr
mov r15, qword ptr [rbx]
movabs rax, 6917529027641081856
mov r10d, 1025
xor edi, edi
mov qword ptr [rbp - 144], 0
mov qword ptr [rbp - 200], 0
mov qword ptr [rbp - 176], 0
...omitting the rest
now, I look at this and the only part I recognize is push rbp and move rbp, rsp ![]()
I don’t know if staring at this assembly and picking it apart is going to be useful, so I wanted to ask:
- Does anyone know how Zig implements this? Does it rely on LLVM to “do the hard part” (I found this one link Arbitrary bit width integers - #7 by Sandro_Magi - LLVM Dev List Archives - LLVM Discussion Forums )
- Is staring at this assembly actually going to be useful Maybe it’s not as scary as I think and there is a method to the madness, but seeing
movabs rax, 6917529027641081856spooks me.