Guest User

Untitled

a guest
Feb 12th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2. BufferSize: .word 32
  3. Newline: .asciiz "\r\n"
  4. Separator_Text: .asciiz " => "
  5. Prompt_Text: .asciiz "Enter a string: "
  6.  
  7. .text
  8. main:
  9.     jal prompt
  10.     jal print_separator
  11.     jal loop_start
  12.     jal print_string
  13.     j exit
  14.  
  15. prompt:
  16.     # Print prompt string
  17.     li $v0, 4
  18.     la $a0, Prompt_Text
  19.     syscall
  20.     # Alloc string buffer
  21.     li $v0, 9
  22.     lw $a0, BufferSize
  23.     syscall
  24.     # Read string input, $s0 contains beginning of string addr
  25.     move $s0, $v0
  26.     move $a0, $s0
  27.     li $v0, 8
  28.     lw $a1, BufferSize
  29.     syscall
  30.     # Reset index
  31.     li $s1, 0
  32.     # Return
  33.     jr $ra
  34.  
  35. # Preserve return address and begin loop
  36. loop_start:
  37.     move $s2, $ra
  38.     j loop
  39.  
  40. # Loop through characters in buffer
  41. loop:
  42.     lw $t0, BufferSize
  43.     bge $s1, $t0, loop_done
  44.     jal tolower
  45.     addi $s1, $s1, 1
  46.     j loop
  47.  
  48. # Return to preserved address
  49. loop_done:
  50.     jr $s2
  51.  
  52. # $s0 - Base String Address
  53. # $s1 - Byte Index
  54. tolower:
  55.     # Load byte
  56.     add $t0, $s0, $s1
  57.     lb $t1, ($t0)
  58.     # Check if uppercase character
  59.     li $t2, 65
  60.     blt $t1, $t2, tolower_skip
  61.     li $t2, 90
  62.     bgt $t1, $t2, tolower_skip
  63.     # Perform lowercase change
  64.     addi $t1, $t1, 32
  65.     # Store byte
  66.     sb $t1, ($t0)
  67.     # Return
  68.     jr $ra
  69.  
  70. tolower_skip:
  71.     jr $ra
  72.    
  73. # Print separator string
  74. print_separator:
  75.     li $v0, 4
  76.     la $a0, Separator_Text
  77.     syscall
  78.     jr $ra
  79.  
  80. # Print string @ addr in $s0
  81. print_string:
  82.     li $v0, 4
  83.     la $a0, ($s0)
  84.     syscall
  85.     jr $ra
  86.    
  87. exit:
  88.     li $v0, 10
  89.     syscall
Add Comment
Please, Sign In to add comment