Reputation: 21
I'm trying to read the RGB values from the file into an array, but when I check the buffer it's full of zeros instead of the values. First I tried it in C and then, implemented it in riscv assembly. I'm not sure what's causing this.
Here are the both implementations,
// reads a file with an image in RGB format into an array in memory
void read_rgb_image(char fileName[], unsigned char *arr)
{
FILE *image;
image = fopen(fileName, "rb");
if (!image)
{
printf("unable to open file\n");
exit(1);
}
fread(arr, 3, WIDTH * HEIGHT, image);
fclose(image);
}
read_rgb_image:
addi sp, sp, -4
sw s0, 0(sp)
la a0, filename
li a1, 0 # read-only flag
li a7, 1024 # open file
ecall
mv s0,
la a1, buff # get array add.
li a2, 3
li a7, 63 # read file into buffer
ecall
mv a0, s0
li a7, 57 # close file
ecall
lw s0, 0(sp)
addi sp, sp, 4
ret
Upvotes: 1
Views: 527
Reputation: 21
After some research, I fixed the code, and here are the mistakes I've fixed:
I wanted to read all values into the buffer. The file contains 172800 values since the resolution of the picture is 320x180. And there are three values per pixel (RGB components). I've assumed that the max. length flag for reading system calls should be 3 because of the RGB values. After changing it to 172800 it read the whole file into the buffer.
li a2, 172800
I tried to load bytes with lw
instruction instead of lbu
. Each value has a size of a byte and should be stored as unsigned.
Finally, I checked if the file was opened correctly by checking if the file descriptor equals zero.
Upvotes: 1