CSC231 ledON.asm

From dftwiki3
Revision as of 12:43, 7 August 2008 by Thiebaut (talk | contribs)
Jump to: navigation, search
;;; ledON.asm
;;; D. Thiebaut
;;; This program turns the LED On on the Arduino stamp.
;;; The program simply writes an \r -terminated string to /dev/ttyUSB0
;;; that simply puts a 1 on Digital Pin 13.  The string is "w d 13 1\r"
;;;
;;;
;;; nasm -f elf ledON.asm
;;; nasm -f elf asm_io.asm
;;; gcc -m32 -o ledON driver.c asm_io.o ledON.o
;;; (use -m32 on 64-bit machines)
;;; 
;;; Without the asm_io.* and driver.c wrappers, then link with
;;; ld -melf_i386 ledON.o -o ledON
;;; (use -melf_i386 on 64-bit machines)
;;; 
;;; To Execute:
;;; 
;;; ./ledON
;;; 
;;; -------------------------------------------------------------------

%include "asm_io.inc"

%assign EXIT            1
%assign OPEN            5
%assign CLOSE           6        
%assign READ            3
%assign CREATE          8        
%assign WRITE           4
%assign STDOUT          1
%assign O_CREAT         64
%assign O_RDONLY        0
%assign O_WRONLY        1         
%assign O_APPEND        1024        
%assign O_RDWR          2        
%assign S_IRUSR         00400q 
%assign S_IWUSR         00200q
%assign S_IXUSR         00100q
        

;;;          __  __
;;;         |  \/  | __ _  ___ _ __ ___  ___
;;;         | |\/| |/ _` |/ __| '__/ _ \/ __|
;;;         | |  | | (_| | (__| | | (_) \__ \
;;;         |_|  |_|\__,_|\___|_|  \___/|___/
;;; 
        
; --- MACRO -----------------------------------------------
;       openFile2  fileName, handle
%macro  openFile2        2
        mov     eax, OPEN
        mov     ebx,%1                  ; address of ASCIIZ filename
        mov     ecx,O_WRONLY | O_APPEND ; mode for opening file
        mov     edx,S_IRUSR | S_IWUSR 
        int     0x80
        test    eax,eax
        jns     %%openFile
        mov     eax, errorMsg1
        call    print_string
        jmp     exit
%%openFile:   
        mov     %2,eax        
%endmacro


; --- MACRO -----------------------------------------------
;       closeFile  handle
%macro  closeFile       1
        mov     eax,CLOSE
        mov     ebx,%1
        int     0x80
%endmacro        
        
; --- MACRO -----------------------------------------------
;       readFile  handle,buffer,maxbufflength,noread
%macro  readFile 4        
        mov     eax,READ
        mov     ebx,%1
        mov     ecx,%2
        mov     edx,%3
        int     0x80
        test    eax,eax
        jns     %%readFile
        mov     eax, errorMsg
        call    print_string
        jmp     exit
%%readFile:     
        mov     %4,eax        
%endmacro                               

        ; --- MACRO -----------------------------------------------
;       writeFile  handle,buffer,noChars
%macro  writeFile 3
        mov     eax,WRITE
        mov     ebx,%1
        mov     ecx,%2
        mov     edx,%3
        int     0x80
        test    eax,eax
        jns     %%writeFile
        mov     eax, errorMsg
        call    print_string
        jmp     exit
%%writeFile:     
%endmacro                               

;;;          ____        _
;;;         |  _ \  __ _| |_ __ _
;;;         | | | |/ _` | __/ _` |
;;;         | |_| | (_| | || (_| |
;;;         |____/ \__,_|\__\__,_|
;;; 
        

         section        .data
errorMsg  db     "Error accessing file!",0x0a, 0
errorMsg1 db     "Error opening file!",0x0a, 0
usb       db     "/dev/ttyUSB0",0        
handle    dd     0
noRead    dd     0  
msg       db     "w d 13 1", 0x0d 
msgLen    equ    $-msg
                
          section .bss
%assign   MAXBUF  100
buffer    resb    MAXBUF

;;;           ____          _
;;;          / ___|___   __| | ___
;;;         | |   / _ \ / _` |/ _ \
;;;         | |__| (_) | (_| |  __/
;;;          \____\___/ \__,_|\___|
;;; 
        

        section .text

global  asm_main
asm_main:

;;; open file, write string, close file...
        
        openFile2       usb, dword[handle]
        writeFile       [handle], msg, msgLen
        closeFile       [handle]

        
;;; we're done!
        
exit:   
        mov     eax,EXIT
        mov     ebx,0
        int     0x80             ; final system call