CSC231 writeIntegerAtIndex.asm

From dftwiki3
Revision as of 15:09, 19 November 2008 by Thiebaut (talk | contribs) (New page: --~~~~ ---- <code><pre> ;;; writeIntegerAtIndex.asm ;;; D. Thiebaut ;;; Gets two integers from the driver2.c program, which are ;;; saved in Index and Value, two global integers (dwords). ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 20:09, 19 November 2008 (UTC)


;;; writeIntegerAtIndex.asm
;;; D. Thiebaut
;;; Gets two integers from the driver2.c program, which are
;;; saved in Index and Value, two global integers (dwords).
;;; It then takes the Value and stores it at Fib[Index], where
;;; Fib is an array of dwords containing a few fibonacci constants.
;;; The array and the string msg are then written in binary (byte
;;; for byte) in a binary file called data.bin.
;;; 
;;; 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
        global   Value          ; make this label known to the driver2.c
        global   Index          ; program.  Same for Index.
fileName db     "data.bin",0
handle   dd     0
noRead   dd     0               ; to store # of chars read from file
Index    dd     0
Value    dd     0
        
;;; some variables of interest
buffer   equ    $
fib      dd     1, 1, 2, 3, 5, 8, 13, 21
msg      db     "hello", 0
        
bufferLen   equ    $-buffer     ; # of bytes used by fib and msg

        section .bss
MAXBUF  equ     100000
bigbuff resb    MAXBUF          ; 100,000 bytes of storage        

	;; -------------------------
	;; code area
	;; -------------------------

	section	.text
	global	asm_main

asm_main:
;;; modify the fib array and put Value at fib[Index]

        mov     ebx, [Index]
        mov     eax, [Value]
        mov     dword[fib+ebx], eax
        
;;; write the contents of the buffer to file data.bin

;;;     createFile( fileName,  [handle] )
        mov     eax, fileName
        push    eax
        mov     eax, handle
        push    eax
        call    createFile

;;;     writeFile( [handle], message, msgLen )
        push    dword [handle]
        mov     eax, buffer
        push    eax
        mov     eax, bufferLen
        push    eax
        call    writeFile

;;;     close( [handle] )
        push    dword [handle]
        call    close
         
;;; return to driver.c

        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