CSC231 Homework 8 Solution
--D. Thiebaut 20:12, 19 November 2008 (UTC)
;;; hw8.asm
;;; Yang Li
;;;
;;; This program works with arduino-serial.c,
;;; and reads a file of commands and sends them to the Arduino.
;;;
;;; To assemble and run:
;;; nasm -f elf hw8.asm
;;; gcc -o talkToArduino arduino-serial.c hw8.o
;;;
;;; ----------------------- EXTERN LABELS -----------------------
extern serialport_writebyte ; int function
extern serialport_write ; int function
extern serialport_read_until ; int function
extern displayBuffer ; int function
extern buf
extern byte
;;; ----------------------- 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
;;; --- MACRO -----------------------------------------------
;;; openFile filename, handle
%macro openFile 2
mov eax,SYS_OPEN
mov ebx,%1
mov ecx, O_RDONLY
mov edx, S_IRUSR|S_IWUSR|S_IXUSR
int 0x80
test eax,eax
jns %%readFile
print2 "Could not open file"
mov eax,SYS_EXIT
mov ebx,0
int 0x80 ; final system call
%%readFile:
mov %2, eax ; save handle
%endmacro
;;; --- MACRO -----------------------------------------------
;;; readFile handle, buffer, buffer-length, number-bytes-read
%macro readFile 4
mov eax,SYS_READ
mov ebx,%1 ; file descriptor in bx
mov ecx,%2 ; buffer address
mov edx,%3 ; max number of bytes to read
int 0x80
mov %4,eax ; save number bytes read
%endmacro
;;; --- MACRO -----------------------------------------------
;;; close handle
%macro close 1
mov eax,SYS_CLOSE
mov ebx,%1
int 0x80
%endmacro
;; -------------------------
;; data segment
;; -------------------------
section .data
fileName db "arduino.cmd",0
handle dd 0
noRead dd 0 ; to store # of chars read from file
section .bss
MAXBUF equ 100000
buffer resb MAXBUF ; 100,000 bytes of storage
;; -------------------------
;; code area
;; -------------------------
section .text
global asm_main
asm_main:
;;-----------------READ FILE------------------
;;; open the file and put its contents in Buffer.
;;; keep track of # of bytes read in noRead
openFile fileName, [handle]
readFile [handle], buffer, MAXBUF, [noRead]
close [handle]
;;---------EXAMINE EACH CHARACTER------------
mov ebx,buffer ;address of first character of one command
mov esi,0 ;number of characters in this command
;; loop for every character in the command file
mov ecx,[noRead]
for:
mov al,[ebx+esi]
cmp al,10
je nextCmd ;if not end of command:
inc esi ;read next character
jmp endloop
nextCmd: ;if end of command:
mov byte[ebx+esi],0 ;terminate command with 0
inc esi
pushad
mov eax,ebx ;write command to buf
mov ecx,esi
call copyMsg
call serialport_write
popad
add ebx,esi ;point ebx to start of next command
mov esi,0 ;set length of new command to 0
endloop:
loop for
ret
;;; ----------------------------------------------------------------
;;; delay100ms: delay 100 ms, or 0.1 sec
;;; modified register: none
;;; ----------------------------------------------------------------
delay100ms:
pushad
mov ecx, 100000000 ; 2 x 100,000,000 cycles (assuming 2GHz)
.for add eax, 1 ; 1 cycle
loop .for ; 1 cycle
popad
ret
;;; ----------------------------------------------------------------
;;; copyMsg: puts array whose address in eax in external buffer
;;; number of bytes shoudl be in ecx.
;;; modified register: none
;;; ----------------------------------------------------------------
copyMsg:
pushad
mov esi, eax ; source buffer
mov edi, buf ; destination buffer in C program
.for mov al, [esi]
mov [edi], al
inc esi
inc edi
loop .for
popad
ret