0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ubuntuにてgccgoを使ってアセンブリ

Last updated at Posted at 2022-11-29

go + nasmを使ってみたかったのでサンプルのhello worldを出力してみました。

参考はこちら(12年前の投稿だけど):

バージョンがいろいろ狂っているので、ちょっと直したいところです。

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.7 LTS
Release:	16.04
Codename:	xenial

$ sudo apt-get -y install golang
$ go version
go version go1.6.2 linux/amd64 #古すぎな

$ sudo apt-get -y install nasm
$ nasm -version
NASM version 2.11.08

$ sudo apt-get -y install gccgo
$ gccgo --version
gccgo (Ubuntu 6.0.1-0ubuntu1) 6.0.0 20160414 (experimental) [trunk revision 234994]
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

確認のために肝心のhello()を2回一応呼び出す。

main.go
package main

func hello();

func main() {
    hello()
    hello()
}

こちらは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 main.hello        ; make label available to linker (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

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

以下のように出力されるはず。

$ make
$ ./main
Hello World
Hello World
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?