When running the code, the minimum value error occurs. How can I fix it?

.data
prompt: .asciiz "Enter your first and last name: "
max_msg: .asciiz "\nMaximum ASCII value: "
min_msg: .asciiz "\nMinimum ASCII value: "
newline: .asciiz "\n"

.text
.globl main

main:
    # Prompt for input
    li $v0, 4       # system call for print string
    la $a0, prompt  # load address of prompt
    syscall

    # Read input
    li $v0, 8       # system call for read string
    la $a0, buffer  # load address of buffer
    li $a1, 255     # set maximum length of input
    syscall

    # Find maximum and minimum ASCII values
    la $t0, buffer   # load address of buffer into $t0

    li $t1, 0x20     # initialize $t1 to the ASCII value of space
    li $t2, 0x7F     # initialize $t2 to the largest possible ASCII value
    li $t3, 0x00     # initialize $t3 to the smallest possible ASCII value
    li $t4, 0         # initialize $t4 to 0

loop:
    lb $t5, ($t0)    # load byte from current position
    beq $t5, 0, done # if byte is null terminator, we're done

    # if byte is a space, skip it
    beq $t5, $t1, next

    # if this is the first non-space character, initialize $t4
    beq $t4, 0, init

    # find maximum ASCII value
    bgt $t5, $t3, is_max
    j is_min

is_max:
    move $t3, $t5    # set $t3 to current byte
    j next

    # find minimum ASCII value
is_min:
    blt $t5, $t2, set_min
    j next

set_min:
    move $t2, $t5    # set $t2 to current byte
    j next

init:
    # initialize $t4 to the first non-space character
    move $t4, $t5
    j next

next:
    addi $t0, $t0, 1 # move to next byte
    j loop           # jump to loop

done:
    # Print results
    li $v0, 4         # system call for print string
    la $a0, max_msg   # load address of max message
    syscall

    li $v0, 1         # system call for print integer
    move $a0, $t3     # load maximum ASCII value
    syscall

    li $v0, 4         # system call for print string
    la $a0, min_msg   # load address of min message
    syscall

    li $v0, 1         # system call for print integer
    move $a0, $t2     # load minimum ASCII value
    syscall

    li $v0, 4         # system call for print string
    la $a0, newline   # load address of newline
    syscall

    # Exit program
    li $v0, 10        # set system call number to 10 for exit
    syscall           # perform system call to exit
.data
buffer: .space 256   # allocate space for input buffer

Here the maximum ascii value of my name is coming right but the minimum value is coming wrong.

My Output:

Enter your first and last name: Isaac Newton Maximum ASCII value: 119 Minimum ASCII value: 10

My expected output:

Enter your first and last name: Isaac Newton Maximum ASCII value: 119 Minimum ASCII value: 73

How can I fix it?

1

There are 1 best solutions below

5
nulzo On

The MIPS system call 8 is similar to fgets() in C such that it includes a newline at the end of the input. You should be checking for the ascii value for a newline, which, as you may have guessed, is 10.

Now, how come it was stopping after the newline character? Well, the rest of the buffer the buffer is padded with a single null character. This is why it was always setting minValue to be 10, and terminating the program immediately after. All you need to do to fix your problem is implement a check to look for chars with an ascii value of 10 (in your case you could very likely just change beq $t5, 0, done to beq $t5, 10, done). I hope that helps to clear up any confusion you may be having.

Edit: As mentioned below by Peter, this implementation can lead to vulnerabilities in the program. For a wonderful explanation as to why, please see their comments down below.