2023-12-09 03:12:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-11 00:14:13 +00:00
|
|
|
// "io/ioutil"
|
|
|
|
"os"
|
2023-12-09 03:12:45 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompile(t *testing.T) {
|
|
|
|
|
|
|
|
// @ref https://gist.github.com/armicron/e891709ce8893df2fd5fc74c846dcf20
|
|
|
|
const src = `
|
2023-12-11 00:14:13 +00:00
|
|
|
section .rodata
|
2023-12-09 03:12:45 +00:00
|
|
|
$msg = sz "Hello, world\n"
|
|
|
|
$filename = sz "test.txt"
|
2023-12-11 00:14:13 +00:00
|
|
|
|
|
|
|
section .data
|
2023-12-09 03:12:45 +00:00
|
|
|
$fd = u64 0
|
|
|
|
|
|
|
|
section .text
|
|
|
|
|
|
|
|
global _start: ;tell linker entry point
|
|
|
|
mov rdi, &$filename
|
|
|
|
mov rsi, 66 ;O_CREAT = 0102o (man open)
|
|
|
|
mov rdx, 438 ;umode_t = 0666 octal
|
|
|
|
mov rax, 2
|
|
|
|
syscall
|
|
|
|
|
|
|
|
mov $fd, rax
|
2023-12-11 00:14:13 +00:00
|
|
|
mov rdx, strlen($msg) ;message strlen
|
2023-12-09 03:12:45 +00:00
|
|
|
mov rsi, &$msg ;message to write
|
|
|
|
mov rdi, $fd ;file descriptor
|
|
|
|
mov rax, 1 ;system call number (sys_write)
|
|
|
|
syscall ;call kernel
|
|
|
|
|
|
|
|
mov rdi, $fd
|
|
|
|
mov rax, 3 ;sys_close
|
|
|
|
syscall
|
|
|
|
|
|
|
|
mov rax, 60 ;system call number (sys_exit)
|
|
|
|
syscall ;call kernel
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2023-12-11 00:14:13 +00:00
|
|
|
/*
|
|
|
|
assemble(strings.NewReader(src), ioutil.Discard)
|
|
|
|
*/
|
|
|
|
|
|
|
|
fh, err := os.OpenFile("output.o", os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assemble(strings.NewReader(src), fh) // ioutil.Discard)
|
2023-12-09 03:12:45 +00:00
|
|
|
|
|
|
|
}
|