Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given below is a very simple C code and it s RISCV instruction sequence ( written manually ) . int x , y , m

Given below is a very simple C code and its RISCV instruction sequence (written
manually).
int x,y,m,c;
m =3;
x =-7;
c =10;
y = mx + c;
Assembly for rars
.data
m:
.word 3
x:
.word -7
c:
.word 10
y:
.word -1 # random initial value
.text
# Initialize m , x, c and y
# m in t1, x in t2, c in t3, y in t4
#addi t1, x0,3
lui t0,%hi(m)
addi t0, t0,%lo(m)
lw t1,0(t0)
#addi t2, x0,-7
lui t0,%hi(x)
addi t0, t0,%lo(x)
lw t2,0(t0)
#addi t3, x0,10
lui t0,%hi(c)
addi t0, t0,%lo(c)
lw t3,0(t0)
add t4, x0, x0
# Multiply without mul instruction
mul_m_x:
beq t1, x0, done
add t4, t4, t2
addi t1, t1,-1
jal x0, mul_m_x
done :
add t4, t4, t3
lui t0,%hi(y)
addi t0, t0,%lo(y)
sw t4,0(t0)
Question:
a.
The code given for Q1 assumes t1= m is always >=0.
We do not handle the case when t1= m is less than 0
Please correct the given assembly code to handle positive and negative values for all m, x and c.
The test it with -
m =-3, x =2, c =5
m =-3, x =-2, c =-5
m =3, x =2, c =-5
m =-3, x =0, c =5
m =0, x =4, c =-5
For each case, please feel free to use registers as you need (within the ABI rules) but store the
final result in t4
b.
Please write the above code using a function. Pass m, x and c to the function and the function
must calculate the results and return, then the main assembly code puts the result in t4(if not
already there) and exits.
Please test it with -
m =-3, x =2, c =5
m =-3, x =-2, c =-5
m =3, x =2, c =-5
m =-3, x =0, c =5
m =0, x =4, c =-5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions