imageassemblyriscv32rars-simulator

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

Solution

  • After some research, I fixed the code, and here are the mistakes I've fixed: