Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 10.14 KB | None | 0 0
  1. .data
  2. KBBuffer: .skip 512
  3. strPrompt: .asciz "Enter a string:\n"
  4. nL: .asciz "\n"
  5.  
  6.  
  7. lengthOutputPrompt: .asciz "1. Length of \'"
  8. lengthOutputPrompt2: .asciz "\' is: "
  9. lengthOutput: .skip 12
  10.  
  11.  
  12. // Test variables
  13. // endsWith
  14. endsWithTestOutput: .asciz "10. Does \'"
  15. endsWithTestOutput1: .asciz "\' end with \'"
  16. endsWithTestOutput2: .asciz "\' ? "
  17. endsWithTestSuccess: .asciz "yes"
  18. endsWithTestFail:    .asciz "no"
  19. endsWithTest: .asciz "in the hat."
  20.  
  21. // indexOf subroutines
  22. indexOf_1Output:  .asciz "11. The index of the first occurence of \'g' in \'green eggs and ham\' is: "
  23. indexOf_2Output:  .asciz "12. The index of the first occurence of \'g' starting from index 9 in \'green eggs and ham\' is: "
  24. indexOf_3Output:  .asciz "13. The index of the first occurence of \'eggs`' in \'green eggs and ham\' is: "
  25. indexOf_3Str: .asciz "eggs"
  26.  
  27. // lastIndexOf subroutines
  28. lastIndexOf_1Output:  .asciz "14. The index of the last occurence of \'g\' in \'green eggs and ham\' is: "
  29. lastIndexOf_2Output:  .asciz "15. The index of the last occurence of \'g\' starting from index 9 in \'green eggs and ham\' is: "
  30. lastIndexOf_3Output:  .asciz "16. The index of the last occurence of \'eggs\' in \'green eggs and ham\' is: "
  31.  
  32. //replace
  33. replace_Output: .asciz "17. Replace all occurences of \'C\' in str1 with \'B\': "
  34.  
  35. // upper/lower case
  36. toLowerCase_Output: .asciz "18. Str1 toLowerCase: "
  37. toUpperCase_Output: .asciz "19. Str1 toUpperCase: "
  38.  
  39. // concat
  40. concat_Output: .asciz "20. Str1 + \' \' + Str2: "
  41. strSpace: .asciz " "
  42.  
  43. @@@@ CODE @@
  44.     .text
  45.     .global _start
  46.     .extern malloc
  47. _start:
  48.  
  49. @@@@@@ GET FIRST INPUT @@@@@@
  50. getFirstInput:
  51.   ldr R1,=strPrompt
  52.     bl putstring
  53.  
  54.     ldr R1, =KBBuffer
  55.     mov R2, #512
  56.     bl getstring  // R0 = number of characters read from keyboard
  57.     add R0, R0, #1 // R0 = R0 + 1 ( for null terminator )
  58.     bl malloc   // R0 = address of memory reserved
  59.     mov R6, R0      // R6 = R0 (address of memory reserved)
  60.     ldr R1, =KBBuffer
  61. getFirstInput_Loop:
  62.     ldrb R5, [R1], #1 // R5 = byte of *R1, R1++
  63.     cmp R5, #0    // check for null
  64.     strb R5, [R0], #1 // *R0 = R5, R0++
  65.     bne getFirstInput_Loop
  66.   ldr R1,=nL
  67.     bl putstring
  68.  
  69. @@@@@@ GET SECOND INPUT @@@@@@
  70. getSecondInput:
  71.     ldr R1,=strPrompt
  72.     bl putstring
  73.  
  74.     ldr R1, =KBBuffer
  75.     mov R2, #512
  76.     bl getstring  // R0 = number of characters read from keyboard
  77.     add R0, R0, #1 // R0 = R0 + 1 ( for null terminator )
  78.     bl malloc       // R0 = address of memory reserved
  79.     mov R7, R0      // R7 = R0 (address of second input string)
  80.     ldr R1, =KBBuffer
  81. getSecondInput_Loop:
  82.     ldrb R5, [R1], #1 // R5 = byte of *R1, R1++
  83.     cmp R5, #0          // check for null
  84.     strb R5, [R0], #1 // *R0 = R5, R0++
  85.     bne getSecondInput_Loop // Keep looping until null terminator
  86.     ldr R1,=nL
  87.     bl putstring
  88.  
  89.     @@@@@@ GET THIRD INPUT @@@@@@
  90.     getThirdInput:
  91.         ldr R1,=strPrompt
  92.         bl putstring
  93.  
  94.         ldr R1, =KBBuffer
  95.         mov R2, #512
  96.         bl getstring  // R0 = number of characters read from keyboard
  97.         add R0, R0, #1 // R0 = R0 + 1 ( for null terminator )
  98.         bl malloc       // R0 = address of memory reserved
  99.         mov R8, R0      // R7 = R0 (address of second input string)
  100.         ldr R1, =KBBuffer
  101.     getThirdInput_Loop:
  102.         ldrb R5, [R1], #1 // R5 = byte of *R1, R1++
  103.         cmp R5, #0          // check for null
  104.         strb R5, [R0], #1 // *R0 = R5, R0++
  105.         bne getThirdInput_Loop // Keep looping until null terminator
  106.         ldr R1,=nL
  107.         bl putstring
  108.  
  109.  
  110.     @@@@@@@@@ BEGIN TESTS @@@@@@@@@
  111.     // Remember:
  112.         //
  113.         // R6 = &str1,
  114.     // R7 = &str2,
  115.     // R8 = &str3
  116.  
  117.     @@@ #1: Output length of str1, str2, and str3
  118.     ldr R1,=lengthOutputPrompt
  119.     bl putstring // output "Length of "
  120.  
  121.     mov R1, R6
  122.     bl putstring     // output str1 to console
  123.     bl length       // R0 = length(str1)
  124.  
  125.     ldr R1,=lengthOutput
  126.     bl intasc32  // lengthOutput = asciz(length(str1))
  127.  
  128.     ldr R1,=lengthOutputPrompt2 // " is: "
  129.     bl putstring // Outputs " is: " to console
  130.  
  131.     ldr R1,=lengthOutput // R1 <- &lengthOutput
  132.     bl putstring // Outputs length of str1 to console
  133.     ldr R1,=nL
  134.     bl putstring
  135.  
  136.     @@ #1: Output length of str2
  137.     ldr R1,=lengthOutputPrompt
  138.     bl putstring // output "Length of "
  139.  
  140.     mov R1, R7
  141.     bl putstring     // output str2 to console
  142.     bl length       // R0 = length(str2)
  143.  
  144.     ldr R1,=lengthOutput
  145.     bl intasc32  // lengthOutput = asciz(length(str2))
  146.  
  147.     ldr R1,=lengthOutputPrompt2 // " is: "
  148.     bl putstring // Outputs " is: " to console
  149.  
  150.     ldr R1,=lengthOutput // R1 <- &lengthOutput
  151.     bl putstring // Outputs length of str2 to console
  152.     ldr R1,=nL
  153.     bl putstring
  154.  
  155.     @@ Output length of str3
  156.     ldr R1,=lengthOutputPrompt
  157.     bl putstring // output "Length of "
  158.  
  159.     mov R1, R8
  160.     bl putstring     // output str3 to console
  161.     bl length       // R0 = length(str3)
  162.  
  163.     ldr R1,=lengthOutput
  164.     bl intasc32  // lengthOutput = asciz(length(str3))
  165.  
  166.     ldr R1,=lengthOutputPrompt2 // " is: "
  167.     bl putstring // Outputs " is: " to console
  168.  
  169.     ldr R1,=lengthOutput // R1 <- &lengthOutput
  170.     bl putstring // Outputs length of str3 to console
  171.     ldr R1,=nL
  172.     bl putstring
  173.  
  174.  
  175.     @@@@ #10: endsWith(s1, "in the hat")
  176.     ldr R1,=endsWithTestOutput
  177.     bl putstring //Outputs "Does '" to console
  178.     mov R1, R6
  179.     bl putstring // outputs str1 to console
  180.     ldr R1,=endsWithTestOutput1
  181.     bl putstring // Outputs "' end with '" to console
  182.     ldr R1,=endsWithTest
  183.     bl putstring // Outputs "in the hat" to console
  184.     ldr R1,=endsWithTestOutput2
  185.     bl putstring // Outputs "' ? "
  186.  
  187.     mov R1, R6 // R1 = &str1
  188.     ldr R2,=endsWithTest
  189.     bl endsWith // R0 should be true (1)
  190.     cmp R0, #1
  191.     beq endsWith_Success
  192.     bne endsWith_Fail
  193. endsWith_Success:
  194.     ldr R1,=endsWithTestSuccess
  195.     bl putstring
  196.     b endsWith_Test_End
  197. endsWith_Fail:
  198.     ldr R1,=endsWithTestFail
  199.     bl putstring
  200. endsWith_Test_End:
  201.     ldr R1,=nL
  202.     bl putstring
  203.  
  204.  
  205.     @@@@ #11: indexOf_1(s2, 'g') // output should be 7
  206.     ldr R1,=indexOf_1Output
  207.     bl putstring
  208.     mov R1, R7 // R1 = &str2
  209.     mov R2, #'g' // R2 = ascii('g')
  210.     bl indexOf_1 // R0 = first occurence of 'g' in str2
  211.  
  212.     ldr R1,=lengthOutput // R1 <= &lengthOutput
  213.     bl intasc32                  // R0 = ascii(indexOf_1(str2))
  214.     bl putstring                 // Output ascii(indexOf_1(str2))
  215.     ldr R1,=nL                   // R1 <= &nL
  216.     bl putstring                 // Output newline
  217.  
  218.  
  219.     @@@@ #12: indexOf_2(s2, 'g', 9) // output should be -1
  220.     ldr R1,=indexOf_2Output
  221.     bl putstring
  222.     mov R1, R7   // R1 = &str2
  223.     mov R2, #'g' // R2 = ascii('g')
  224.     mov R3, #9   // Index to start from
  225.     bl indexOf_2 // R0 = first occurence of 'g' in str2
  226.  
  227.     ldr R1,=lengthOutput // R1 <= &lengthOutput
  228.     bl intasc32                  // R0 <= ascii(indexOf_1(str2))
  229.     bl putstring                 // Output ascii(indexOf_1(str2))
  230.     ldr R1,=nL                   // R1 <= &nL
  231.     bl putstring                 // Output newline
  232.  
  233.  
  234.  
  235.     @@@@ #13: indexOf_3(s2, "eggs") // output should be 6
  236.     ldr R1,=indexOf_3Output
  237.     bl putstring
  238.     mov R1, R7                          // R1 = &str2
  239.     ldr R2, =indexOf_3Str   // R2 = asciz('egg')
  240.     bl indexOf_3                      // R0 = first occurence of 'eggs' in str2
  241.  
  242.     ldr R1,=lengthOutput // R1 <= &lengthOutput
  243.     bl intasc32                  // R0 <= ascii(indexOf_1(str2))
  244.     bl putstring                 // Output ascii(indexOf_1(str2))
  245.     ldr R1,=nL                   // R1 <= &nL
  246.     bl putstring                 // Output newline
  247.  
  248.     @@@@ #14: lastIndexOf_1(s2, 'g') // output should be 8
  249.     ldr R1,=lastIndexOf_1Output
  250.     bl putstring
  251.     mov R1, R7                          // R1 = &str2
  252.     mov R2,#'g'                         // R2 = 'g'
  253.     bl lastIndexOf_1                      // R0 = last occurence of 'g' in str2
  254.  
  255.     ldr R1,=lengthOutput // R1 <= &lengthOutput
  256.     bl intasc32                  // R0 <= ascii(indexOf_1(str2))
  257.     bl putstring                 // Output ascii(indexOf_1(str2))
  258.     ldr R1,=nL                   // R1 <= &nL
  259.     bl putstring                 // Output newline
  260.  
  261.     @@@@ #15: lastIndexOf_2(s2, 'g', 9) // output should be -1
  262.     ldr R1,=lastIndexOf_2Output
  263.     bl putstring
  264.     mov R1, R7                              // R1 = &str2
  265.     mov R2, #'g'                            // R2 = 'g'
  266.     mov R3, #9                              // Index to start searching from
  267.     bl lastIndexOf_2                    // R0 = last occurence of 'g' in str2
  268.  
  269.     ldr R1,=lengthOutput // R1 <= &lengthOutput
  270.     bl intasc32                  // R0 <= ascii(indexOf_1(str2))
  271.     bl putstring                 // Output ascii(indexOf_1(str2))
  272.     ldr R1,=nL                   // R1 <= &nL
  273.     bl putstring                 // Output newline
  274.  
  275.     @@@@ #16: lastIndexOf_3(s2, "egg") // output should be 6
  276.     ldr R1,=lastIndexOf_3Output
  277.     bl putstring
  278.     mov R1, R7                              // R1 = &str2
  279.     ldr R2, =indexOf_3Str
  280.     bl lastIndexOf_3                    // R0 = last occurence of 'eggs' in str2
  281.  
  282.     ldr R1,=lengthOutput // R1 <= &lengthOutput
  283.     bl intasc32                  // R0 <= ascii(indexOf_1(str2))
  284.     bl putstring                 // Output ascii(indexOf_1(str2))
  285.     ldr R1,=nL                   // R1 <= &nL
  286.     bl putstring                 // Output newline
  287.  
  288.  
  289.     @@@@ #17: replace(s1, 'C', 'B') // output should be &("Bat in the hat.")
  290.     ldr R1,=replace_Output
  291.     bl putstring
  292.     mov R1, R6                              // R1 = &str1
  293.     mov R2, #'C'                            // R2 = ascii "C"
  294.     mov R3, #'B'                            // R3 = ascii "B"
  295.     bl replace                              // replaces all occurences of 'C' with 'B' in s1
  296.  
  297.     mov R1, R0 // move returned string address into R1 for output
  298.     bl putstring
  299.     ldr R1,=nL                   // R1 <= &nL
  300.     bl putstring                 // Output newline
  301.  
  302.  
  303.     @@@@ #18: toLowerCase(s1) // Output should be "bat in the hat."
  304.     ldr R1,=toLowerCase_Output
  305.     bl putstring
  306.     mov R1, R6                              // R1 = &str1
  307.     bl toLowerCase                    // Converts str1 into all lowercase
  308.  
  309.     mov R1, R0                  // move returned string into R1 for output
  310.     bl putstring
  311.     ldr R1,=nL                   // R1 <= &nL
  312.     bl putstring                 // Output newline
  313.  
  314.  
  315.     @@@@ #19: toUpperCase(s1) // Output should be "BAT IN THE HAT."
  316.     ldr R1,=toUpperCase_Output
  317.     bl putstring
  318.     mov R1, R6                              // R1 = &str1
  319.     bl toUpperCase                    // Converts str1 into all lowercase
  320.  
  321.     mov R1, R0                  // move returned string into R1 for output
  322.     bl putstring
  323.     ldr R1,=nL                   // R1 <= &nL
  324.     bl putstring                 // Output newline
  325.  
  326.     @@@@ #20: concat(s1, " ") // Output should be "BAT IN THE HAT."
  327.     ldr R1,=concat_Output
  328.     bl putstring
  329.  
  330.     mov R1, R6                              // R1 = &str1
  331.     ldr R2, =strSpace                   // R2 = &(" ")
  332.     bl concat                     // Concatenate Str1 with a space
  333.     mov R1, R0
  334.     mov R2, R7          // R2 = &str2
  335.     bl concat                     // Concatenate str1 + str2
  336.  
  337.     mov R1, R0                  // move returned string into R1 for output
  338.     bl putstring
  339.     ldr R1,=nL                   // R1 <= &nL
  340.     bl putstring                 // Output newline
  341.  
  342.  
  343. @ End driver program
  344. end:
  345.   mov r0, #0 @ Exit status code set to 0
  346.   mov r7, #1 @ Service command code (1), will terminate this program
  347.   svc 0      @ Issue Linux command to terminate program
  348.   .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement