Zig Bare Bones: Difference between revisions

m
fix syntax highlighting error
[unchecked revision][unchecked revision]
m (Bot: Replace deprecated source tag with syntaxhighlight)
m (fix syntax highlighting error)
 
(6 intermediate revisions by 2 users not shown)
Line 14:
 
=== build.zig ===
<syntaxhighlight lang="czig">
const std = @import("std");
const Builder = @import("std").build.Builder;
Line 97:
 
=== src/main.zig ===
<sourcesyntaxhighlight lang="czig">
const console = @import("console.zig");
 
Line 129:
console.puts("Hello world!");
}
</syntaxhighlight>
</source>
 
=== src/console.zig ===
<sourcesyntaxhighlight lang="czig">
const fmt = @import("std").fmt;
const Writer = @import("std").io.Writer;
Line 217:
fmt.format(writer, format, args) catch unreachable;
}
</syntaxhighlight lang="c">
</source>
 
=== src/linker.ld ===
A [[linker script]] is also needed. This file tells the linker to place our code at the base address of 1M. In general user-space programming, code are placed at much higher areas, but that requires [[virtual memory]] to be available, or else only few computers could provide such a large physical memory space.
<syntaxhighlight lang="asm">
 
We also asks the linker to place <code>.multiboot</code> section at first, because the Multiboot specification says that the Multiboot header must be at the first 8KiB of the kernel file.
 
<pre>
ENTRY(_start)
Line 247 ⟶ 251:
}
}
</pre>
</syntaxhighlight>
 
=== src/grub.cfg ===
Finally, the last thing you need is a GRUB configuration, which tells the GRUB bootloader how to boot our kernel.
<syntaxhighlight lang="c">
 
<code>menuentry</code> adds a menu entry to the screen. When you press ENTER in the GRUB menu, GRUB will run the commands in the menu block.
 
<code>multiboot</code> asks GRUB to load our kernel from <code>/boot/kernel.elf</code>, and GRUB automatically runs <code>boot</code> after the menu block, which will fire the boot.
 
<syntaxhighlight lang="asmunixconfig">
menuentry "Zig Bare Bones" {
multiboot /boot/kernel.elf
Line 257 ⟶ 267:
 
== Build ==
Now that our kernel code is done, we'll now build our kernel by running the command below:
the command below:
 
<syntaxhighlight lang="bash">