vak: (Daemon)
[personal profile] vak
Для знакомства с архитектурой SPARC напишем классический пример Hello World на ассемблере. Вот такой файл hello.S:
#include <sys/syscall.h>

        .section .text
        .globl _start
_start:
        mov SYS_write, %g1  // system call to print a string
        mov 1, %o0          // stdout
        set message, %o1    // text to print
        mov 14, %o2         // how many bytes
1:      ta  0               // syscall

        mov SYS_exit, %g1   // system call to finish the program
        mov 0, %o0          // status code
2:      ta  0               // syscall

message:
        .string "Hello, world!\n"
Компилируем в статический бинарник для простоты:
$ cc -E hello.S | as -o hello.o -

$ ld -static -nopie hello.o -o hello

$ size hello
text	data	bss	dec	hex
75	0	0	75	4b
Однако... не работает. Чтобы удовлетворить встроенным в OpenBSD механизмам безопасности, надо добавить ещё пару секций:
        // Mark this binary as being built specifically for OpenBSD.
        .section ".note.openbsd.ident", "a"
        .p2align 2
        .long   8, 4, 1
        .ascii  "OpenBSD\0"
        .long   0

        // List of allowed syscalls.
        .section ".openbsd.syscalls"
        .p2align 2
        .long   1b, SYS_write
        .long   2b, SYS_exit
Вот теперь хорошо:
$ ./hello
Hello, world!
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org