Difference between revisions of "CSC231 ledOn.asm"

From dftwiki3
Jump to: navigation, search
(New page: <code><pre> ;;; 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 put...)
 
Line 1: Line 1:
 
<code><pre>
 
<code><pre>
 +
 
;;; ledON.asm
 
;;; ledON.asm
 
;;; D. Thiebaut
 
;;; D. Thiebaut
Line 11: Line 12:
 
;;; gcc -m32 -o ledON driver.c asm_io.o ledON.o
 
;;; gcc -m32 -o ledON driver.c asm_io.o ledON.o
 
;;; (use -m32 on 64-bit machines)
 
;;; (use -m32 on 64-bit machines)
;;;  
+
;;;
 
;;; Without the asm_io.* and driver.c wrappers, then link with
 
;;; Without the asm_io.* and driver.c wrappers, then link with
 
;;; ld -melf_i386 ledON.o -o ledON
 
;;; ld -melf_i386 ledON.o -o ledON
 
;;; (use -melf_i386 on 64-bit machines)
 
;;; (use -melf_i386 on 64-bit machines)
;;;  
+
;;;
 
;;; To Execute:
 
;;; To Execute:
;;;  
+
;;;
 
;;; ./ledON
 
;;; ./ledON
;;;  
+
;;;
 
;;; -------------------------------------------------------------------
 
;;; -------------------------------------------------------------------
  
Line 26: Line 27:
 
%assign EXIT            1
 
%assign EXIT            1
 
%assign OPEN            5
 
%assign OPEN            5
%assign CLOSE          6      
+
%assign CLOSE          6
 
%assign READ            3
 
%assign READ            3
%assign CREATE          8      
+
%assign CREATE          8
 
%assign WRITE          4
 
%assign WRITE          4
 
%assign STDOUT          1
 
%assign STDOUT          1
 
%assign O_CREAT        64
 
%assign O_CREAT        64
 
%assign O_RDONLY        0
 
%assign O_RDONLY        0
%assign O_WRONLY        1        
+
%assign O_WRONLY        1
%assign O_APPEND        1024      
+
%assign O_APPEND        1024
%assign O_RDWR          2      
+
%assign O_RDWR          2
%assign S_IRUSR        00400q  
+
%assign S_IRUSR        00400q
 
%assign S_IWUSR        00200q
 
%assign S_IWUSR        00200q
 
%assign S_IXUSR        00100q
 
%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                             
 
  
 
;;;          ____        _
 
;;;          ____        _
Line 112: Line 47:
 
