英文:
Reading an RGB formatted file into a buffer in RISCV (32-bit) assembly
问题
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, a0
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
英文:
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
答案1
得分: 0
经过一些研究,我修复了代码,以下是我修复的错误:
-
我想要将所有值读入缓冲区。文件包含172800个值,因为图片的分辨率是320x180。每个像素有三个值(RGB分量)。我假设读取系统调用的最大长度标志应为3,因为涉及RGB值。将其更改为172800后,它将整个文件读入缓冲区。
li a2, 172800
-
我尝试使用
lw
指令而不是lbu
来加载字节。每个值的大小为一个字节,并应以无符号形式存储。 -
最后,我通过检查文件描述符是否等于零来验证文件是否正确打开。
英文:
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 oflbu
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论