...
1 package golangasm
2
3 import (
4 "bytes"
5 "testing"
6
7 "github.com/twitchyliquid64/golang-asm/obj"
8 "github.com/twitchyliquid64/golang-asm/obj/x86"
9 )
10
11 func noop(builder *Builder) *obj.Prog {
12 prog := builder.NewProg()
13 prog.As = x86.ANOPL
14 prog.From.Type = obj.TYPE_REG
15 prog.From.Reg = x86.REG_AX
16 return prog
17 }
18
19 func addImmediateByte(builder *Builder, in int32) *obj.Prog {
20 prog := builder.NewProg()
21 prog.As = x86.AADDB
22 prog.To.Type = obj.TYPE_REG
23 prog.To.Reg = x86.REG_AL
24 prog.From.Type = obj.TYPE_CONST
25 prog.From.Offset = int64(in)
26 return prog
27 }
28
29 func movImmediateByte(builder *Builder, reg int16, in int32) *obj.Prog {
30 prog := builder.NewProg()
31 prog.As = x86.AMOVB
32 prog.To.Type = obj.TYPE_REG
33 prog.To.Reg = reg
34 prog.From.Type = obj.TYPE_CONST
35 prog.From.Offset = int64(in)
36 return prog
37 }
38
39 func movMemByte(builder *Builder, intoReg, memReg int16) *obj.Prog {
40 prog := builder.NewProg()
41 prog.As = x86.AMOVB
42 prog.To.Type = obj.TYPE_REG
43 prog.To.Reg = intoReg
44 prog.From.Type = obj.TYPE_MEM
45 prog.From.Reg = memReg
46 return prog
47 }
48
49 func TestBasic(t *testing.T) {
50 b, _ := NewBuilder("amd64", 64)
51 b.AddInstruction(noop(b))
52 b.AddInstruction(movImmediateByte(b, x86.REG_AL, 16))
53 b.AddInstruction(addImmediateByte(b, 16))
54 got, want := b.Assemble(), []byte{0x0f, 0x1f, 0xc0, 0xb0, 0x10, 0x04, 0x10}
55 if !bytes.Equal(got, want) {
56 t.Errorf("assembly = %v, want %v", got, want)
57 }
58 }
59
60 func TestMove(t *testing.T) {
61 b, _ := NewBuilder("amd64", 64)
62 b.AddInstruction(noop(b))
63 b.AddInstruction(movMemByte(b, x86.REG_AL, x86.REG_BX))
64 got, want := b.Assemble(), []byte{0x0F, 0x1F, 0xC0, 0x8A, 0x03}
65 if !bytes.Equal(got, want) {
66 t.Errorf("assembly = %v (%X), want %v", got, got, want)
67 }
68 }
69
View as plain text