Go Assembly by Example: Hello

The following example is a bad example. It was obtained by using the following command on a simple Go program printing the string “hello world”:

  1. go tool compile -S helloworld.go
  1. package main
  2. //The underscore _ is to avoid an “unusued package” compiler error.
  3. import _ "fmt"
  4. func hello()
  5. func main() {
  6. hello()
  7. }

This #include is needed to use Go Assembly’s constants (for example the RODATA that we use later on). Other pre-processor macros can be used in Go Assembly (like #define).

  1. #include "textflag.h"

DATA allows you to store global words in memory 1, 2, 4 or 8 bytes at a time. The <> after the symbol name restricts the data to the current file.

  1. DATA world<>+0(SB)/8, $"hello wo"
  2. DATA world<>+8(SB)/4, $"rld "
  3. //GLOBL makes the data global and read-only (RODATA) for the relevant length (12).
  4. GLOBL world<>+0(SB), RODATA, $12
  5. TEXT ·hello(SB),$88-0
  6. SUBQ $88, SP
  7. MOVQ BP, 80(SP)
  8. LEAQ 80(SP), BP
  9. //A Go string is created in my_string (a pointer to the string followed by the string’s length).
  10. LEAQ world<>+0(SB), AX
  11. MOVQ AX, my_string+48(SP)
  12. MOVQ $11, my_string+56(SP)
  13. MOVQ $0, autotmp_0+64(SP)
  14. MOVQ $0, autotmp_0+72(SP)
  15. LEAQ type·string(SB), AX
  16. MOVQ AX, (SP)
  17. LEAQ my_string+48(SP), AX
  18. MOVQ AX, 8(SP)
  19. //convT2E from the runtime package is used to create an interface.
  20. CALL runtime·convT2E(SB)
  21. MOVQ 24(SP), AX
  22. MOVQ 16(SP), CX
  23. MOVQ CX, autotmp_0+64(SP)
  24. MOVQ AX, autotmp_0+72(SP)
  25. LEAQ autotmp_0+64(SP), AX
  26. MOVQ AX, (SP)
  27. MOVQ $1, 8(SP)
  28. MOVQ $1, 16(SP)
  29. //fmt.Println is called with the interface created
  30. CALL fmt·Println(SB)
  31. //This is pretty complicated, the lesson here is: don’t try to call functions from Go’s assembly.
  32. MOVQ 80(SP), BP
  33. ADDQ $88, SP
  34. RET

Next example: Arcsin.

ft_authoradmin  ft_create_time2018-02-03 13:02
 ft_update_time2018-02-03 13:04