Categorygithub.com/akyoto/asm
repositorypackage
0.5.0
Repository: https://github.com/akyoto/asm.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

asm

Godoc Report Tests Coverage Sponsor

An x86-64 assembler written in Go. It is used by the Q programming language for machine code generation.

It was born out of the need for a highly performant assembler that had next to no overhead when compiling instructions to bytecode. When I started this project, I had no idea of how x86 instructions were constructed and I was forced to do a lot of reverse engineering via NASM.

Hopefully this repository helps someone who is learning about x86 assembly and its bytecode format.

There are a few examples to get an idea of the API I was aiming for.

x86-64 bytecode

An x86-64 program consists of a list of instructions. All of these instructions are built with the following format:

NameSize in bytesRequired?
Legacy prefixes1-4
OP code1-4required
Mod/RM1
SIB1
Displacement1-8
Immediate1-8

Out of these only the actual OP code which decides the instruction to execute is required. The remaining components depend on what instruction and what kind of parameters you have.

The maximum size for a single instruction is limited to 15 bytes.

Mod/RM

The Mod/RM byte has the following format:

NameSize in bits
Mod2
Reg3
RM3
(mod << 6) | (reg << 3) | rm

SIB

The SIB byte has the same format as Mod/RM, just with different meanings:

NameSize in bits
Scale2
Index3
Base3
(scale << 6) | (index << 3) | base

The opcode directory has a few helper functions to construct these components.

Registers

The following is a list of register names you can use. I decided to stick with the original names instead of r0-r7 for rax-rbp. I might still switch to r0-r7 for the future and enable the old names as synonyms.

64 bit32 bit16 bit8 bit
raxeaxaxal
rcxecxcxcl
rdxedxdxdl
rbxebxbxbl
rsiesisisil
rdiedididil
rspespspspl
rbpebpbpbpl
r8r8dr8wr8b
r9r9dr9wr9b
r10r10dr10wr10b
r11r11dr11wr11b
r12r12dr12wr12b
r13r13dr13wr13b
r14r14dr14wr14b
r15r15dr15wr15b

Resources