Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- KBBuffer: .skip 512
- strPrompt: .asciz "Enter a string:\n"
- nL: .asciz "\n"
- lengthOutputPrompt: .asciz "1. Length of \'"
- lengthOutputPrompt2: .asciz "\' is: "
- lengthOutput: .skip 12
- // Test variables
- // endsWith
- endsWithTestOutput: .asciz "10. Does \'"
- endsWithTestOutput1: .asciz "\' end with \'"
- endsWithTestOutput2: .asciz "\' ? "
- endsWithTestSuccess: .asciz "yes"
- endsWithTestFail: .asciz "no"
- endsWithTest: .asciz "in the hat."
- // indexOf subroutines
- indexOf_1Output: .asciz "11. The index of the first occurence of \'g' in \'green eggs and ham\' is: "
- indexOf_2Output: .asciz "12. The index of the first occurence of \'g' starting from index 9 in \'green eggs and ham\' is: "
- indexOf_3Output: .asciz "13. The index of the first occurence of \'eggs`' in \'green eggs and ham\' is: "
- indexOf_3Str: .asciz "eggs"
- // lastIndexOf subroutines
- lastIndexOf_1Output: .asciz "14. The index of the last occurence of \'g\' in \'green eggs and ham\' is: "
- lastIndexOf_2Output: .asciz "15. The index of the last occurence of \'g\' starting from index 9 in \'green eggs and ham\' is: "
- lastIndexOf_3Output: .asciz "16. The index of the last occurence of \'eggs\' in \'green eggs and ham\' is: "
- //replace
- replace_Output: .asciz "17. Replace all occurences of \'C\' in str1 with \'B\': "
- // upper/lower case
- toLowerCase_Output: .asciz "18. Str1 toLowerCase: "
- toUpperCase_Output: .asciz "19. Str1 toUpperCase: "
- // concat
- concat_Output: .asciz "20. Str1 + \' \' + Str2: "
- strSpace: .asciz " "
- @@@@ CODE @@
- .text
- .global _start
- .extern malloc
- _start:
- @@@@@@ GET FIRST INPUT @@@@@@
- getFirstInput:
- ldr R1,=strPrompt
- bl putstring
- ldr R1, =KBBuffer
- mov R2, #512
- bl getstring // R0 = number of characters read from keyboard
- add R0, R0, #1 // R0 = R0 + 1 ( for null terminator )
- bl malloc // R0 = address of memory reserved
- mov R6, R0 // R6 = R0 (address of memory reserved)
- ldr R1, =KBBuffer
- getFirstInput_Loop:
- ldrb R5, [R1], #1 // R5 = byte of *R1, R1++
- cmp R5, #0 // check for null
- strb R5, [R0], #1 // *R0 = R5, R0++
- bne getFirstInput_Loop
- ldr R1,=nL
- bl putstring
- @@@@@@ GET SECOND INPUT @@@@@@
- getSecondInput:
- ldr R1,=strPrompt
- bl putstring
- ldr R1, =KBBuffer
- mov R2, #512
- bl getstring // R0 = number of characters read from keyboard
- add R0, R0, #1 // R0 = R0 + 1 ( for null terminator )
- bl malloc // R0 = address of memory reserved
- mov R7, R0 // R7 = R0 (address of second input string)
- ldr R1, =KBBuffer
- getSecondInput_Loop:
- ldrb R5, [R1], #1 // R5 = byte of *R1, R1++
- cmp R5, #0 // check for null
- strb R5, [R0], #1 // *R0 = R5, R0++
- bne getSecondInput_Loop // Keep looping until null terminator
- ldr R1,=nL
- bl putstring
- @@@@@@ GET THIRD INPUT @@@@@@
- getThirdInput:
- ldr R1,=strPrompt
- bl putstring
- ldr R1, =KBBuffer
- mov R2, #512
- bl getstring // R0 = number of characters read from keyboard
- add R0, R0, #1 // R0 = R0 + 1 ( for null terminator )
- bl malloc // R0 = address of memory reserved
- mov R8, R0 // R7 = R0 (address of second input string)
- ldr R1, =KBBuffer
- getThirdInput_Loop:
- ldrb R5, [R1], #1 // R5 = byte of *R1, R1++
- cmp R5, #0 // check for null
- strb R5, [R0], #1 // *R0 = R5, R0++
- bne getThirdInput_Loop // Keep looping until null terminator
- ldr R1,=nL
- bl putstring
- @@@@@@@@@ BEGIN TESTS @@@@@@@@@
- // Remember:
- //
- // R6 = &str1,
- // R7 = &str2,
- // R8 = &str3
- @@@ #1: Output length of str1, str2, and str3
- ldr R1,=lengthOutputPrompt
- bl putstring // output "Length of "
- mov R1, R6
- bl putstring // output str1 to console
- bl length // R0 = length(str1)
- ldr R1,=lengthOutput
- bl intasc32 // lengthOutput = asciz(length(str1))
- ldr R1,=lengthOutputPrompt2 // " is: "
- bl putstring // Outputs " is: " to console
- ldr R1,=lengthOutput // R1 <- &lengthOutput
- bl putstring // Outputs length of str1 to console
- ldr R1,=nL
- bl putstring
- @@ #1: Output length of str2
- ldr R1,=lengthOutputPrompt
- bl putstring // output "Length of "
- mov R1, R7
- bl putstring // output str2 to console
- bl length // R0 = length(str2)
- ldr R1,=lengthOutput
- bl intasc32 // lengthOutput = asciz(length(str2))
- ldr R1,=lengthOutputPrompt2 // " is: "
- bl putstring // Outputs " is: " to console
- ldr R1,=lengthOutput // R1 <- &lengthOutput
- bl putstring // Outputs length of str2 to console
- ldr R1,=nL
- bl putstring
- @@ Output length of str3
- ldr R1,=lengthOutputPrompt
- bl putstring // output "Length of "
- mov R1, R8
- bl putstring // output str3 to console
- bl length // R0 = length(str3)
- ldr R1,=lengthOutput
- bl intasc32 // lengthOutput = asciz(length(str3))
- ldr R1,=lengthOutputPrompt2 // " is: "
- bl putstring // Outputs " is: " to console
- ldr R1,=lengthOutput // R1 <- &lengthOutput
- bl putstring // Outputs length of str3 to console
- ldr R1,=nL
- bl putstring
- @@@@ #10: endsWith(s1, "in the hat")
- ldr R1,=endsWithTestOutput
- bl putstring //Outputs "Does '" to console
- mov R1, R6
- bl putstring // outputs str1 to console
- ldr R1,=endsWithTestOutput1
- bl putstring // Outputs "' end with '" to console
- ldr R1,=endsWithTest
- bl putstring // Outputs "in the hat" to console
- ldr R1,=endsWithTestOutput2
- bl putstring // Outputs "' ? "
- mov R1, R6 // R1 = &str1
- ldr R2,=endsWithTest
- bl endsWith // R0 should be true (1)
- cmp R0, #1
- beq endsWith_Success
- bne endsWith_Fail
- endsWith_Success:
- ldr R1,=endsWithTestSuccess
- bl putstring
- b endsWith_Test_End
- endsWith_Fail:
- ldr R1,=endsWithTestFail
- bl putstring
- endsWith_Test_End:
- ldr R1,=nL
- bl putstring
- @@@@ #11: indexOf_1(s2, 'g') // output should be 7
- ldr R1,=indexOf_1Output
- bl putstring
- mov R1, R7 // R1 = &str2
- mov R2, #'g' // R2 = ascii('g')
- bl indexOf_1 // R0 = first occurence of 'g' in str2
- ldr R1,=lengthOutput // R1 <= &lengthOutput
- bl intasc32 // R0 = ascii(indexOf_1(str2))
- bl putstring // Output ascii(indexOf_1(str2))
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #12: indexOf_2(s2, 'g', 9) // output should be -1
- ldr R1,=indexOf_2Output
- bl putstring
- mov R1, R7 // R1 = &str2
- mov R2, #'g' // R2 = ascii('g')
- mov R3, #9 // Index to start from
- bl indexOf_2 // R0 = first occurence of 'g' in str2
- ldr R1,=lengthOutput // R1 <= &lengthOutput
- bl intasc32 // R0 <= ascii(indexOf_1(str2))
- bl putstring // Output ascii(indexOf_1(str2))
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #13: indexOf_3(s2, "eggs") // output should be 6
- ldr R1,=indexOf_3Output
- bl putstring
- mov R1, R7 // R1 = &str2
- ldr R2, =indexOf_3Str // R2 = asciz('egg')
- bl indexOf_3 // R0 = first occurence of 'eggs' in str2
- ldr R1,=lengthOutput // R1 <= &lengthOutput
- bl intasc32 // R0 <= ascii(indexOf_1(str2))
- bl putstring // Output ascii(indexOf_1(str2))
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #14: lastIndexOf_1(s2, 'g') // output should be 8
- ldr R1,=lastIndexOf_1Output
- bl putstring
- mov R1, R7 // R1 = &str2
- mov R2,#'g' // R2 = 'g'
- bl lastIndexOf_1 // R0 = last occurence of 'g' in str2
- ldr R1,=lengthOutput // R1 <= &lengthOutput
- bl intasc32 // R0 <= ascii(indexOf_1(str2))
- bl putstring // Output ascii(indexOf_1(str2))
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #15: lastIndexOf_2(s2, 'g', 9) // output should be -1
- ldr R1,=lastIndexOf_2Output
- bl putstring
- mov R1, R7 // R1 = &str2
- mov R2, #'g' // R2 = 'g'
- mov R3, #9 // Index to start searching from
- bl lastIndexOf_2 // R0 = last occurence of 'g' in str2
- ldr R1,=lengthOutput // R1 <= &lengthOutput
- bl intasc32 // R0 <= ascii(indexOf_1(str2))
- bl putstring // Output ascii(indexOf_1(str2))
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #16: lastIndexOf_3(s2, "egg") // output should be 6
- ldr R1,=lastIndexOf_3Output
- bl putstring
- mov R1, R7 // R1 = &str2
- ldr R2, =indexOf_3Str
- bl lastIndexOf_3 // R0 = last occurence of 'eggs' in str2
- ldr R1,=lengthOutput // R1 <= &lengthOutput
- bl intasc32 // R0 <= ascii(indexOf_1(str2))
- bl putstring // Output ascii(indexOf_1(str2))
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #17: replace(s1, 'C', 'B') // output should be &("Bat in the hat.")
- ldr R1,=replace_Output
- bl putstring
- mov R1, R6 // R1 = &str1
- mov R2, #'C' // R2 = ascii "C"
- mov R3, #'B' // R3 = ascii "B"
- bl replace // replaces all occurences of 'C' with 'B' in s1
- mov R1, R0 // move returned string address into R1 for output
- bl putstring
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #18: toLowerCase(s1) // Output should be "bat in the hat."
- ldr R1,=toLowerCase_Output
- bl putstring
- mov R1, R6 // R1 = &str1
- bl toLowerCase // Converts str1 into all lowercase
- mov R1, R0 // move returned string into R1 for output
- bl putstring
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #19: toUpperCase(s1) // Output should be "BAT IN THE HAT."
- ldr R1,=toUpperCase_Output
- bl putstring
- mov R1, R6 // R1 = &str1
- bl toUpperCase // Converts str1 into all lowercase
- mov R1, R0 // move returned string into R1 for output
- bl putstring
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @@@@ #20: concat(s1, " ") // Output should be "BAT IN THE HAT."
- ldr R1,=concat_Output
- bl putstring
- mov R1, R6 // R1 = &str1
- ldr R2, =strSpace // R2 = &(" ")
- bl concat // Concatenate Str1 with a space
- mov R1, R0
- mov R2, R7 // R2 = &str2
- bl concat // Concatenate str1 + str2
- mov R1, R0 // move returned string into R1 for output
- bl putstring
- ldr R1,=nL // R1 <= &nL
- bl putstring // Output newline
- @ End driver program
- end:
- mov r0, #0 @ Exit status code set to 0
- mov r7, #1 @ Service command code (1), will terminate this program
- svc 0 @ Issue Linux command to terminate program
- .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement