Capitalizing Strings and Accessing Files using Software Interrupts
.text
.global _start
_start:
@ Open input file
ldr r0, =inputfile @ set input filename
mov r1, #0 @ open for reading
swi 0x66 @ Open the file
bcs inputError @ if error opening, go to error
mov r4, r0 @ save input file handle in r4
@ Open output file
ldr r0, =outputfile @ set output filename
mov r1, #1 @ open for writing
swi 0x66 @ Open the file
bcs outputError @ if error opening, go to error
mov r5, r0 @ save output file handle in r5
readLoop:
@ Read char from input file
mov r0, r4 @ load input file handle
ldr r1, =buffer @ read in buffer
mov r2, #1024 @ read at most 1024 characters
swi 0x6a @ read string from file
bcs readEnd @ if empty file, end loop
ldr r1, =buffer @ point to buffer
mov r2, #0 @ index in buffer, start in zero
mov r3, #1 @ indicate if we should change to uppercase
convLoop:
ldrb r0, [r1, r2] @ load char from string
cmp r0, #0 @ see if we reached the end of string
beq writeFile @ if so, write string
cmp r0, #'A' @ if not letter
blt saveSpace
cmp r0, #'Z' @ if letter
ble letter
cmp r0, #'a' @ if not letter
blt saveSpace
cmp r0, #'z' @ if not letter
bgt saveSpace
cmp r3, #1 @ see if we need to capitalize
bne saveChar @ if not, don't capitalize
sub r0, r0, #32 @ convert character to lowercase
letter:
mov r3, #0 @ 0 to avoid capitals, we are inside a word
b saveChar @ save character
saveSpace:
mov r3, #1 @ 1 to capitalize next letter
mov r0, #32 @ load space character
saveChar:
strb r0, [r1, r2] @ save space in string
add r2, r2, #1 @ increment string index
b convLoop
writeFile:
mov r0, #10
strb r0, [r1, r2] @ save newline in string
add r2, r2, #1 @ increment string index
mov r0, #0
strb r0, [r1, r2] @ save end of string
@ Write string to ouput file
mov r0, r5 @ load output file handle
ldr r1, =buffer @ write from buffer
mov r2, #1024 @ write all characters
swi 0x69 @ write string to file
b readLoop @ read next string from file
readEnd:
@ Close input file
mov r0, r4 @ get input file handle
swi 0x68 @ close the file
@ Close output file
mov r0, r5 @ get output file handle
swi 0x68 @ close the file
@ print end message
mov r0, #1 @ print to stdout
ldr r1, =okMsg @ load ok message
swi 0x69 @ print the message
b exit @ exit the program
inputError:
mov r0, #1 @ print to stdout
ldr r1, =inputErrorMsg @ load error message
swi 0x69 @ print the error message
b exit
outputError:
mov r0, #1 @ print to stdout
ldr r1, =outputErrorMsg @ load error message
swi 0x69 @ print the error message
exit:
swi 0x11 @ Exit the program
.data
inputfile:
.asciz "input.txt"
outputfile:
.asciz "output.txt"
inputErrorMsg:
.asciz "Error opening the input file: \"input.txt\"\n"
outputErrorMsg:
.asciz "Error opening the ouutput file: \"output.txt\"\n"
okMsg:
.asciz "File \"output.txt\" has been created\n"
buffer:
.skip 1024
.end