github.com/cloudwego/iasm@v0.2.0/x86_64/program_test.go (about)

     1  //
     2  // Copyright 2024 CloudWeGo Authors
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package x86_64
    18  
    19  import (
    20      `bytes`
    21      `testing`
    22  
    23      `github.com/cloudwego/iasm/expr`
    24      `github.com/davecgh/go-spew/spew`
    25  )
    26  
    27  func TestProgram_Assemble(t *testing.T) {
    28      a := CreateArch()
    29      b := CreateLabel("bak")
    30      s := CreateLabel("tab")
    31      j := CreateLabel("jmp")
    32      p := a.CreateProgram()
    33      p.JMP    (j)
    34      p.JMP    (j)
    35      p.Link   (b)
    36      p.Data   (bytes.Repeat([]byte { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }, 15))
    37      p.Data   ([]byte { 0x0f, 0x1f, 0x00 })
    38      p.JMP    (b)
    39      p.Link   (j)
    40      p.LEAQ   (Ref(s), RDI)
    41      p.MOVSLQ (Sib(RDI, RAX, 4, -4), RAX)
    42      p.ADDQ   (RDI, RAX)
    43      p.JMPQ   (RAX)
    44      p.Align  (32, expr.Int(0xcc))
    45      p.Link   (s)
    46      p.Long   (expr.Ref(s.Retain()).Sub(expr.Ref(j.Retain())))
    47      p.Long   (expr.Ref(s.Retain()).Sub(expr.Ref(b.Retain())))
    48      spew.Dump(p.AssembleAndFree(0))
    49  }