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

hello()在main.go中打了两次电话,只是要仔细检查一下hello()是否正确返回。

请注意,在Linux上,直接调用中断80h并不被认为是一种好的方式,而调用C语言编写的函数则更“面向未来”。还要注意,这是专门用于64位Linux的程序集,并且在任何方面,形状或形式均与平台无关。

我知道这不是您问题的直接答案,但这是我知道在缺少内联的情况下将Go与汇编结合使用的最简单方法。如果确实需要内联,则可以编写一个脚本从源文件中提取内联程序集,并按照上述模式进行准备。足够近?:)

Go,C和Nasm的快速示例:gonasm.tgz

gccgo的更高版本需要-g标志,并且仅需要“ main.hello”而不是“

go.main.hello”。这是Go,C和Yasm的更新示例:goyasm.tgz

以上是 Go代码中可以包含内联汇编吗? 的全部内容, 来源链接: utcz.com/qa/417584.html

回到顶部