All pastes #1278399 Raw Edit

Miscellany

public text v1 · immutable
#1278399 ·published 2008-12-07 00:39 UTC
rendered paste body
; _fopen:
; Opens a file.
; ebx = File's Name
; ecx = File flags
; edx = File mode
;
;        Return:
; FileDescriptor = FD

; Modes
%define O_ACCMODE 0003h
%define O_RDONLY  0000h
%define O_WRONLY  0001h
%define O_RDWR    0002h
%define O_CREAT   0064h
%define O_EXCL    00C8h
%define O_NOCTTY  0190h
%define O_TRUNC   03E8h
%define O_APPEND  07D0h
%define O_NONBLOCK 0FA0h
%define O_NDELAY  0FA0h
%define O_SYNC    000186A0h
%define O_FSYNC   000186A0h
%define O_ASYNC   00030D40h
; Flags (sum them together)
%define OWNER_EXEC  0040h
%define OWNER_READ  0100h
%define OWNER_WRITE 0080h
%define GROUP_EXEC  0008h
%define GROUP_READ  0020h
%define GROUP_WRITE 0010h
%define USER_EXEC   0001h
%define USER_READ   0004h
%define USER_WRITE  0002h


segment .data
    Advertence1: db "Insert file name, up to 8 characters: ", 0
    AdvertenceLen1: equ $-Advertence1
    Advertence2: db "Read (R) or write (W)?: ", 0
    AdvertenceLen2: equ $-Advertence2
    EndOfFile db "End Of File", 0
    EndOfFileLen equ $-EndOfFile
    Error1: db "Unknown operation", 0
    ErrorLen1: equ $-Error1
    Error2: db "Problems with open", 0
    ErrorLen2: equ $-Error2
    Error3: db "Problems with read", 0
    ErrorLen3: equ $-Error3
    Error4: db "Trying to do file operation without an available file descriptor", 0
    ErrorLen4: equ $-Error4

segment .bss
    OpInfo:
        resb 1
    ProgramMode:
        resb 1
    FileName:
        resb 8
    FileDescriptor:
        resb 1
    FileFlags:
        resw 1
    ; lalala saves us from dying (eax is a 32 bit register so it will also
    ; read FileModes along with FileFlags)
    FileSavior:
        resw 1
    FileModes:
        resw 1
    FileSavior2:
        resw 1

segment .text
    global _start

_start:
    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, Advertence1
    mov edx, AdvertenceLen1
    int 80h

    mov eax, 0003h
    mov ebx, 0000h
    mov ecx, FileName
    mov edx, 0008h
    int 80h

    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, Advertence2
    mov edx, AdvertenceLen2
    int 80h

    mov eax, 0003h
    mov ebx, 0000h
    mov ecx, ProgramMode
    mov edx, 0001h
    int 80h

    mov eax, 0003h
    int 80h

    mov word [FileFlags], 0000h
    or word [FileFlags], O_RDWR
    or word [FileFlags], O_CREAT
    or word [FileFlags], O_APPEND
    mov word [FileModes], 0000h
    add word [FileModes], OWNER_READ
    add word [FileModes], OWNER_WRITE
    add word [FileModes], GROUP_READ
    add word [FileModes], USER_READ
    call _fopen

    cmp byte [ProgramMode], 0052h
    je _fread
    cmp byte [ProgramMode], 0057h
    je _fwrite
    jne _error1
    call _exit

_fopen:
    mov eax, 0005h
    mov ebx, FileName
    mov ecx, [FileFlags]
    mov edx, [FileModes]
    int 80h
    mov [FileDescriptor], eax
    ret

_fclose:
    mov eax, 0006h
    mov ebx, FileDescriptor
    int 80h

    ret

_fread:
    mov eax, 0003h
    mov ebx, [FileDescriptor]
    mov ecx, [OpInfo]
    mov edx, 0001h
    int 80h

    cmp eax, 0000h
    je _eof
    cmp eax, 0000h
    jb _error3

    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, [OpInfo]
    mov edx, 0001h
    int 80h
    
    test eax, eax
    je _fread
    ret

_fwrite:
    jmp _error1
    ret

_error1:
    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, Error1
    mov edx, ErrorLen1
    int 80h

    call _exit
    ret

_error2:
    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, Error2
    mov edx, ErrorLen2
    int 80h

    call _exit
    ret

_error3:
    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, Error3
    mov edx, ErrorLen3
    int 80h

    call _exit
    ret

_error4:
    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, Error4
    mov edx, ErrorLen4
    int 80h

    call _exit
    ret

_eof:
    mov eax, 0004h
    mov ebx, 0001h
    mov ecx, EndOfFile
    mov edx, EndOfFileLen
    int 80h

    call _exit
    ret

_exit:
    call _fclose

    mov eax, 0001h
    mov ebx, 0000h
    int 80h