Skip to content

Instantly share code, notes, and snippets.

@ashafq
Last active December 8, 2016 01:36
Show Gist options
  • Save ashafq/e94701bd838eeaa2b9674cecc61f8db5 to your computer and use it in GitHub Desktop.
Save ashafq/e94701bd838eeaa2b9674cecc61f8db5 to your computer and use it in GitHub Desktop.
This is a simple example/tutorial on how to intermix assembly (x86_64) code with go.

The source files below should be arranged as:

./main.go
./sqrt/sqrt.s
./sqrt/sqrt.go

To run:

>> go build main.go
>> ./main
sqrt(2) = 1.4142135623730951
package main
import ("fmt"
"./sqrt")
func main() {
x := 2.0
fmt.Printf("sqrt(%v) = %v\n", x, sqrt.Sqrt(x));
}
package sqrt
/*
extern double as_sqrt(double);
*/
import "C"
func Sqrt(x float64) float64 {
return float64(C.as_sqrt(C.double(x)))
}
.text
.globl as_sqrt
.balign 16
.type as_sqrt, @function
as_sqrt:
sqrtsd %xmm0, %xmm0
retq
.size as_sqrt, .-as_sqrt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment