231a-alassemblerprogram

From CSclasswiki
Jump to: navigation, search
Final Project
Eric Salinger
This program takes 2 arduino units that are interconnected and
determines how well their digital pins work
to assemble and run --
nasm -f elf project.asm
gcc -o project project.o asm_io.o arduino-serial.c
./project -p (address of arduino A) -z (address of arduino B)


%include "asm_io.inc"

extern serialport_writebyte ; int function extern serialport_write  ; int function extern serialport_read_until ; int function extern displayBuffer  ; int function

extern serialport2_writebyte ; int function extern serialport2_write  ; int function extern serialport2_read_until ; int function extern rand

extern outt ; output buffer

extern buf ; buffer for arduino a extern byte extern buf2 ; buffer for arduino b


section .text intro db "Arduino Tester by Eric Salinger.", 0x0a, 0x0a, 0x00 read db "r" readinga db "Reading A...", 0x00 done db "Done", 0x00 readingb db "Reading B...", 0x00

set trials to the number of times you want to execute

trials db 150 pins equ 12 WRITE equ 4 STDOUT equ 1 on dd 49 off dd 48 trial dd "Test #", 0x00 writea dd "Writing to A", 0x00 writeb dd "Writing to B", 0x00

desc db 0x0a, 0x0a," Below is the table of errors over the specified number of trials (Default 15). Each number corresponds to the number of failures on a specific pin on each arduino, respectively.",0x0a, 0x0a, "Write Errors:", 0x00 desc2 db "Read Errors:", 0x00 msg1 db "Chip Pin 2 Pin 3 Pin 4 Pin 5 Pin 6 Pin 7 Pin 8 Pin 9 Pin 10 Pin 11 Pin 12 Pin 13", 0x00 ard1 db "Adruino A", 0x00 ard2 db "Arduino B", 0x00 tab db " ", 0x00


expected - what we should get from the arduino
errorsa - how many write errors there are with pin n on arduino a.
errorsb - how many write errors there are with pin n on arduino b.
errorsar - how many read errors there are with pin n on arduino a.
errorsbr - how many read errors there are with pin n on arduino b.

section .bss expected resb 14 errorsa resb 13 errorsb resb 13 errorsar resb 13 errorsbr resb 13


section .data global asm_main

asm_main:

print an intro

mov eax, intro mov ecx, 35 call print_string call print_nl mov eax, 0

zero everything out

call zero .loop:

get new random pin values.

call initializepins pushad

print some stuff, protecting all variables by putting them in the stack

mov eax, trial mov ecx, 12 call print_string popad

print an integer, corresponding to the number of completed trials.

call print_int call print_nl

edx = 0 means 'use arduino a'

mov edx, 0

output our values to the arduino a, then see what happens.

call outputvalues call collectvalues

do the same thing for arduino b.

inc edx call outputvalues call collectvalues

increment number of trials

inc eax cmp al, byte[trials] jne .loop

all done, print a report

call report call exit


zeroes everything

zero: pushad mov eax, 0 mov ebx, "0" mov ecx, 0

.loop mov [expected + eax], bl mov [errorsa + eax], cl mov [errorsb + eax], cl mov [errorsbr +eax], cl mov [errorsar +eax], cl mov [buf+eax], bl mov [buf2+eax], bl mov [outt+eax], bl inc eax cmp eax, 13 jne .loop

popad ret


Collects the values from the arduino, then processes them.

collectvalues: pushad mov ebx, "0" mov esi, 0

pushad mov eax, readinga mov ecx, 12

call print_string call print_nl

unimportant
mov eax, 0
mov al, 49
mov byte[expected +1], al
mov byte[outt + 1], al
mov al, 48
mov byte[expected +12], al
mov byte[outt + 12], al


call serialport_write

call serialport_read_until mov eax, done mov ecx, 5

call print_string call print_nl

mov eax, readingb

call print_string call print_nl

mov eax, 0

unimportant
mov al, 49
mov byte[outt + 2], al
mov al, 48
mov byte[outt + 12], al


call serialport2_read_until

mov eax, done mov ecx, 5

call serialport2_write

call print_string call print_nl popad


.loopa mov ebx, 0 mov eax, 0

       mov     bl, byte [buf + esi]
debug output
mov cl, byte [expected+esi]
mov al, bl
call print_char
mov al, cl
call print_char
call print_nl

;;;;; get input in input cmp bl, byte[expected + esi] jne .adda

.aend inc esi mov ecx, esi cmp cl, pins jne .loopa

call print_nl

jmp .donea

.donea mov esi, 0

mov ebx, "1"

.loopb mov ebx, 0 mov eax, 0

       mov     bl, byte [buf2 +esi]
debug output
mov al, bl
call print_char

;;; get inputb in input cmp bl, byte [expected + esi] jne .addb

.bend inc esi mov ecx, esi cmp cl, pins jne .loopb

debug output
call print_nl

jmp .return

.return popad ret

.adda cmp dl, 0 jne .addar mov eax, 0 mov al, byte[errorsa + esi] inc al mov byte [errorsa +esi], al jmp .aend

.addb cmp dl, 0 je .addbr mov eax, 0 mov al, byte [errorsb + esi] inc al mov byte [errorsb + esi], al jmp .bend

.addbr mov eax, 0 mov al, byte [errorsbr +esi] inc al mov byte [errorsbr +esi], al jmp .bend

.addar mov eax, 0 mov al, byte [errorsar +esi] inc al mov byte [errorsar +esi], al jmp .aend

initializepins: pushad mov ebx, 0 mov ecx, pins .loop cmp eax, 1 je .one cmp eax, 0 je .zero

jmp .random .cont inc ebx loop .loop

mov esi, expected mov byte [esi +13], 0x0a mov byte [outt +12], 0x0a

mov byte [buf2+13], 0x0a

jmp .return

.one mov esi, expected mov edx, "1" mov byte [esi +ebx], dl mov byte [outt +ebx], dl

mov byte [buf2+ebx], dl

jmp .cont

.zero mov esi, expected mov edx, "0" mov byte [esi +ebx], dl mov byte [ebx +outt], dl

mov byte [ebx +buf2], dl

jmp .cont

.random ;;; for some reason loops will not work when assigning random variables - the program seg faults.

the following code is not pretty to look at.

mov esi, expected

call rand and eax, 1 add eax, 48 mov byte [esi+0], al

               mov     byte [outt+ 0], al
mov byte [buf2+ 0], al


call rand and eax, 1 add eax, 48 mov byte [esi+1], al

               mov     byte [outt+ 1], al
mov byte [buf2+ 1], al

call rand and eax, 1 add eax, 48 mov byte [esi+2], al

               mov     byte [outt+ 2], al
mov byte [buf2+ 2], al


call rand and eax, 1 add eax, 48 mov byte [esi+3], al

               mov     byte [outt+ 3], al
mov byte [buf2+ 3], al


call rand and eax, 1 add eax, 48 mov [esi+4], al

               mov     byte [outt+ 4], al
mov byte [buf2+ 4], al


call rand and eax, 1 add eax, 48 mov [esi+5], al

               mov     byte [outt+ 5], al
mov byte [buf2+ 5], al


call rand

               and     eax, 1
               add     eax, 48
               mov     [esi+6], al
               mov     byte [outt+ 6], al
mov byte [buf2+ 6], al


call rand

               and     eax, 1
               add     eax, 48
               mov     [esi+7], al
               mov     byte [outt+ 7], al
mov byte [buf2+ 7], al


call rand

               and     eax, 1
               add     eax, 48
               mov     [esi+8], al
               mov     byte [outt+ 8], al
mov byte [buf2+ 8], al


call rand

               and     eax, 1
               add     eax, 48
               mov     [esi+9], al
               mov     byte [outt+ 9], al
mov byte [buf2+ 9], al


call rand

               and     eax, 1
               add     eax, 48
               mov     [esi+10], al
               mov     byte [outt+ 10], al
mov byte [buf2+ 10], al


call rand

               and     eax, 1
               add     eax, 48
               mov     [esi+11], al
               mov     byte [outt+ 11], al
mov byte [buf2+ 11], al


call rand
and eax, 1
add eax, 48
mov [esi+12], al
mov byte [outt+ 12], al
mov byte [buf2+12], al



mov byte [esi +12], 0x0a mov byte [outt+12], 0x0a

mov byte [buf2+12], 0x0a

jmp .return


.return popad ret

write things to the serial ports, and give us some information too.

outputvalues: pushad


cmp edx, 0 jne .altwrite jmp .write .altwrite mov eax, writeb mov ecx, 12 call print_string call print_nl

               mov     eax, expected
               mov     ecx, 13

call serialport2_write mov eax, done mov ecx, 5 call print_string call print_nl jmp .return .write mov eax, writea mov ecx, 12 call print_string call print_nl mov eax, expected mov ecx, 13

call serialport_write mov eax, done mov ecx, 5 call print_string call print_nl jmp .return

.return popad ret

exit: mov eax, 1 mov ebx, 0 int 0x80


Give us an overview of what's happened. Total number of errors, etc.

report: mov eax, desc call print_string call print_nl call print_nl mov eax, msg1 call print_string call print_nl mov eax, ard1 call print_string

jmp .return

mov esi, 0

.ard1 mov eax, tab call print_string mov eax, 0 mov al, byte [esi + errorsa] call print_int inc esi cmp esi, pins jne .ard1 call print_nl

mov esi, 0 mov eax, ard2 call print_string .ard2 mov eax, tab call print_string mov eax, 0 mov al, byte [esi + errorsb] call print_int inc esi cmp esi, pins jne .ard2

call print_nl call print_nl

       mov     eax, desc2
       call    print_string
       call    print_nl
       mov     eax, msg1
       call    print_string
       call    print_nl

mov eax, ard1 call print_string mov esi, 0


.ard1a mov eax, tab call print_string mov eax, 0 mov al, byte [esi+errorsar] call print_int inc esi cmp esi, pins jne .ard1a call print_nl

       mov     eax, ard2
       call    print_string
       mov     esi, 0
       .ard2a
       mov     eax, tab
       call    print_string
       mov     eax, 0
       mov     al, byte [esi+errorsbr]
       call    print_int
       inc     esi
       cmp     esi, pins

jne .ard2a call print_nl

.return ret