CSC231 createFileProc.asm
--D. Thiebaut 13:47, 10 November 2008 (UTC)
;;; writeFile.asm
;;; D. Thiebaut
;;; Writes the contents of the message defined in the data
;;; segment to a file called data.txt.
;;;
;;; Uses macros for printing and functions for file operations.
;;; ---------------------------------------------------------
;;; FILE-RELATED CONSTANTS
;;; ---------------------------------------------------------
%assign SYS_EXIT 1
%assign SYS_WRITE 4
%assign SYS_READ 3
%assign SYS_LSEEK 19
%assign SEEKSET 0
%assign STDOUT 1
%assign SYS_OPEN 5
%assign SYS_CLOSE 6
%assign SYS_CREATE 8
%assign O_RDONLY 000000q
%assign O_WRONLY 000001q
%assign O_RDWR 000002q
%assign O_CREAT 000100q
%assign S_IRUSR 00400q
%assign S_IWUSR 00200q
%assign S_IXUSR 00100q
;;; --- MACRO -----------------------------------------------
;;; print msg,length
%macro print 2 ; %1 = address %2 = # of chars
pushad ; save all registers
mov edx,%2
lea ecx,[%1]
mov eax,SYS_WRITE
mov ebx,STDOUT
int 0x80
popad ; restore all registers
%endmacro
;;; --- MACRO -----------------------------------------------
;;; print2 "quoted string"
%macro print2 1 ; %1 = immediate string,
section .data
%%str db %1
%%strL equ $-%%str
section .text
print %%str, %%strL
%endmacro
;; -------------------------
;; data segment
;; -------------------------
section .data
fileName db "data.txt",0
handle dd 0
noRead dd 0 ; to store # of chars read from file
message db "A fine romance, with no kisses", 0x0a
db "A fine romance, my friend, this is!", 0x0a,
db "We should be like a couple of hot tomatoes", 0x0a
db "But youre as cold as yesterdays mashed potatoes", 0x0a, 0x0a
msgLen equ $-message
section .bss
MAXBUF equ 100000
buffer resb MAXBUF ; 100,000 bytes of storage
;; -------------------------
;; code area
;; -------------------------
section .text
global _start
_start:
;;; open the file and put its contents in Buffer.
;;; keep track of # of bytes read in noRead
;;; createFile( fileName, [handle] )
mov eax, fileName
push eax
mov eax, handle
push eax
call createFile
;;; writeFile( [handle], message, msgLen )
push dword [handle]
mov eax, message
push eax
mov eax, msgLen
push eax
call writeFile
;;; close( [handle] )
push dword [handle]
call close
print buffer, [noRead]
;;; exit()
mov eax,SYS_EXIT
mov ebx,0
int 0x80 ; final system call
;;; ---------------------------------------------------------
;;; sleep: assuming a 2 GHz processor, the instruction
;;; for: loop for should take 1 cycle to execute
;;; since the processor goes through 2E09 cycles
;;; per second, loading ecx with 2000000000 before
;;; the loop should yield something in the order of
;;; a second delay.
sleep: push ecx
mov ecx, 2000000000
.for loop .for
pop ecx
ret
;;; --------------------------------------------------------------
;;; readFile( handle, buffer, buffer_length, number_bytes_read )
;;; reads bytes into buffer. At most buffer_length bytes are
;;; read. Number of bytes read are stored in dword number_bytes_read.
readFile:
push ebp
mov ebp, esp
%define rf_handle dword [ebp+20]
%define rf_buffer dword [ebp+16]
%define rf_len dword [ebp+12]
%define rf_bytes dword [ebp+8]
pushad
mov eax,SYS_READ
mov ebx, rf_handle
mov ecx, rf_buffer
mov edx, rf_bytes
int 0x80
mov ebx, rf_bytes ; get the address of variable
mov [ebx], eax ; put # bytes read in it
popad
pop ebp
ret 4*4
;;; --------------------------------------------------------------
;;; close( handle )
;;; closes the file
close: push ebp
mov ebp, esp
pushad
%define cl_handle dword [ebp+8]
mov eax,SYS_CLOSE
mov ebx,cl_handle
int 0x80
popad
pop ebp
ret 4
;;; --------------------------------------------------------------
;;; createFile( filename, handle )
;;; creates a file whose name is pointed to by filename, and puts
;;; the handle in the dword variable handle.
;;; Does not modify registers.
createFile:
push ebp
mov ebp, esp
pushad
%define cf_fileName dword[ebp+12]
%define cf_handle dword[ebp+8]
mov eax,SYS_CREATE
mov ebx,cf_fileName
mov ecx, S_IRUSR|S_IWUSR|S_IXUSR
int 0x80
test eax,eax
jns .fileOk
print2 "Could not create file"
mov eax,SYS_EXIT
mov ebx,0
int 0x80 ; final system call
.fileOk:
mov ebx, cf_handle
mov [ebx], eax
.done: popad ; restore registers
pop ebp
ret 8
;;; --------------------------------------------------------------
;;; openFile( filename, handle )
;;; opens a file for reading, puts handle in dword whose address
;;; is passed in stack.
;;; Does not modify the registers, but modifies flags.
openFile:
push ebp
mov ebp, esp
pushad
%define of_fileName dword[ebp+12]
%define of_handle dword[ebp+8]
mov eax,SYS_OPEN
mov ebx, of_fileName
mov ecx, O_RDONLY
mov edx, S_IRUSR|S_IWUSR|S_IXUSR
int 0x80
test eax,eax
jns .fileOk
print2 "Error opening file"
mov eax, SYS_EXIT
mov ebx, 1
int 0x80
.fileOk:
mov ebx, of_handle
mov [ebx], eax ; save handle
.done: popad ; restore registers
pop ebp
ret 8
;;; --------------------------------------------------------------
;;; writeFile( handle, buffer, bufferLen )
;;; writes bufferLen bytes stored in buffer to file whose handle
;;; is in "handle"
writeFile:
push ebp
mov ebp, esp
pushad
%define wf_handle [ebp+16]
%define wf_buffer [ebp+12]
%define wf_len [ebp+8]
mov eax,SYS_WRITE
mov ebx,wf_handle
mov ecx,wf_buffer
mov edx,wf_len
int 0x80
popad
pop ebp
ret 3*4