; Assessment, Excersise 1
;
; Program to print the contents of a file
; to the debug channel
;

                AREA    printFile, CODE, READONLY               ; declare code area
SWI_WriteC      EQU     0x0     ; output character in r0
SWI_Exit        EQU     0x11    ; finish program
SWI_Open        EQU     0x66    ; open a file or device
SWI_Close       EQU     0x68    ; close an open file or device
SWI_Read        EQU     0x6a    ; read from an open file or device
SWI_Flen        EQU     0x6c    ; returns the current length of an open file or device
readonly        EQU     0       ; open a file or device for reading
                ENTRY           ; code entry point
START           ADR     r0, FILENAME    ; set r0 to point to FILENAME
                MOV     r1, #readonly   ; set access mode to file as readonly
                SWI     SWI_Open        ; open the file pointed at by r0
                CMP     r0, #0  ; check for success on opening file
                SWIEQ   SWI_Exit        ; if file could not be opened then exit now
                MOV     r5, r0  ; save the file handle in r5 for later
                SWI     SWI_Flen        ; store the length of the file in r0
                CMP     r0, #1  ; check that the call was successful
                SWIEQ   SWI_Exit        ; if the call failed then exit now
                MOV     r2, r0          ; move the length of the file into r2
                MOV     r0, r5          ; move the file handle back into r0
                ADR     r1, FILECONTENTS        ; point r1 at a place for the contents of the file
                SWI     SWI_Read        ; read the data from the file
                CMP     r0, #0  ; check that the call was successful
                SWIEQ   SWI_Exit        ; if anything was left unread from the file then exit now

LOOP            LDRB    r0, [r1], #1    ; get the next byte
                CMP     r0, #0  ; check for 'null' character
                SWINE   SWI_WriteC      ; if not end, print ..
                BNE     LOOP            ; .. and loop back

                MOV     r0, r5  ; move the file handle back into r0
                SWI     SWI_Close       ; close the file
                SWI     SWI_Exit        ; end of execution

FILENAME = "turing.txt", 0      ; filename + null
FILECONTENTS = ""       ; arbitrary amount of memory for contents of file

                END