Created
May 6, 2022 18:14
-
-
Save micahswitzer/0db7f825ee58e8f25911250296a2d90a to your computer and use it in GitHub Desktop.
This file contains 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
const std = @import("std"); | |
fn comptimeBytes(comptime name: []const u8, comptime instructions: []const u8) []const u8 {\ | |
// won't compile with ".section .bss\n" | |
asm volatile (".global " ++ name ++ ", " ++ name ++ "_end\njmp " ++ name ++ "_end\n" ++ name ++ ":\n" ++ instructions ++ "\n" ++ name ++ "_end:"); | |
const start = @extern(*const u8, .{ .name = name }); | |
const end = @extern(*const u8, .{ .name = name ++ "_end" }); | |
const size = @ptrToInt(end) - @ptrToInt(start); | |
var result: []const u8 = undefined; | |
result.ptr = @ptrCast([*]const u8, start); | |
result.len = size; | |
return result; | |
} | |
// Type your code here, or load an example. | |
pub fn main() !void { | |
const syscall = comptimeBytes("syscall_bytes", "syscall"); | |
const xor_rax = comptimeBytes("xor_rax", "xor %%rax, %%rax"); | |
const out = std.io.getStdOut().writer(); | |
try out.print("syscall: {}\nxor rax: {}", .{ | |
std.fmt.fmtSliceHexLower(syscall), | |
std.fmt.fmtSliceHexLower(xor_rax), | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment