Commit 1674a9b8 authored by kjc015's avatar kjc015
Browse files

Lab 07 Complete

parent fd3f11dd
all: counte primes2 matrix_sum
CC=gcc
CFLAGS=-Wall -Werror
primes2: primes2.o
$(CC) $(CFLAGS) primes2.c is_prime.s -o primes2
counte: counte.o
$(CC) $(CFLAGS) counte.c counte.s -o counte
matrix-sum: matrix-sum.o
$(CC) $(CFLAGS) matrix-sum-main.c matrix-sum.s -o matrix-sum
time: time.o
$(CC) $(CFLAGS) time.c time.s -o time
#include <stdio.h>
#include <stdlib.h>
int counte(char* string);
int main(){
char input[30];
printf("Enter a string: ");
fgets(input, 30, stdin);
printf("There are %d e's\n", counte(input));
return 0;
}
.globl counte
.data
character: .asciz "e"
.text
counte:
la s2, character
lb s2, 0(s2)
j find # call find
find:
lb t2, 0(a0) #load the first character of the string into t2
addi a0, a0, 1 #increment to next character in string
beq t2, s2, found #branch if the character is found
beq t2, zero, exit #branch if we reach the end of the string (null character)
j find
found:
addi s3, s3, 1 #add 1 to sum
j find
exit:
mv a0, s3 #move sum to a0 for return
jr ra #return to original function call
#include <stdio.h>
extern void matrix_sum(int A[2][3], int B[2][3], int C[2][3]);
int
print_m (int x[2][3])
{
int i,j;
for (j = 0; j < 2; j++){
for (i = 0; i < 3; i++){
printf("%d ", x[j][i]);
}
printf ("\n");
}
printf ("\n");
return 0;
}
int
main(void)
{
int A[2][3] = {{0, 1, 2}, {9, 8, 7}};
int B[2][3] = {{6, 5, 4}, {3, 4, 5}};
// int C[2][3] = {{0, 0, 0}, {0, 0, 0}};
int C[2][3] = {0};
print_m(A);
print_m(B);
matrix_sum(A,B,C);
print_m(C);
return 0;
}
.globl matrix_sum
.text
matrix_sum:
mv s2, a0 #load address of first array
mv s3, a1 #load address of second array
mv s4, a2 #load address of third array
li s5, 0
j loop
loop:
li t1, 6
add t2, zero, s5 #store i in t2
slli t2, t2, 2 #move i value
add t3, t2, s2 #access address of ith element of A
add t4, t2, s3 #access address of ith element of B
add t5, t2, s4 #access address of ith element of C
lw t3, 0(t3) #ith element of A
lw t4, 0(t4) #ith element of B
add t6, t3, t4 #add elem of A to elem of B and store in t6
sw t6, 0(t5) #store t6 in t5 (ith element of C)
beq s5, t1, end
addi s5, s5, 1
j loop
end:
mv a0, s4
jr ra
#include <stdio.h>
#include <time.h>
void add_second(struct tm *tm);
int main(void)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf ("it is now: %s\n", asctime(tm));
int i;
for (i=0; i<2345; i++){
add_second(tm);
}
printf ("it is now: %s\n", asctime(tm));
return 0;
}
.globl add_second
add_second:
li t5, 59
li t6, 23
seconds:
lb t1, 0(a0)
beq t1, t5, rollminute
addi t1, t1, 1
sw t1, 0(a0)
jr ra
rollminute:
sw zero, 0(a0)
lb t1, 4(a0)
beq t1, t5, rollhour
addi t1, t1, 1
sw t1, 4(a0)
jr ra
rollhour:
sw zero, 4(a0)
lb t1, 8(a0)
beq t1, t6, clear
addi t1, t1, 1
sw t1, 8(a0)
jr ra
clear:
sw zero, 8(a0)
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