I am having an issue with byte alignment and im not sure what to change or how to fix it (MIPS)

32 Views Asked by At

I've tried several variations and nothing is working correctly - i keep getting an error from qtspim

I am trying to parse the header from a ppm file and store the relevant quantities as integers so that i can later on use them in calculations.

I read from the file and my entire header is included in a buffer, and i now need to break this up and actually retrieve values for my width, height, and max value.

The problem is as follows: When i run through the buffer bit by bit, i try to store the relevant bits that would form, in this code snippet, the width value. I keep getting an error on this line:

sb $t2, 0($t7)

Ive attached the rest of the code below.

ive tried different variations on $t7, including having another temporary buffer to load into $t7, different space allocations, etc. i believe it to be an issue regarding bit alignment, but im at a loss as to where to start on fixing that.

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" 

.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 the width and height and max value of the file
init_values:
    li $t7, 0       # Use to store width
    li $t8, 0       # Use to store height
    li $t9, 0       # Use to store max val
    
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
    sb $t2, 0($t7)
    addi $t1, $t1, 1
    beq $t2, 32, store_width            # Check if char is whitespace 
    j read_width
    
store_width:
    move $t0, $t7
    jal str_to_int
    sw $t6, width
    
0

There are 0 best solutions below