日本の心・短歌

AnaTofuZ @並列信頼研

CM

今回のLT

「日本のこころ短歌」

短歌知っていますか?

短歌とは5・7・5・7・7語で表される唄

例えば…

いのちなき 砂のかなしさよさらさらと 握れば指のあひだより落つ

思い出の一つのようでそのままにしておく麦わら帽子のへこみ

趣がある

ところで

コンパイラを作る時

アセンブラ

C言語からアセンブラ

以下のようなC言語のコードがある

#include <stdio.h>

int main(void){
    int i = 0;
    i += 3;

    printf("%d\n",i);
    return 0;
}

clang -O0 -s hoge.c などとするとアセンブラが吐かれる

アセンブラ

	.section	__TEXT,__text,regular,pure_instructions
	.macosx_version_min 10, 12
	.globl	_main
	.p2align	4, 0x90
_main:                                  ## @main
	.cfi_startproc
## BB#0:
	pushq	%rbp
Lcfi0:
	.cfi_def_cfa_offset 16
Lcfi1:
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
Lcfi2:
	.cfi_def_cfa_register %rbp
	subq	$16, %rsp
	leaq	L_.str(%rip), %rdi
	movl	$0, -4(%rbp)
	movl	$0, -8(%rbp)
	movl	-8(%rbp), %eax
	addl	$3, %eax
	movl	%eax, -8(%rbp)
	movl	-8(%rbp), %esi
	movb	$0, %al
	callq	_printf
	xorl	%esi, %esi
	movl	%eax, -12(%rbp)         ## 4-byte Spill
	movl	%esi, %eax
	addq	$16, %rsp
	popq	%rbp
	retq
	.cfi_endproc

	.section	__TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
	.asciz	"%d\n"
.subsections_via_symbols

軽く解説

	movl	-8(%rbp), %eax
	addl	$3, %eax
	movl	%eax, -8(%rbp)
	movl	-8(%rbp), %esi
	movb	$0, %al
	callq	_printf

さて

ここにアセンブラがあるじゃろ

        .global main
main:
        mov     $0x616b6157, %eax
        push    %ebx
        push    %eax
        mov     $4, %edx
        mov     $1, %ebx
        mov     $4, %eax
        mov     %esp, %ecx
        int     $0x80
        pop     %eax
        xor     %eax, %eax
        pop     %ebx
        ret

ここからはlinux-x86 32bitのアセンブラで行きます

これをアセンブリして色々見てみる

$ gcc -m32 -o waka waka.s

してアセンブリしてみましょう。(debianで実行しました)

$./waka
Waka

Wakaと出力される(3行目で設定している文字列)

逆アセンブル

00000560 <main>:
 560:   b8 57 61 6b 61          mov    $0x616b6157,%eax
 565:   53                      push   %ebx
 566:   50                      push   %eax
 567:   ba 04 00 00 00          mov    $0x4,%edx
 56c:   bb 01 00 00 00          mov    $0x1,%ebx
 571:   b8 04 00 00 00          mov    $0x4,%eax
 576:   89 e1                   mov    %esp,%ecx
 578:   cd 80                   int    $0x80
 57a:   58                      pop    %eax
 57b:   31 c0                   xor    %eax,%eax
 57d:   5b                      pop    %ebx
 57e:   c3                      ret

お気づきですか?

00000560 <main>:
 560:   b8 57 61 6b 61          mov    $0x616b6157,%eax

 565:   53                      push   %ebx
 566:   50                      push   %eax
 567:   ba 04 00 00 00          mov    $0x4,%edx

 56c:   bb 01 00 00 00          mov    $0x1,%ebx

 571:   b8 04 00 00 00          mov    $0x4,%eax
 576:   89 e1                   mov    %esp,%ecx

 578:   cd 80                   int    $0x80
 57a:   58                      pop    %eax
 57b:   31 c0                   xor    %eax,%eax
 57d:   5b                      pop    %ebx
 57e:   c3                      ret

ようこそアセンブラ短歌へ

b8 57 61 6b 61
53 50 ba 04 00 00 00
bb 01 00 00 00
b8 04 00 00 00 89 e1
cd 80 58 31 c0 5b c3

ちょっとした技術説明

 560:   b8 57 61 6b 61          mov    $0x616b6157,%eax
 565:   53                      push   %ebx
 566:   50                      push   %eax
 567:   ba 04 00 00 00          mov    $0x4,%edx
 56c:   bb 01 00 00 00          mov    $0x1,%ebx
 571:   b8 04 00 00 00          mov    $0x4,%eax
 576:   89 e1                   mov    %esp,%ecx
 578:   cd 80                   int    $0x80

linuxの32bit-modeでは

その後int 0x80という命令で実行をする

なお int0x80 は歴史的な命令で、現在はSYSENTER 命令を使う事が推奨されている

奥深い世界

.section .text
.global main
.type main, @function
 main:

 xor %eax, %eax

 nop
 nop
 inc %eax

 push $('W'|'a'<<8|'k'<<16|'a'<<24)
 nop
 inc %eax

 push $4
 pop %edx
 nop
 inc %eax

 mov %esp, %ecx
 push $1
 pop %ebx
 nop
 inc %eax

 int $0x80
 pop %eax
 xor %eax, %eax
 nop
 ret

inc命令で韻を踏む

00000560 <main>:
 560:   31 c0                   xor    %eax,%eax
 562:   90                      nop
 563:   90                      nop
 564:   40                      inc    %eax
 565:   68 57 61 6b 61          push   $0x616b6157
 56a:   90                      nop
 56b:   40                      inc    %eax
 56c:   6a 04                   push   $0x4
 56e:   5a                      pop    %edx
 56f:   90                      nop
 570:   40                      inc    %eax
 571:   89 e1                   mov    %esp,%ecx
 573:   6a 01                   push   $0x1
 575:   5b                      pop    %ebx
 576:   90                      nop
 577:   40                      inc    %eax
 578:   cd 80                   int    $0x80
 57a:   58                      pop    %eax
 57b:   31 c0                   xor    %eax,%eax
 57d:   90                      nop
 57e:   c3                      ret
 57f:   90                      nop
31 cr 90 90 40
68 57 61 6b 61 90 40
61 04 5a 90 40 
89 e1 6a 01 5b 90 40
cd 80 58 31 c0 90 c3

作品紹介

id:asmtankaさん

push  $0
pop   %eax
push  %eax
inc   %eax

push  $0x616d6179
push  %eax
inc   %eax

push  $8
pop   %edx
pop   %ebx
inc   %eax

push  $0x616b6157
push  %esp
inc   %eax

pop   %ecx
int   $0x80
pop   %eax
pop   %eax
pop   %eax
ret

詳しくは

そういえばPerl

#!/usr/bin/perl

APPEAL:

listen (please, please);

open yourself, wide;
    join (you, me),
connect (us,together),

tell me.

do something if distressed;

    @dawn, dance;
    @evening, sing;
    read (books,$poems,stories) until peaceful;
    study if able;

    write me if-you-please;

sort your feelings, reset goals, seek (friends, family, anyone);

        do*not*die (like this)
        if sin abounds;

keys (hidden), open (locks, doors), tell secrets;
do not, I-beg-you, close them, yet.

                            accept (yourself, changes),
                            bind (grief, despair);

require truth, goodness if-you-will, each moment;

select (always), length(of-days)

# listen (a perl poem)
# Sharon Hopkins
# rev. June 19, 1995