3 回答

TA貢獻1890條經驗 獲得超9個贊
不支持內聯匯編,但是您可以鏈接通過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
- 3 回答
- 0 關注
- 368 瀏覽
添加回答
舉報