Last active
March 4, 2023 04:18
-
-
Save g-cassie/71365ed67f1ee99700decaccad551b8f to your computer and use it in GitHub Desktop.
Computer Enhance Part 1 in Zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I am a complete Zig noob so please be kind! | |
const std = @import("std"); | |
pub fn getName(addr: u8, w: bool, dest: []u8) void { | |
if (w) { | |
std.mem.copy(u8, dest, switch (addr) { | |
0b00_000_000 => "ax", | |
0b00_000_001 => "cx", | |
0b00_000_010 => "dx", | |
0b00_000_011 => "bx", | |
0b00_000_100 => "sp", | |
0b00_000_101 => "bp", | |
0b00_000_110 => "si", | |
0b00_000_111 => "di", | |
else => unreachable, | |
}); | |
} else { | |
std.mem.copy(u8, dest, switch (addr) { | |
0b00_000_000 => "al", | |
0b00_000_001 => "cl", | |
0b00_000_010 => "dl", | |
0b00_000_011 => "bl", | |
0b00_000_100 => "ah", | |
0b00_000_101 => "ch", | |
0b00_000_110 => "dh", | |
0b00_000_111 => "bh", | |
else => unreachable, | |
}); | |
} | |
} | |
pub fn main() !void { | |
var file = try std.fs.cwd().openFile("listing_0038_many_register_mov", .{}); | |
defer file.close(); | |
var buf_reader = std.io.bufferedReader(file.reader()); | |
var in_stream = buf_reader.reader(); | |
var buf: [2]u8 = undefined; | |
while (try in_stream.read(&buf) > 0) { | |
//std.debug.print("{b} {b}\n", .{ buf[0], buf[1] }); | |
var instr = [_]u8{ 0, 0, 0 }; | |
std.mem.copy(u8, &instr, switch (buf[0] & 0b111111_0_0) { | |
0b100010_00 => "mov", | |
else => unreachable, | |
}); | |
var d: bool = buf[0] & 0b000000_1_0 > 0; | |
var w: bool = buf[0] & 0b000000_0_1 > 0; | |
//var mod: u8 = buf[1] & 0b11_000_000; | |
var pos1: u8 = (buf[1] & 0b00_111_000) >> 3; | |
var pos2: u8 = buf[1] & 0b00_000_111; | |
// if d, pos1 is the source and pos2 is the destination | |
var name1 = [_]u8{ 0, 0 }; | |
var name2 = [_]u8{ 0, 0 }; | |
getName(if (d) pos1 else pos2, w, &name1); | |
getName(if (d) pos2 else pos1, w, &name2); | |
// I was too lazy to figure out how to write to a file in zig | |
std.debug.print("{s} {s}, {s}\n", .{ instr, name1, name2 }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment