Created
November 22, 2024 16:26
-
-
Save lantos1618/04b9fca402f101709d568f555efc707e to your computer and use it in GitHub Desktop.
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
| Program { | |
| instructions: vec<Instruction> | |
| } | |
| main() { | |
| program $= Program( | |
| Instructions: MACOS_ARM64 | |
| ) | |
| // cat program.s && as -o program.o program.s -arch arm64 --target=arm64-apple-macos11.0 && ld -o program program.o -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lc -lSystem -macos_version_min 11.0 -e _start && ./program | |
| program.add(".global _start").comment("Define the global entry point"); | |
| program.add(".extern _printf").comment("Declare the external function"); | |
| program.add(".extern _exit").comment("Declare the _exit function (underscore required for macOS)"); | |
| data_section := program.section("__DATA", "__data") | |
| data_section.add("msg: .asciz \"Hello, world!\\n\"") | |
| text_section := program.section("__TEXT", "__text") | |
| text_section.add("_start: adrp x0, msg@PAGE").comment("Load the page of 'msg' into x0"); | |
| text_section.add("add x0, x0, msg@PAGEOFF").comment("Add the page offset of 'msg' to x0"); | |
| text_section.add("bl _printf").comment("Call printf"); | |
| text_section.add("mov x0, #0").comment("Exit status 0"); | |
| text_section.add("bl _exit").comment("Call _exit"); | |
| obj_file = program.compile( | |
| asm_file: Path::new("program.s"), | |
| obj_file: Path::new("program.o"), | |
| bin_file: Path::new("program"), | |
| link: LinkOptions ( | |
| ld_flags: ["-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib", "-lc", "-lSystem", "-macos_version_min 11.0", "-e _start"], | |
| ) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment