User input length wrong result in MIPS32

66 views Asked by At

Ok so we have a simple program that takes an input(string) from the user,max length is 1000, and then it prints the length of that string.But for some reason,result is not the correct one.For example if I enter word name i get result 5 , and if i enter word example i get 8 which of course is wrong.Can somebody tell me why is this happening?Here is my code:

.data
    buffer: .space 1000
    str1:  .asciiz "Enter string:"
    str2:  .asciiz "You wrote:\n"
    str3:  .asciiz "String length is: \n"
    CRLF: .asciiz "\n"

.text

main:
    la $a0, str1    # Load and print string asking for string
    li $v0, 4
    syscall

    la $a0, CRLF
    li $v0, 4
    syscall #change line

    li $v0, 8       # take in input

    la $a0, buffer  # load byte space into address
    li $a1, 1000   # allot the byte space for string

    move $t0, $a0   # save string to t0
    syscall

    loop:
    lb $t1 0($t0)
    beq $t1 $zero end
    addi $t0 $t0 1
    j loop

    end:
    la $t1 buffer
    sub $t3 $t0 $t1 #$t3 now contains the length of the string

    la $a0, str3    # Load and print string about string length
    li $v0, 4
    syscall



    move $a0,$t3
    li $v0,1
    syscall

    la $a0, CRLF
    li $v0, 4
    syscall #change line


    li $v0, 10      # end program
    syscall

A small extra question.How can we change this code to make input max length change dynamically according to what the user enters?

0

There are 0 answers