VOOZH about

URL: https://qiita.com/donalsea/items/d393c6e2ac805016ee94

⇱ Mac OSX の x86-64 のアセンブリ言語で,libc の puts 関数を呼び出すプログラム #MacOSX - Qiita


👁 Image
5

Go to list of users who liked

5

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@donalsea

Mac OSX の x86-64 のアセンブリ言語で,libc の puts 関数を呼び出すプログラム

5
Last updated at Posted at 2012-08-05

http://qiita.com/items/b7ed1c9feef1b570a6b9 の続き。x86-64では引数を渡す際に,可能な限りレジスタを用いる。

hello2.s
 .text
 .globl _main

msg: .asciz "hello, world."

_main:
 # スタックポインタを16バイト境界に合わせる (ABIの規約)
 subq $8, %rsp
 
 # 第1引数 (%rdi) に文字列のポインタを指定
 leaq msg(%rip), %rdi
 
 # ベクタレジスタ数 (%rax) を0に設定
 movq $0, %rax
 
 # putsを呼ぶ
 callq _puts
 
 # スタックポインタを_mainの呼び出し前の状態に戻す
 addq $8, %rsp
 
 # 戻り値として0を返す
 movq $0, %rax
 ret

実行方法:

$gcc -o hello2 hello2.s
$./hello2
hello, world.

gcc を使わずに,ld コマンドだけで libc とリンクする方法は,よく分からない。

参考: x86-64 (amd64) の ABI (Application Binary Interface)
http://www.x86-64.org/documentation.html

追記 (8/5)

正しいやり方か確信がもてないが,以下の方法で ld コマンドでアセンブルできる。

$as -o hello2.o hello2.s
$ld -o hello2 -macosx_version_min 10.7 -lSystem /usr/lib/crt1.o hello2.o
$./hello2
hello, world.
5

Go to list of users who liked

5
0

Go to list of comments

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
5

Go to list of users who liked

5