将RGB格式的文件读入RISCV(32位)汇编中的缓冲区。

huangapple go评论46阅读模式
英文:

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 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.

huangapple
  • 本文由 发表于 2023年5月20日 23:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定