在Go代码中是否可以包含内联汇编?
这篇博客文章展示了将Go编译为单独的.s
文件并编辑它,但不支持像许多C编译器那样将内联汇编作为Go函数的一部分。
在Go代码中是否可以包含内联汇编?
这篇博客文章展示了将Go编译为单独的.s
文件并编辑它,但不支持像许多C编译器那样将内联汇编作为Go函数的一部分。
目前Go语言不支持内联汇编,但是你可以通过C链接汇编代码,借助cgo和import "C"
编译,就像在 gmp.go中所示。或者,你也可以使用直接与Go兼容的汇编风格,如 asm_linux_amd64.s所示。这需要函数名以"·"开头。
另外,你也可以使用nasm和gccgo,这是我目前最喜欢的方式。(请注意,Nasm似乎不支持以"·"开头的函数)。
这里有一个可行的“hello world”示例:
hello.asm:
; Based on hello.asm from nasm
SECTION .data ; data section
msg: db "Hello World",10 ; the string to print, 10=cr
len: equ $-msg ; "$" means "here"
; len is a value, not an address
SECTION .text ; code section
global go.main.hello ; make label available to linker (Go)
go.main.hello:
; --- setup stack frame
push rbp ; save old base pointer
mov rbp,rsp ; use stack pointer as new base pointer
; --- print message
mov edx,len ; arg3, length of string to print
mov ecx,msg ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write sysout command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
; --- takedown stack frame
mov rsp,rbp ; use base pointer as new stack pointer
pop rbp ; get the old base pointer
; --- return
mov rax,0 ; error code 0, normal, no error
ret ; return
main.go:
package main
func hello();
func main() {
hello()
hello()
}
还有一个方便的Makefile:
main: main.go hello.o
gccgo hello.o main.go -o main
hello.o: hello.asm
nasm -f elf64 -o hello.o hello.asm
clean:
rm -rf _obj *.o *~ *.6 *.gch a.out main
我在main.go中调用了hello()
两次,只是为了确认hello()是否正确返回。
请注意,在Linux上直接调用interrupt 80h不被认为是良好的编程风格,调用用C编写的函数更加“具有未来性”(future proof)。此外,请注意,这是专门针对64位Linux的汇编语言,不以任何方式、形式为跨平台设计。
我知道这不是你问题的直接答案,但这是我所知道的使用Go与汇编结合的最简单路径,如果没有内联的话。如果您真的需要内联,可以编写一个提取源文件中内联汇编并按照上述模式准备它的脚本。够接近了吗? :)
Go、C和Nasm的快速示例: gonasm.tgz
更新:后续版本的gccgo需要-g标志,仅需要“main.hello”而不是“go.main.hello”。以下是Go、C和Yasm的更新示例:goyasm.tgz
package math
// Abs returns the absolute value of x.
//
// Special cases are:
// Abs(±Inf) = +Inf
// Abs(NaN) = NaN
func Abs(x float64) float64
然后,在http://golang.org/src/pkg/math/abs_amd64.s中,这个文件实现了Intel 64位的Abs函数:
#include "textflag.h"
// func Abs(x float64) float64
TEXT ·Abs(SB),NOSPLIT,$0
MOVQ $(1<<63), BX
MOVQ BX, X0 // movsd $(-0.0), x0
MOVSD x+0(FP), X1
ANDNPD X1, X0
MOVSD X0, ret+8(FP)
RET
这种汇编函数的一个问题是,它们不会被 Go 编译器内联,因此如果您多次调用小函数,性能提升的限制是有限的。在 Go 库中,abs 函数不再使用汇编实现。我认为随着内联技术的改进,近期的 Go 版本中,编译 abs 函数时不使用汇编更快。
_amd64.s:2: illegal or missing addressing mode for symbol NOSPLIT
- Sebi2020当然,在Go语言本身中并没有任何东西会阻止其他Go语言实现(即:其他Go编译器)支持内联汇编。内联汇编是一个特定于编译器的决策 - 它与Go语言本身无关。
在任何情况下,内联汇编都是不安全的,因为它不能通过Go的类型系统进行正确性检查。似乎更好的方法是在类似C的语言中实现需要使用内联汇编的任何函数,并从Go中调用C函数。