Title Life test
Author irql
Link http://thunked.org/p/view/pub/
Created 2012-05-09 21:50:04
Expires never
Filename life.s
Language @Formula/@Command
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
// x86-64
// I want to be run under a vt100 terminal emulator

.globl _start

.include "macro.s"

prntfield:
    pushq %rcx
    pushq %rdx
    pushq %rax

    movq $63, %rcx
    movq $8, %rdx
1:
    pushq %rcx
    bt $1, (%rbp)
    jnc 2f
    docall3 $NR_write, $1, $live_cell, $6
    jmp 3f
2:
    docall3 $NR_write, $1, $dead_cell, $6
3:
    testq %rdx, %rdx
    jnz 4f
    jmp 5f
4:
    dec %rdx
    rorq $1, (%rbp)
    popq %rcx
    loopq 1b

    popq %rax
    popq %rdx
    popq %rcx
    ret
5:
    docall3 $NR_write, $1, $str3+2, $1
    movq $9, %rdx
    jmp 4b


_start:
    movq %rsp, %rbp
    subq $8, %rsp

    docall2 $NR_open, $str1, $0

    testq %rax, %rax
    jl 1f

    movq %rax, %r11

    docall3 $NR_read, %r11, %rbp, $8
    docall $NR_close, %r11
    call prntfield

    jmp 2f

1:
    docall3 $NR_write, $1, $str2, $28
    docall3 $NR_write, $1, $str3+2, $1
2:
    docall $NR_exit, $15

live_cell:
.ascii "\x1b[42m "
dead_cell:
.ascii "\x1b[40m "
str3:
.ascii "#.\n"
str2:
.ascii "Failed to open "
str1:
.asciz "/dev/urandom"
 
Filename macro.s
Language @Formula/@Command
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
.macro docall4 snum, arg1, arg2, arg3, arg4
    pushq %rdi
    pushq %rsi
    pushq %rdx
    pushq %r10

    movq \snum, %rax
    movq \arg1, %rdi
    movq \arg2, %rsi
    movq \arg3, %rdx
    movq \arg4, %r10
    syscall

    popq %r10
    popq %rdx
    popq %rsi
    popq %rdi
.endm

.macro docall3 snum, arg1, arg2, arg3
    pushq %rdi
    pushq %rsi
    pushq %rdx

    movq \snum, %rax
    movq \arg1, %rdi
    movq \arg2, %rsi
    movq \arg3, %rdx
    syscall

    popq %rdx
    popq %rsi
    popq %rdi
.endm

.macro docall2 snum, arg1, arg2, arg3
    pushq %rdi
    pushq %rsi

    movq \snum, %rax
    movq \arg1, %rdi
    movq \arg2, %rsi
    syscall

    popq %rsi
    popq %rdi
.endm

.macro docall snum, arg1
    pushq %rdi

    movq \snum, %rax
    movq \arg1, %rdi
    syscall

    popq %rdi
.endm

.equ NR_read, 0
.equ NR_write, 1
.equ NR_open, 2
.equ NR_close, 3
.equ NR_exit, 60