Skip to content

Conversation

@GasInfinity
Copy link
Contributor

This adds support for x86_16, a.k.a IA-16, a.k.a i86 via CBE targeting the IA-16 fork of GCC.

Primarily targeted for freestanding and DOS programs (other). Support for far pointers should be done in a follow-up PR as it involves lowering them in CBE.

As usize is 16-bits I had to add some explicit @intCasts in compiler_rt, I'm sure a lot of std code will have the same issue (#10669).

Here's a snippet I tested in DOSBOX:

// zig build-exe -target x86_16-other main.zig -OReleaseFast -ofmt=c
// ia16-elf-gcc main.c -Os -o main.exe

export fn main(argc: usize, argv: [*]const [*:0]const c_char) callconv(.c) u8  {
    _ = argv;
    var fmt_buf: [1024]u8 = undefined;
    dosPrintString(std.fmt.bufPrintZ(&fmt_buf, "Hello DOS from zig using int 21H (AH = 02H, a.k.a character output)!\nGot {} arguments!\n", .{
        argc,
    }) catch unreachable);
    return 0;
}

fn dosPrintString(str: []const u8) void {
    for(str) |c| {
        asm volatile("int $0x21" :: [code] "{ah}" (@as(u8, 2)), [character] "{dl}" (@as(u8, c)) : .{
            .cc = true,
            .al = true,
        });
    }
}

const std = @import("std");

Partially addresses #7469

@alexrp alexrp self-assigned this Oct 24, 2025
Comment on lines +2274 to +2275
// Technically x86 can use segmentation...
.far => (arch == .x86_16),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the question is what compilers support, not what the architecture supports.

Which might actually be an argument for moving supportsAddressSpace to std.zig.target... but that's a problem for another day.

Comment on lines +530 to +535
// x86_16 extra address spaces.
/// Allows addressing the entire address space by storing both segment and offset.
far,
Copy link
Member

@alexrp alexrp Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have precedent for addrspace affecting pointer size; we kind of just assume that all pointers have the same size. That's wrong for many reasons and will have to change one way or another, but just so you know, here be dragons.

@alexrp

This comment was marked as resolved.

myclevorname added a commit to myclevorname/zig that referenced this pull request Oct 25, 2025
@alexrp
Copy link
Member

alexrp commented Oct 27, 2025

This looks good to me now.

Just to keep history clean, would you mind rewriting this into 4 commits:

  • One that adjusts the intByteSize return type
  • One that makes the adjustments needed for 16-bit usize (compiler-rt, std.Io.Writer, std.os.windows)
  • One that adds 16-bit x86 support in zig.h
  • One that adds all the target information, calling conventions, etc

@alexrp alexrp merged commit 06d9e3b into ziglang:master Oct 28, 2025
7 of 9 checks passed
@alexrp alexrp removed their assignment Oct 28, 2025
myclevorname added a commit to myclevorname/zig that referenced this pull request Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants