• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created almost 6 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The assembler from the Go compiler, in library form.

golang-asm

A mirror of the assembler from the Go compiler, with import paths re-written for the assembler to be functional as a standalone library.

License as per the Go project.

Status

Works, but expect to dig into the assembler godoc's to work out what to set different parameters of obj.Prog to get it to generate specific instructions.

Example

Demonstrates assembly of a NOP & an ADD instruction on x86-64.

package main

import (
	"fmt"

	asm "github.com/twitchyliquid64/golang-asm"
	"github.com/twitchyliquid64/golang-asm/obj"
	"github.com/twitchyliquid64/golang-asm/obj/x86"
)

func noop(builder *asm.Builder) *obj.Prog {
	prog := builder.NewProg()
	prog.As = x86.ANOPL
	prog.From.Type = obj.TYPE_REG
	prog.From.Reg = x86.REG_AX
	return prog
}

func addImmediateByte(builder *asm.Builder, in int32) *obj.Prog {
	prog := builder.NewProg()
	prog.As = x86.AADDB
	prog.To.Type = obj.TYPE_REG
	prog.To.Reg = x86.REG_AL
	prog.From.Type = obj.TYPE_CONST
	prog.From.Offset = int64(in)
	return prog
}

func movImmediateByte(builder *asm.Builder, reg int16, in int32) *obj.Prog {
	prog := builder.NewProg()
	prog.As = x86.AMOVB
	prog.To.Type = obj.TYPE_REG
	prog.To.Reg = reg
	prog.From.Type = obj.TYPE_CONST
	prog.From.Offset = int64(in)
	return prog
}

func main() {
	b, _ := asm.NewBuilder("amd64", 64)
	b.AddInstruction(noop(b))
	b.AddInstruction(movImmediateByte(b, x86.REG_AL, 16))
	b.AddInstruction(addImmediateByte(b, 16))
	fmt.Printf("Bin: %x\n", b.Assemble())
}

Working out the parameters of obj.Prog

This took me some time to work out, so I'll write a bit here.

Use these references

Instruction constants have a naming scheme

Instructions are defined as constants in the package for the relavant architecture, and have an 'A' prefix and a size suffix.

For example, the MOV instruction for 64 bits of data is AMOVQ (well, at least in amd64).

Search the go source for usage of a given instruction

For example, if I wanted to work out how to emit the MOV instruction for 64bits, I would search the go source on github for AMOVQ or x86.AMOVQ. Normally, you see find a few examples where the compiler backend fills in a obj.Prog structure, and you follow it's lead.

More Repositories

1

subnet

Simple, auditable & elegant VPN, built with TLS mutual authentication and TUN.
Go
1,058
star
2

usbd-hid

Rust
87
star
3

kcdb

Unofficial KiCad footprint database & indexer
Go
42
star
4

liquid-cad

A 2-d, constraint-solving CAD for rapid prototyping
Rust
31
star
5

pushtart

The worlds lightest PaaS host.
Go
11
star
6

seeed-erpc-rs

Drivers for Seeed Studio's eRPC-based wifi protocol
Rust
11
star
7

kicad2pcbshopper

Give it a KiCad PCB file, it will extract all the parameters and find the cheapest/best PCB manufacturers for you.
KiCad Layout
11
star
8

Micstream

A JS library for audio calls, using webSockets instead of webRTC.
Go
8
star
9

minikernel

Go
7
star
10

bob-the-builder

A very simple run tool with a user interface - intended for build automation - designed for home use on a raspberry pi with minimal resource usage.
Go
6
star
11

kcgen

Go library and tools for generating kicad footprints and libraries.
Go
6
star
12

subshard

Chrome based proxy, built with Golang & Python.
Go
5
star
13

gofi

Open-source implementation of a controller which is compatible with ubiquiti APs.
Go
5
star
14

raspberry-box

Makefiles for modifying a Raspberry Pi .img
Go
5
star
15

go-liquiddsp

Go
4
star
16

time2go

Our submission for GovHack 2016. time2go finds public transport for you, optimizing the route so you can hits as many pokestops as possible.
Python
3
star
17

pamtls

Authenticate/login to Linux machines by querying a JSON webservice.
Go
3
star
18

harsh

The beginnings of a generic AST, executor, visualizer, and go-parser.
Go
3
star
19

oww-rust-core

Rust
3
star
20

go-apriltag

Apriltag image recognition for Go.
C
3
star
21

nexus

Go
2
star
22

nsstls

NSS module to get user information from a JSON webservice.
Go
2
star
23

wifidec

Repository for scriptz playing around with decoding elements of the Wifi stack (mainly Radiotap and 802.11 frames)
Python
2
star
24

CNC

Generic architecture 4 command and control for distributed organisations.
Go
2
star
25

nugget

Building a network filesystem - an exercise in learning how FUSE works.
Go
2
star
26

fz-rust

A demo compiling the flipperzero-hello-rust plugin without the firmware toolchain
Rust
2
star
27

babysitter

Simple system service to keep daemons running.
Go
2
star
28

archive

Where old/crappy repos go - to avoid github clutter
Go
1
star
29

R2K9

Browser based parametric CAD
JavaScript
1
star
30

esp32-riscv-emu

C++
1
star
31

kan

Rust
1
star
32

rwords

Randomly generate english words.
Go
1
star
33

rnd

Go
1
star
34

liquid-launcher

Rust
1
star
35

debdep

Go
1
star