Ale*_*der 37
不支持内联汇编,但您可以使用C编译汇编代码,使用cgo编译和使用import "C"
,就像在gmp.go中一样.您也可以使用与Go直接兼容的程序集样式编写,例如asm_linux_amd64.s,它要求函数名称以"·"开头.
或者,你可以使用nasm和gccgo,这是我最喜欢的方式.(注意,Nasm似乎不支持以"·"开头的函数).
这是一个有效的"你好世界"的例子:
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
Run Code Online (Sandbox Code Playgroud)
main.go:
package main
func hello();
func main() {
hello()
hello()
}
Run Code Online (Sandbox Code Playgroud)
一个方便的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
Run Code Online (Sandbox Code Playgroud)
我hello()
在main.go中调用两次,只是为了仔细检查hello()是否正确返回.
请注意,直接调用中断80h在Linux上不被认为是好的样式,并且编写的调用函数C更具"未来证明".另请注意,这是专门针对64位Linux的程序集,并且不以任何方式,形状或形式独立于平台.
我知道这不是你问题的直接答案,但这是我所知道的最简单的使用Go组装的路线,缺乏内联.如果您确实需要内联,则可以编写一个脚本,从源文件中提取内联汇编,并按照上述模式进行准备.足够近?:)
Go,C和Nasm的简单示例:gonasm.tgz
更新:更高版本的gccgo需要-g标志,只需要"main.hello"而不是"go.main.hello".以下是Go,C和Yasm的更新示例:goyasm.tgz
kri*_*anp 13
不,你不能,但通过使用go编译器很容易提供一个函数的汇编实现.无需使用"导入C"来使用程序集.
看看数学库中的一个例子:
http://golang.org/src/pkg/math/abs.go:Abs函数在此go文件中声明.(此文件中还有一个abs的实现,但由于它具有小写名称,因此不会导出.)
package math
// Abs returns the absolute value of x.
//
// Special cases are:
// Abs(±Inf) = +Inf
// Abs(NaN) = NaN
func Abs(x float64) float64
Run Code Online (Sandbox Code Playgroud)
然后,在http://golang.org/src/pkg/math/abs_amd64.s中,在此文件中为英特尔64位实现了Abs:
// 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
Run Code Online (Sandbox Code Playgroud)