Author Topic: [Linux x86] - read a file character by character (inefficient)  (Read 1566 times)

0 Members and 1 Guest are viewing this topic.

xor

  • Guest
[Linux x86] - read a file character by character (inefficient)
« on: December 11, 2010, 05:23:31 pm »
Code: (asm) [Select]
.section .text
        .globl _start

_start:
        xor  %eax, %eax
        xor  %ebx, %ebx
        xor  %ecx, %ecx
        xor  %edx, %edx
        jmp  file


open:
        mov  $0x05, %al    # 5 sys_open(const char *, int, int)
        mov  (%esp), %ebx  # load string pointer into register
        xor  %ecx, %ecx    # O_RDONLY (open read only)
        int  $0x80

        mov  %eax, %esi    # file descriptor
        jmp  read

exit:
        xor  %ebx, %ebx
        mov  $0x01, %al
        int  $0x80

read:
        mov  $0x03, %al    # 3 sys_read(unsigned int, char *, size_t)
        mov  %esi, %ebx    # file descriptor (unsigned int)
        lea  (%esp), %ecx  #
        mov  $0x01, %dl    # read only 1 char (third arg)
        int  $0x80         # sys_call

        xor  %ebx, %ebx    # clear out ebx
        cmp  %eax, %ebx    # compare eax and ebx
        je   exit          # if EAX && EBX == NULL, EOF

        mov  $0x04, %al    # 4 sys_write(unsigned int, const char *, size_t)
        mov  $0x01, %bl    # write to STDOUT
        mov  $0x01, %dl    # write one character
        int  $0x80         # call it

        jmp  read          # read the next char

file:
        call    open
        .string  "/etc/passwd"

Not tested this one in a while, can't remember whether it's supposed to be .string or .ascii. Give it a try and let me know!