;;;        | |_| | (_| | || (_| |
 
;;;        | |_| | (_| | || (_| |
 
;;;        |____/ \__,_|\__\__,_|
 
;;;        |____/ \__,_|\__\__,_|
;;;  
+
;;;
       
+
 
  
        section        .data
+
          section        .data
 
errorMsg  db    "Error accessing file!",0x0a, 0
 
errorMsg  db    "Error accessing file!",0x0a, 0
 
errorMsg1 db    "Error opening file!",0x0a, 0
 
errorMsg1 db    "Error opening file!",0x0a, 0
usb      db    "/dev/ttyUSB0",0      
+
usb      db    "/dev/ttyUSB0",0
 
handle    dd    0
 
handle    dd    0
noRead    dd    0
+
noRead    dd    0
msg      db    "w d 13 1", 0x0d  
+
msg      db    "w d 13 1", 0x0d
 
msgLen    equ    $-msg
 
msgLen    equ    $-msg
               
+
 
 
           section .bss
 
           section .bss
 
%assign  MAXBUF  100
 
%assign  MAXBUF  100
Line 133: Line 68:
 
;;;        | |__| (_) | (_| |  __/
 
;;;        | |__| (_) | (_| |  __/
 
;;;          \____\___/ \__,_|\___|
 
;;;          \____\___/ \__,_|\___|
;;;  
+
;;;
       
+
 
  
 
         section .text
 
         section .text
Line 142: Line 77:
  
 
;;; open file, write string, close file...
 
;;; open file, write string, close file...
       
 
        openFile2      usb, dword[handle]
 
        writeFile      [handle], msg, msgLen
 
        closeFile      [handle]
 
  
          
+
         mov    ebx, usb
 +
        call    openFile
 +
        mov    dword[handle], eax
 +
        mov    ebx, [handle]
 +
        mov    ecx, msg
 +
        mov    edx, msgLen
 +
        call    writeFile
 +
        mov    ebx, dword[handle]
 +
        call    closeFile
 +
 
 
;;; we're done!
 
;;; we're done!
       
+
 
exit:  
+
exit:
 
         mov    eax,EXIT
 
         mov    eax,EXIT
 
         mov    ebx,0
 
         mov    ebx,0
 
         int    0x80            ; final system call
 
         int    0x80            ; final system call
 +
 +
 +
;;; --------------------------------------------------------
 +
;;; openFile
 +
;;; put address of ASCIIZ file name in ebx
 +
;;; handle returned in eax
 +
;;; --------------------------------------------------------
 +
openFile:
 +
        mov    eax, OPEN
 +
        mov    ecx,O_WRONLY | O_APPEND ; mode for opening file
 +
        mov    edx,S_IRUSR | S_IWUSR
 +
        int    0x80
 +
        test    eax,eax        ; test for errors
 +
        jns    doneOpenFile
 +
        mov    eax, errorMsg1
 +
        call    print_string
 +
        jmp    exit
 +
doneOpenFile:
 +
        ret                    ; handle in eax
 +
 +
;;; --------------------------------------------------------
 +
;;; closeFile: the handle must be in ebx on entry.
 +
;;;              Closes the file
 +
;;; --------------------------------------------------------
 +
closeFile:
 +
        mov    eax,CLOSE
 +
        int    0x80
 +
        ret
 +
 +
;;; --------------------------------------------------------
 +
;;; readFile: on entry:
 +
;;;                  handle in ebx
 +
;;;                  buffer address in ecx
 +
;;;                  max buffer length in edx
 +
;;; on exit: number of chars read in eax
 +
;;; --------------------------------------------------------
 +
readFile:
 +
        mov    eax,READ
 +
        int    0x80
 +
        test    eax,eax
 +
        jns    doneReadFile
 +
        mov    eax, errorMsg
 +
        call    print_string
 +
        jmp    exit
 +
doneReadFile:
 +
        ret
 +
 +
 +
;;; --------------------------------------------------------
 +
;;; writeFile:
 +
;;; on entry:
 +
;;;        handle is in ebx
 +
;;;        buffer address is in ecx,
 +
;;;        number of chars is in edx.
 +
;;; --------------------------------------------------------
 +
writeFile:
 +
        mov    eax,WRITE
 +
        int    0x80
 +
        test    eax,eax
 +
        jns    doneWriteFile
 +
        mov    eax, errorMsg
 +
        call    print_string
 +
        jmp    exit
 +
doneWriteFile:
 +
        ret
  
  
 
</pre></code>
 
</pre></code>

Revision as of 16:22, 30 October 2008


;;; 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


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


          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...

        mov     ebx, usb
        call    openFile
        mov     dword[handle], eax
        mov     ebx, [handle]
        mov     ecx, msg
        mov     edx, msgLen
        call    writeFile
        mov     ebx, dword[handle]
        call    closeFile

;;; we're done!

exit:
        mov     eax,EXIT
        mov     ebx,0
        int     0x80             ; final system call


;;; --------------------------------------------------------
;;; openFile
;;; put address of ASCIIZ file name in ebx
;;; handle returned in eax
;;; --------------------------------------------------------
openFile:
        mov     eax, OPEN
        mov     ecx,O_WRONLY | O_APPEND ; mode for opening file
        mov     edx,S_IRUSR | S_IWUSR
        int     0x80
        test    eax,eax         ; test for errors
        jns     doneOpenFile
        mov     eax, errorMsg1
        call    print_string
        jmp     exit
doneOpenFile:
        ret                     ; handle in eax

;;; --------------------------------------------------------
;;; closeFile: the handle must be in ebx on entry.
;;;               Closes the file
;;; --------------------------------------------------------
closeFile:
        mov     eax,CLOSE
        int     0x80
        ret

;;; --------------------------------------------------------
;;; readFile: on entry:
;;;                  handle in ebx
;;;                  buffer address in ecx
;;;                  max buffer length in edx
;;; on exit: number of chars read in eax
;;; --------------------------------------------------------
readFile:
        mov     eax,READ
        int     0x80
        test    eax,eax
        jns     doneReadFile
        mov     eax, errorMsg
        call    print_string
        jmp     exit
doneReadFile:
        ret


;;; --------------------------------------------------------
;;; writeFile:
;;; on entry:
;;;         handle is in ebx
;;;         buffer address is in ecx,
;;;         number of chars is in edx.
;;; --------------------------------------------------------
writeFile:
        mov     eax,WRITE
        int     0x80
        test    eax,eax
        jns     doneWriteFile
        mov     eax, errorMsg
        call    print_string
        jmp     exit
doneWriteFile:
        ret