I’ve been tasked with implementing a few image processing functions in RISC-V assembly, using .GRAY images, and wrote a prototype in C to have some sort of guideline, which reads like so
#include <stdio.h>
#define width 400
#define height 599
//read image's pixels
void readGray(unsigned char image[width][height]) {
FILE *source = fopen("cat_noisy.gray", "rb");
fread(image, sizeof(unsigned char), width * height, source); /*read through the file*/
fclose(source);
}
void writeGray(unsigned char image[width][height]) {
FILE *file = fopen("cat_mean.gray", "wb");
fwrite(image, sizeof(unsigned char), width * height, file);
fclose(file);
}
When trying to accomplish the same in RISC-V assembly, I’ve got this read function
.data
input: .asciz "cat_noisy.gray"
output_mean: .asciz "cat_mean.gray"
output_median: .asciz "cat_median.gray"
error: .asciz "erro a ler ficheiro"
status_read: .asciz "image read"
status_write: .asciz "image written"
image: .space 239600 # (599 * 400)
.text
.global main
readGray:
#args
addi sp, sp, -8 # make space in stack
sb s0, 0(sp) # store image name byte in stack
sb s1, 4(sp) # store image size byte in stack
#opening file
mv s0, a1 # move to where array will be saved
li a7, 1024 # syscall to open file
li a1, 0 # syscall to read file
ecall #open file
mv s1, a0 # save descriptive in s1
beqz a0, readGray_error # if failed, show error
#if success, read
li a7, 63 # syscall to read file
mv a0, s1 # save descriptive in a0
mv a1, s0 # save image array in a1
li a2, 239600 # read up to 239600 bytes - image size
ecall # read file
readGray_done: #close file and reset args
li a7, 57
mv a0, s1
ecall
li a0, 0
li a1, 0
li a2, 0
addi sp, sp, 8
lb a0, 0(sp)
lb a1, 4(sp)
#print success
la a0, status_read
li a7, 4
ecall
ret
readGray_error: #print error
la a0, error
li a7, 4
ecall
j readGray_done
And this write function
writeGray:
#args
addi sp, sp, -8
sw s0, 0(sp)
sw s1, 4(sp)
mv s0, a1
li a7, 1024
li a1, 1
ecall
mv s1, a0
beqz a0, writeGray_error
li a7, 64
mv a0, s1
mv a1, s0
li a2, 239600
ecall
writeGray_done:
li a7, 57
mv a0, s0
ecall
li a0, 0
li a1, 0
li a2, 0
addi sp, sp, 8
lb a0, 0(sp)
lb a1, 4(sp)
la a0, status_write
li a7, 4
ecall
ret
writeGray_error:
la a0, error
li a7, 4
ecall
j writeGray_done
And here’s my main function
# readGray
la a0, input
la a1, image
jal readGray
# writeGray
la a0, input
la a1, image
jal writeGray
#exit
li a7, 10
ecall
``
The issue I've run across is the image either fails to generate, or just generates a black .PNG image (after converting with ImageMagick), even when using my input image - defined in the .data segment - for both read and write (that should make a copy).
Thank you for all the help in advance, apologies for the long post.
2