Skip to content

DQNEO/goas

Repository files navigation

goas - a port of GNU Assembler written in go

goas is an assembler that behaves like as, GNU Assembler.

This is just a toy program to illustrate how an assembler works. Actually I learned how an assembler works by writing this program :).

It does not mean to support all syntax or instructions, but Linux x86-64 AT&T syntax only. However, for any input it supports, it behaves exactly the same as as, which means it produces the very same binary files (*.o) as as does.

The most interesting thing is that it can assemble my Go compiler babygo. (You can see it by running make babygo.)

Requirements

You need a linux with gcc installed. If you are using MacOS or Windows, you can use my docker image to run goas.

$ docker run --rm -it -v `pwd`:/mnt/goas -w /mnt/goas dqneo/ubuntu-compiler-go bash

How to build

$ go build

How to use

Prepare a small assembly file test.s

.text
.global _start
_start:
  movq $42, %rdi # status
  movq $60, %rax # sys_exit
  syscall

And you can assemble it

$ ./goas -o test.o test.s
$ ld -o test test.o
$ ./test; echo $?
42

Demo

goas-min-demo

Supported Instructions

See test files under /t and /t2 directory to know what syntax it can assemble.

Design

goas is composed of 4 files.

File Role
parser.go parser
encoder.go instruction encoder
elf_writer.go ELF format writer
main.go miscellaneous tasks

Parser

parser.go is a simple recursive descent parser. The boundary between lexer nd parser are not clearly separated.

Each line of source code is converted into a statement object.

It produces a list of statements in the end.

Instruction encoder

encoder.go translates an instruction with operands into a piece of x86-64 binary machine code.

ELF format Writer

elf_writer.go composes an object which represents ELF file format and write it into a binary object file.

Test

$ docker run --rm -it -v `pwd`:/mnt/goas -w /mnt/goas dqneo/ubuntu-compiler-go make test

References

ELF

GNU Assembler

X86-64 Instruction set and encoding

License

MIT

Author

@DQNEO

About

port of GNU Assembler written in go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages