; file: mcat.asm ; who: Marcus Fritzsch, mATfritschy-DOTde ; when: 20050211 ; desc: mcat, mmapped small asm cat ; works on linux kernel 2.6.9 ; assem: nasm -f elf mcat.asm -o mcat.o; ld -s -o mcat mcat.o BITS 32 %define BUFLEN 0x2000 %define ST_SIZE 20 %define STATLEN 60 ; st_size offset, STATLEN: ; /usr/include/linux/stat.h:struct kstat global _start section .bss buf resb BUFLEN statbuf resb STATLEN ptr resd 1 fd resd 1 section .text _start: xor eax, eax inc eax cmp dword [esp], eax jg .mapfiles .stdin: mov al, 3 ; read xor ebx, ebx mov ecx, buf mov edx, BUFLEN int 0x80 cmp eax, 0 jle .exit mov edx, eax xor eax, eax mov al, 4 ; write xor ebx, ebx inc ebx mov ecx, buf int 0x80 jmp .stdin .exit: xor ebx, ebx xor eax, eax inc eax ; exit int 0x80 .mapfiles: add esp, byte 4 .loop: add esp, byte 4 mov ebx, [esp] or ebx, ebx jz .exit xor eax, eax mov al, 5 ; open xor ecx, ecx int 0x80 xor edx, edx cmp eax, edx jl short .loop mov dword [fd], eax xor eax, eax mov al, 108 ; fstat mov ebx, [fd] mov ecx, statbuf int 0x80 cmp eax, edx ; edx is zero jl .close xor eax, eax mov al, 192 ; mmap2 xor ebx, ebx ; start: (hint) mov ecx, [statbuf+ST_SIZE] ; length xor edx, edx inc edx ; prot: PROT_READ xor esi, esi inc esi ; flags: MAP_SHARED mov edi, [fd] ; fd xor ebp, ebp ; offs int 0x80 test eax, eax jz .close mov dword [ptr], eax xor eax, eax mov al, 4 ; write xor ebx, ebx inc ebx mov ecx, [ptr] mov edx, [statbuf+ST_SIZE] int 0x80 xor eax, eax ; munmap mov al, 91 mov ebx, [ptr] mov ecx, [statbuf+ST_SIZE] int 0x80 .close: xor eax, eax mov al, 6 ; close mov ebx, [fd] int 0x80 jmp .loop