user21893624
user21893624

Reputation: 11

I can't find the error in my MIPS assembly code

I wrote a MIPS assembly program that reads a PPM image file and increases each RGB value by 10 and then writes the output to a new file called question1_output.ppm but my program hasn't been writing the output to the new file which i created in the same folder and is just empty. The program also calculates the averages for the RGB values of the original and new image file and prints those on the console.

My code is :

.data
    original_image:         .asciiz "C:/Users/erinl/OneDrive - University of Cape Town/2. Second Year/Semester 2/CSC2002S/Assignments/Arch_Assigmnet1/house_64_in_ascii_crlf.ppm"
    bright_image:           .asciiz "C:/Users/erinl/OneDrive - University of Cape Town/2. Second Year/Semester 2/CSC2002S/Assignments/Arch_Assigmnet1/question1_output.ppm"
    buffer:                 .space 12
    total_pixels:           .word 0
    max_value:              .word 255
    newline:                .asciiz "\n"
    buffer1:                .space 4096


.text
.globl main


#Function to open file for reading
open_file_read:
    li $v0, 13
    syscall
    move $t0, $v0       #Save file descriptor
    jr $ra


#Function to open a file for writing
open_file_write:
    li $v0, 13
    li $a1, 1
    syscall
    move $t0, $v0       #Save the file descriptor
    jr $ra


#Function to read integer from file
integer_read:
    li $v0, 14
    syscall
    jr $ra

#Function to write integer to file
integer_write:
    li $v0, 11
    syscall
    jr $ra


#Function to read header information of original PPM file
read_ppm_header:
    #Open original PPM file for reading
    la $a0, original_image
    jal open_file_read

    #Read & ignore first four lines (header)
    li $t1, 4


read_header_loop:
    beqz $t1, read_header_complete
    la $a1, buffer
    jal integer_write
    addi $t1, $t1, -1
    j read_header_loop


read_header_complete:
    jr $ra


#Function to write header information to new PPM file
write_ppm_header:
    #Open the new PPM file for writing
    la $a0, bright_image
    jal open_file_write

    #Write first four lines (header)
    li $t1, 4


write_header_loop:
    beqz $t1, header_write_done
    la $a1, buffer
    jal write_integer
    addi $t1, $t1, -1
    j write_header_loop


header_write_done:
    jr $ra


#Function to process PPM image
process_ppm_image:
    la $a0, original_image
    jal open_file_read


    #Open new PPM file for writing
    la $a0, bright_image
    jal open_file_write


    #Read & write header information
    jal read_ppm_header
    jal write_ppm_header


    #Initialize variables
    li $t2, 0          #Sum of R values
    li $t3, 0          #Sum of G values
    li $t4, 0          #Sum of B values
    li $t5, 0          #Pixel count



#Main loop to process pixels

process_pixels:

    #Read RGB values from original file
    la $a1, buffer
    jal integer_read
    lw $t6, 0($a1)
    addi $a1, $a1, 4
    jal integer_read
    lw $t7, 0($a1)
    addi $a1, $a1, 4
    jal integer_read
    lw $t8, 0($a1)


    #Increase each RGB value by 10
    addi $t6, $t6, 10    #R value
    addi $t7, $t7, 10    #G value
    addi $t8, $t8, 10    #B value


    #Stop values at 255 if exceed
    bgt $t6, $t9, stop_red
    bgt $t7, $t9, stop_green
    bgt $t8, $t9, stop_blue
    j update_pixel


stop_red:
    li $t6, 255         #Stop R value to 255
    j update_pixel


stop_green:
    li $t7, 255         #Stop G value to 255
    j update_pixel


stop_blue:
    li $t8, 255         #Stop B value to 255


update_pixel:
    #Write updated RGB values to new file
    la $a1, buffer
    sw $t6, 0($a1)
    addi $a1, $a1, 4
    sw $t7, 0($a1)
    addi $a1, $a1, 4
    sw $t8, 0($a1)


    #Update sums & pixel count
    add $t2, $t2, $t6
    add $t3, $t3, $t7
    add $t4, $t4, $t8
    addi $t5, $t5, 1


    #Check if processed all pixels
    beq $t5, $t0, processing_complete


    #Otherwise cont. processing next pixel
    j process_pixels


processing_complete:

    #Calculate average RGB values for  original image

    div $t2, $t5        #Calculate R average
    mflo $t6
    div $t3, $t5        #Calculate G average
    mflo $t7
    div $t4, $t5        #Calculate B average
    mflo $t8


    #Print averages as double values
    mtc1 $t6, $f0
    mtc1 $t7, $f1
    mtc1 $t8, $f2

    cvt.d.w $f0, $f0
    cvt.d.w $f1, $f1
    cvt.d.w $f2, $f2


    #Print average R value
    li $v0, 4
    la $a0, newline
    syscall
    mov.s $f12, $f0
    syscall


    #Print average G value
    li $v0, 4
    la $a0, newline
    syscall
    mov.s $f12, $f1
    syscall


    #Print average B value
    li $v0, 4
    la $a0, newline
    syscall
    mov.s $f12, $f2
    syscall


    #Close files
    li $v0, 16          #Close file syscall
    move $a0, $t0       #File descriptor to close
    syscall

    li $v0, 16          #Close file syscall
    move $a0, $t1       #File descriptor to close
    syscall

    jr $ra



main:

    li $v0, 1
    la $a0, buffer1
    li $a1, 12
    syscall


    #Initialize total_pixels with total number pixels
    li $t0, 0  # Replace with the actual number of pixels
    la $t1, buffer1
    li $t2, 4096

    loop:
        lbu $t3, 0($t1)
        addi $t0, $t0, 1        #Increment t0 by 1
        addi $t1, $t1, 1        #Increment the address by 1
        addi $t2, $t2, -1       #Decrement the remaining bytes by 1
        bnez $t2, loop          #If there are remaining bytes, continue the loop

    done:
    #Initialize max_value with maximum value for stopping
    li $t9, 255


    #Call function to process PPM image
    jal process_ppm_image


    #Exit program
    li $v0, 10
    syscall

and the PPM image files are :

P3             #header
# Hse
64 64          #image dimensions
255            #max-value
177
192
179
184
196
174
171
192
174
189
205
192
176
196
180
183
190
168
172
196
185
...

I tried running my program on QtSpim but it just prints "268501290" and then stops working and it doesn't print anything to the output file.

Upvotes: 0

Views: 92

Answers (0)

Related Questions