Quantcast
Channel: Nikolay Igotti
Viewing all articles
Browse latest Browse all 30

Self printing program in assembly

$
0
0
Self printing programs, so called quines are funny, so I decided to write one in linux-x86 ASM just to make almost really self reproducing program (almost, as it relies upon syscall for output, but that's Unix), not relying upon any kind of runtime system provided by language. It's absolutely self-contained, and produces with usual gcc selfprint.s -nodefaultlibs -nostdlib -static -o sp && strip sp 416 bytes ELF file printing its own complete machine code.
macro SC num, a1, a2, a3
        movl  \num, %eax
        movl  \a1,  %ebx
        movl  \a2,  %ecx
        movl  \a3,  %edx
        int $0x80        
.endm

.globl _start
        .type   _start, @function
_start:
        call 1f
1:
        popl %esi
        subl $5, %esi
        xorl %edi, %edi
        subl $0x4, %esp
loop:   
        movzb (%esi,%edi),%eax
        andl $0xf, %eax
        movzb 0x63(%esi,%eax),%ebx        
        shll  $0x8, %ebx
        movzb (%esi,%edi),%eax
        shrl $0x4, %eax
        movzb 0x63(%esi,%eax),%ecx
        orl  %ecx, %ebx
        orl $0x00200000,%ebx
        movl %ebx, (%esp)
        SC  $0x4, $1, %esp, $4
        incl %edi
        cmpl $114, %edi
        jle loop
done:
        SC $0x1 $239 $0 $0
       .byte 0x30
        .byte 0x31
        .byte 0x32
        .byte 0x33
        .byte 0x34
        .byte 0x35
        .byte 0x36
        .byte 0x37
        .byte 0x38
        .byte 0x39
        .byte 0x61
        .byte 0x62
        .byte 0x63
        .byte 0x64
        .byte 0x65
        .byte 0x66

Viewing all articles
Browse latest Browse all 30

Trending Articles