I have written a MIPS Assembly program that takes in a PPM colour image file and increases the brightness of the image by increasing the values of each RGB value in each pixel by 10. And if the value of any of the RGB values exceeds 255 then it just becomes 255.
When I try run it on QtSpim it says there is an error in the loop when I try to accumulate the red values in the $t2 temporary register, in order to calculate the average of all the red value in the pixels later on.
This is the code I have written for this program :
#A program that will increase the RGB value of a PPM colour image by 10.
.data
original_image: .asciiz "Select a PPM colour image:\n"
bright_image: .asciiz "The brightness increased image is:\n"
width: .word 0
height: .word 0
max_value: .word 0
.text
.globl main
main: #Main is the entry point in the program
#Open and read input and output files
li $v0, 13
la $a0, original_image
li $a1, 0 #Read-only mode
syscall
#Read PPM header
li $v0, 14
move $a0, $v0 #File descriptor
la $a1, width
la $a2, height
la $a3, max_value
syscall
#Calculate the number of pixels
lw $t0, width
lw $t1, height
mul $t0, $t0, $t1
#Allocate memory for the pixel data
li $v0, 9
move $a0, $t0 #The number of bytes
syscall
move $s0, $v0 #Store address of allocated memory
#Read the pixel data from memory
li $v0, 14
move $a0, $v0 #File descriptor
move $a1, $s0 #Buffer the address
move $a2, $t0 #Number of bytes
syscall
#Process the pixel data
li $t2, 0 #Counter for the average calculation
#Loop through the pixels and increase the data by 10
loop:
lw $t3, 0($s0) #Load red value
addi $t3, $t3, 10 #Add 10 to red value
sltiu $t4, $t3, 256 #Check if value is greater than 255
addi $t3, $t3, -10 #Subtract 10 from red value
and $t4, $t4, 1 #Convert to 0 or 1
sll $t4, $t4, 8 #Shift left to make 0 or 1 to 0 or 256
add $t3, $t3, $t4 #Add 0 or 256 to red value
addi $t2, $t2, $t3 #Accumulate red value for average calculation
move $t5, $t3 #Store new red value
lw $t3, 4($s0) #Load green value
addi $t3, $t3, 10 #Add 10 to green value
sltiu $t4, $t3, 256 #Check if value is greater than 255
addi $t3, $t3, -10 #Subtract 10 from green value
and $t4, $t4, 1 #Convert to 0 or 1
sll $t4, $t4, 8 #Shift left to make 0 or 1 to 0 or 256
add $t3, $t3, $t4 #Add 0 or 256 to green value
addi $t2, $t2, $t3 #Accumulate green value for average calculation
move $t6, $t3 #Store new green value
lw $t3, 8($s0) #Load blue value
addi $t3, $t3, 10 #Add 10 to blue value
sltiu $t4, $t3, 256 #Check if value is greater than 255
addi $t3, $t3, -10 #Subtract 10 from blue value
and $t4, $t4, 1 #Convert to 0 or 1
sll $t4, $t4, 8 #Shift left to make 0 or 1 to 0 or 256
add $t3, $t3, $t4 #Add 0 or 256 to blue value
addi $t2, $t2, $t3 #Accumulate blue value for average calculation
move $t7, $t3 #Store new blue value
#Store new pixel data
sw $t5, 0($s0) #Store new red value
sw $t6, 4($s0) #Store new green value
sw $t7, 8($s0) #Store new blue value
addi $s0, $s0, 12 #Move to next pixel
addi $t0, $t0, -1 #Decrement counter
bnez $t0, loop #Branch if counter is not zero
#Open new file for writing
li $v0, 15
la $a0, bright_image
li $a1, 1 #Write-only mode
li $a2, 0x1a4 #File permissions
syscall
#Write PPM header
li $v0, 16
move $a0, $v0 #File descriptor
lw $a1, width
lw $a2, height
lw $a3, max_value
syscall
#Write pixel data
li $v0, 15
move $a0, $v0 #File descriptor
move $a1, $s0 #Buffer address
move $a2, $t0 #Number of bytes
syscall
#Calculate average of original RGB values
li $v0, 9
move $a0, $t2 #Sum of RGB values
move $a1, $t0 #Number of pixels
syscall
mtc1 $v0, $f0 #Move result to FPU register
#Calculate average of new RGB values
li $v0, 9
move $a0, $t2 #Sum of RGB values
li $a1, 1 #Number of pixels
syscall
mtc1 $v0, $f2 #Move result to FPU register
#Display average of original RGB values
li $v0, 2
movt $a0, $f0
syscall
#Display average of new RGB values
li $v0, 2
movt $a0, $f2
syscall
#Exit program
li $v0, 10
syscall
Please can someone advise me where I went wrong.
I was expecting the program to load on QtSpim but it gave me and error message of :
" spim: (parser) syntax error on line 66 of file C:/Users/increase_brightness.asm addi $t2, $t2, $t3 #Accumulate red value for average calculation "