/*
 * @topic T0M300 MIPS assembly
 * @brief string length in MIPS assembly
 * reverse the characters in the string
 */
.data
    .eqv SERVICE_EXIT         10
    .eqv SERVICE_PRINT_INT    1
    .eqv READ_STRING_SERVICE  8
    .eqv PRINT_STRING_SERVICE 4
    .eqv EXIT_PROGRAM_SERVICE 10
    .eqv MAX_STRING_SIZE      80
    .eqv NEWLINE              0xA # \n

input:      .space  MAX_STRING_SIZE
msg_length: .asciiz "string length is: "
prompt:     .asciiz "enter a string: "
idx_tail:   .word   0
idx_front:  .word   0

.text
    li  $v0, PRINT_STRING_SERVICE
    la  $t0, prompt
    add $a0, $t0, $zero
    syscall

    li  $v0, READ_STRING_SERVICE
    la  $t0, input
    add $a0, $t0, $zero
    li  $a1, MAX_STRING_SIZE
    syscall

    # find the length of input string:
    move $t3, $zero       # init input buffer index
    li   $t1, NEWLINE
    
compare_char_2_newline:
    lb   $t2, input($t3)
    beq  $t1, $t2, length_is_ready # compare char with \n
    addi $t3, $t3, 1 # increment index
    j compare_char_2_newline
    
length_is_ready:
    li  $v0, PRINT_STRING_SERVICE
    la  $t0, msg_length
    add $a0, $t0, $zero
    syscall

    li  $v0, SERVICE_PRINT_INT
    add $a0, $t3, $zero  # load $a0
    syscall

    li  $v0, SERVICE_EXIT
    syscall