My beq condition isnt working - why cant it see the whitespace?

28 Views Asked by At

Basically just that. I am trying to parse a buffer that i read from a file and break it up into different parts to obtain values for my variables.

The relevant code is as follows:

#corrected
read_width: 
    li $v0, 14              # Read syscall code
    move $a0, $s0           # File descriptor
    move $a1, $t1           # Buffer address
    li $a2, 1               # Maximum length
    syscall

    lb $t2, ($t1)                       # Load the read character
    move $t7, $t2
    addi $t1, $t1, 1
    beq $t2, whitespace, print          # Check if char is whitespace 
    beq $t2, 10, print          # Check if char is whitespace
    j read_width

When i run this in qtspim it never exits the loop, and instead continues to read and store the entire file instead of stopping, like this:

[10010000] 230a3350 74654a20 2034360a 320a3436 P 3 . # J e t . 6 4 6 4 . 2 [10010010] 310a3535 310a3636 310a3638 310a3138 5 5 . 1 6 6 . 1 8 6 . 1 8 1 . 1 [10010020] 310a3537 310a3738 310a3039 310a3937 7 5 . 1 8 7 . 1 9 0 . 1 7 9 . 1 [10010030] 310a3239 310a3239 310a3837 310a3539 9 2 . 1 9 2 . 1 7 8 . 1 9 5 . 1 [10010040] 310a3339 310a3737 310a3639 310a3339 9 3 . 1 7 7 . 1 9 6 . 1 9 3 . 1 [10010050] 310a3837 310a3839 310a3439 310a3937 7 8 . 1 9 8 . 1 9 4 . 1 7 9 . 1 [10010060] 310a3939 310a3539 320a3038 310a3030 9 9 . 1 9 5 . 1 8 0 . 2 0 0 . 1 [10010070] 310a3639 320a3238 310a3130 310a3639 9 6 . 1 8 2 . 2 0 1 . 1 9 6 . 1 [10010080] 320a3438 310a3230 310a3839 320a3338 8 4 . 2 0 2 . 1 9 8 . 1 8 3 . 2 [10010090] 310a3230 310a3839 310a3937 310a3939 0 2 . 1 9 8 . 1 7 9 . 1 9 9 . 1 [100100a0] 310a3739 320a3138 310a3130 310a3839 9 7 . 1 8 1 . 2 0 1 . 1 9 8 . 1

and so on... when it should be stopping after that first 64 in the first line.

Ive attached the rest of my running code in case that is also helpful, any help would be greatly appreciated!


.data
    header_buffer:  .space 256     # Buffer to store the header
    width:          .word 0
    height:         .word 0 
    max_val:        .word 0
    filename: .asciiz "input.ppm" # Change this to your input file name

.text
    .globl main
    
    
main:
        # Open the file
        li $v0, 13              # Open syscall code
        la $a0, filename        # Load the filename into $a0
        li $a1, 0               # Read-only mode
        syscall
        move $s0, $v0           # Save the file descriptor in $s0

        # Read the first 4 lines of the header
        la $t1, header_buffer   # Load the header buffer address
    

# Read the first line of the file - should be P2/P3
read_head:
        li $v0, 14              # Read syscall code
        move $a0, $s0           # File descriptor
        move $a1, $t1           # Buffer address
        li $a2, 1               # Maximum length
        syscall
    
    #Check if this is the end of the first line
    lb $t4, 0($t1)              # Load the read character
    addi $t1, $t1, 1
        beq $t4, 10, read_comment   # End of line (newline character)
    j read_head
    
# Read the second line of the file, check if its a comment 
read_comment:
    li $v0, 14              # Read syscall code
        move $a0, $s0           # File descriptor
        move $a1, $t1           # Buffer address
        li $a2, 1               # Maximum length
        syscall
    
    #Check if this a comment line
    lb $t4, 0($t1)                      # Load the read character
    addi $t1, $t1, 1
        bne $t4, 35, read_width     # Branch to next line if the first char isnt a #
    
read_comment_loop:
    li $v0, 14              # Read syscall code
        move $a0, $s0           # File descriptor
        move $a1, $t1           # Buffer address
        li $a2, 1               # Maximum length
        syscall
    
    #Check if this a comment line
    lb $t4, 0($t1)                      # Load the read character
    addi $t1, $t1, 1
        beq $t4, 10, read_width     # End of line (newline character)
    j read_comment_loop
    
read_width: 
    li $v0, 14              # Read syscall code
    move $a0, $s0           # File descriptor
    move $a1, $t1           # Buffer address
    li $a2, 1               # Maximum length
    syscall

    lb $t2, ($t1)                       # Load the read character
    move $t7, $t2
    addi $t1, $t1, 1
    beq $t2, whitespace, print          # Check if char is whitespace 
    beq $t2, 10, print          # Check if char is whitespace
    j read_width
    
exit:
    # Close the file
    li $v0, 16           # Close syscall code
    move $a0, $s0        # File descriptor
    syscall

    # Exit the program
    li $v0, 10           # Exit syscall code
    syscall

1

There are 1 best solutions below

0
Erik Eidt On

You're using the wrong register source:

    lb $t2, ($t1)                       # Load the read character
    move $t2, $t7
    addi $t1, $t1, 1
    beq $t1, whitespace, print          # Check if char is whitespace 
    beq $t1, 10, print          # Check if char is whitespace
    j read_width

Your beq is testing $t1, which is a pointer.  You wanted $t2 there, which is a byte loaded from the file buffer.


Also, your move $t2, $t7 is backwards, so wiping out $t2 instead of capturing it.