In: Computer Science
Translate following function from C into RISC-V: Display Prime Numbers Between Two Intervals
#include<stdio.h>
int main(){
int low, high, i, flag;
printf("Entertwonumbers(intervals):");
scanf("%d%d",&low,&high);
printf("Primenumbersbetween%dand%dare:",low,high);
//iteration until low is not equal to high
while(low<high)
{
flag=0;
//ignore numbers less than 2
if(low<=1)
{
++low;
continue;
}
//if low is a non-prime number, flag will be 1
for(i=2;i<=low/2;++i)
{
if(low%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d",low);
//to check prime for the next number
//increase low by 1
++low;
}
return0;
}
C to RISC-V :
.LC0:
.string "Entertwonumbers(intervals):"
.LC1:
.string "%d%d"
.LC2:
.string "Primenumbersbetween%dand%dare:"
.LC3:
.string "%d"
main:
addi sp,sp,-32
sd ra,24(sp)
sd s0,16(sp)
addi s0,sp,32
lui a5,%hi(.LC0)
addi a0,a5,%lo(.LC0)
call printf
addi a4,s0,-32
addi a5,s0,-28
mv a2,a4
mv a1,a5
lui a5,%hi(.LC1)
addi a0,a5,%lo(.LC1)
call scanf
lw a5,-28(s0)
lw a4,-32(s0)
mv a2,a4
mv a1,a5
lui a5,%hi(.LC2)
addi a0,a5,%lo(.LC2)
call printf
.L9:
lw a4,-28(s0)
lw a5,-32(s0)
bge a4,a5,.L2
sw zero,-24(s0)
lw a5,-28(s0)
mv a4,a5
li a5,1
bgt a4,a5,.L3
lw a5,-28(s0)
addiw a5,a5,1
sext.w a5,a5
sw a5,-28(s0)
j .L4
.L3:
li a5,2
sw a5,-20(s0)
.L7:
lw a5,-28(s0)
srliw a4,a5,31
addw a5,a4,a5
sraiw a5,a5,1
sext.w a4,a5
lw a5,-20(s0)
sext.w a5,a5
bgt a5,a4,.L5
lw a5,-28(s0)
mv a4,a5
lw a5,-20(s0)
remw a5,a4,a5
sext.w a5,a5
bnez a5,.L6
li a5,1
sw a5,-24(s0)
j .L5
.L6:
lw a5,-20(s0)
addiw a5,a5,1
sw a5,-20(s0)
j .L7
.L5:
lw a5,-24(s0)
sext.w a5,a5
bnez a5,.L8
lw a5,-28(s0)
mv a1,a5
lui a5,%hi(.LC3)
addi a0,a5,%lo(.LC3)
call printf
.L8:
lw a5,-28(s0)
addiw a5,a5,1
sext.w a5,a5
sw a5,-28(s0)
.L4:
j .L9
.L2:
li a5,0
mv a0,a5
ld ra,24(sp)
ld s0,16(sp)
addi sp,sp,32
jr ra