Thanks for all the feedback and info guys, it's cleared up some important questions I've had. I've rewritten the main program to be linked with ld. There's a massive drop in the file size, and I guess if this were something that was operating in large batches, it would make a speed difference too. Revised code below:
;PROGRAM: Cat64.asm
;AUTHOR: 0xFF
;DATE: 27/10/2013
;DESCRIPTION This program is a simple replication of the
; cat coreutil from linux which takes input
; from a file and writes its contents into
; stdout. For error handling and reporting it
; must be linked with my file handling error
; function ORWerrors.asm.
;
;COMPILE INSTRUCTIONS:
; nasm -f elf64 -o Cat64.o Cat64.asm
; ld -o Cat64 Cat64.o ../ORWErrors/ORWerrors.o ../SysPrint/SysPrint.o
SECTION .data
SECTION .bss
ByteBuff resb 256
SECTION .text
extern ORWerrors
global _start
_start:
nop ;traditional nop
cmp qword [rsp], 2 ;check argc at rsp: was an argument given?
jl Exit ;jump to exit if only 1 command argument
;(if only 1, then no file specified)
;opening the file
mov rax, 2 ;specify open syscall
mov rdi, [rsp+16] ;placing argv[1] in rdi for syscalls
mov rsi, 0 ;setting flags to 0, read only
mov rdx, 0 ;do I have to include this mode specification?
syscall ;call the kernel
cmp rax, 0 ;check for errors on opening file
jl Error ;if less than zero, error
mov rbx, rax ;saving file descriptor into rbx where it is safe across calls
read:
mov rax, 0 ;read syscall number
mov rdi, rbx ;placing file descriptor into
mov rsi, ByteBuff ;moving buffer address to rsi
mov rdx, 256 ;specity to read 256 bytes
syscall ;call the kernel
cmp rax, 0 ;check for EOF
jl Error ;if less than zero, error
jne print ;if not EOF, jump over to print
;close file and jump to exit
mov rax, 3 ;specify close syscall
mov rdi, rbx ;specify file descriptor
syscall ;call the kernel
cmp rax, 0 ;check for errors
jl Error ;if less than zero, error
jmp NoError ;jump to exit
print:
mov rdx, rax ;mov number of bytes read to rdx
mov rax, 1 ;specify print (sys_write) syscall
mov rdi, 1 ;specify stdout file descriptor
mov rsi, ByteBuff ;mov buffer address to rsi to prepare for printing syscall
syscall ;call the kernel
cmp rax, 0 ;check for errors
jl Error ;if less than zero, error
jmp read ;jump back to read for another loop
Error:
call ORWerrors ;call the external error handler
mov rdi, rax ;move an error code to rdi
jmp Exit ;nothing more to do
NoError:
mov rdi, 0 ;if no errors occured, will report 0 error
Exit:
mov rax, 60 ;specify exit syscall
syscall ;call the kernel
i guess other tasks are to add a little usage message and start using make files to simplify compiling, but then again I've learned probably all I will from this project... Hmm, what next I wonder?