Commit cd792fdf authored by kjc015's avatar kjc015
Browse files

Lab 07, Exercise 4 Complete

parent cfec60cd
CC=gcc
CFLAGS=-Wall -Werror
primes2: primes2.o
$(CC) $(CFLAGS) primes2.c is_prime.s -o primes2
# RISCV assembly is_prime
.text
.globl is_prime
is_prime:
li s3, 2
addi s2, a0, -1
j loop
loop:
bge s3, s2, exit1
li t2, 0
rem t1, a0, s3
beq t1, t2, exit2
addi s3, s3, 1
j loop
exit1:
li a0, 1
j end
exit2:
li a0, 0
j end
end:
jr ra
/*
real:11.430s
user:11.136s
sys:0.280s
This was faster than my C compiler. It is faster because C is such a high level
language compared to assembly. Technically, since machine code is so low level
all you have to do is convert it to binary to compile it. With C, theres more
compiling that has to go on.
*/
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n);
int main (int argc, char* argv[])
{
int i, n = 0, maxnum=80;
if (argc > 1){
maxnum = atoi(argv[1]);
}
for (i = 2; n < maxnum; i++){
if (is_prime(i)){
printf("%7d", i);
n++;
if ((n % 10) == 0){
printf("\n");
}
}
}
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment