231a-aj lab 6

From CSclasswiki
Jump to: navigation, search

Lab 6

Purpose

The purpose of this lab was to learn how to make the computer and the arduino talk to each other

Method

I followed the directions found here

Code

  ;;; -------------------------------------------------------------------
  ;;; 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!
       ret
  ;;; if there's a problem, we exit right away 
  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

I made a version of this for turning the LED off

  ;;; -------------------------------------------------------------------
  ;;; ledOff.asm
  ;;; Tonje Stolpestad (based on code by 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 0\r"
  ;;;
  ;;;
  ;;; nasm -f elf ledOff.asm
  ;;; nasm -f elf asm_io.asm
  ;;; gcc -m32 -o ledOff driver.c asm_io.o ledOff.o
  ;;; (use -m32 on 64-bit machines)
  ;;;
  ;;; Without the asm_io.* and driver.c wrappers, then link with
  ;;; ld -melf_i386 ledOff.o -o ledOff
  ;;; (use -melf_i386 on 64-bit machines)
  ;;;
  ;;; To Execute:
  ;;;
  ;;; ./ledOff
  ;;;
  ;;; -------------------------------------------------------------------
 %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 0", 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!
       ret
  ;;; if there's a problem, we exit right away 
  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