我的 beq 条件不起作用 - 为什么它看不到空白?

问题描述 投票:0回答:1

基本上就是这样。我正在尝试解析从文件中读取的缓冲区并将其分解为不同的部分以获得变量的值。

相关代码如下:

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 $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

当我在 qtspim 中运行它时,它永远不会退出循环,而是继续读取和存储整个文件而不是停止,如下所示: [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 等等... 当它应该在第一行的第一个 64 之后停止时。

我已附上其余的运行代码,以防也有帮助,任何帮助将不胜感激!


.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 $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
    
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

loops parsing mips qtspim
1个回答
0
投票

您使用了错误的寄存器源:

    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

您的

beq
正在测试
$t1
,这是一个指针。您想要
$t2
,这是从文件缓冲区加载的一个字节。


此外,您的

move $t2, $t7
是向后的,因此擦除
$t2
而不是捕获它。

© www.soinside.com 2019 - 2024. All rights reserved.