第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

可以在Go代碼中包含內(nèi)聯(lián)匯編嗎?

可以在Go代碼中包含內(nèi)聯(lián)匯編嗎?

Go
慕雪6442864 2021-04-09 13:15:26
可以在Go代碼中包含內(nèi)聯(lián)匯編嗎?
查看完整描述

3 回答

?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊

不支持內(nèi)聯(lián)匯編,但是您可以鏈接通過C用匯編語(yǔ)言編寫的代碼,使用cgo進(jìn)行編譯和使用import "C",例如gmp.go中的代碼。您也可以使用與Go直接兼容的匯編樣式來編寫,例如asm_linux_amd64.s中,它要求函數(shù)名稱以“·”開頭。


或者,您可以使用nasm和gccgo,這是我到目前為止最喜歡的方法。(請(qǐng)注意,Nasm似乎不支持以“·”開頭的功能)。


這是一個(gè)有效的“ 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()

}

還有一個(gè)方便的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中打了兩次電話,只是要仔細(xì)檢查一下hello()是否正確返回。


請(qǐng)注意,在Linux上,直接調(diào)用中斷80h并不是一種好的方式,而調(diào)用C語(yǔ)言編寫的函數(shù)則更“面向未來”。還要注意,這是專門用于64位Linux的程序集,并且在任何方式,形狀或形式上都不與平臺(tái)無關(guān)。


我知道這不是您問題的直接答案,但這是我知道在缺少內(nèi)聯(lián)的情況下將Go與匯編結(jié)合使用的最簡(jiǎn)單方法。如果確實(shí)需要內(nèi)聯(lián),則可以編寫一個(gè)腳本從源文件中提取內(nèi)聯(lián)程序集,并按照上述模式進(jìn)行準(zhǔn)備。足夠接近?:)


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


更新: gccgo的更高版本需要-g標(biāo)志,并且僅需要“ main.hello”而不是“ go.main.hello”。這是Go,C和Yasm的更新示例:goyasm.tgz


查看完整回答
反對(duì) 回復(fù) 2021-04-26
  • 3 回答
  • 0 關(guān)注
  • 369 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)