In: Computer Science
Program 1-100 integer accumulation using RISC-V assembly.
Note: RARS only simulates 320-bit instructions of RISC-V, so make sure you use the instruction that operate work-size data type (mostly with `w` as the last character of the instruction mnemonic)
Greetings!!
Code:
#DATA SEGMENT
.data
sum: .ascii "Sum of numbers from 1-100: "
#CODE SEGMENT
.text
#MAIN STARTS HERE
main:
li t0,1 #load first number
li t1,0 #load 0 to hold sum
li t2,101 #load the upper limit
#PROCESSING..
loop:
beq t0,t2,done #check whether the number is equal to 101
add t1,t1,t0 #else sum it up in t1
addi t0,t0,1 #increment the number
j loop #repeat
done:
#DISPLAY MESSAGE
la a0,sum #load address of message
li a7,4 #parameter
ecall #display
#DISPLAY SUM
add a0,zero,t1 #load sum to a0 for display
li a7,1 #parameter
ecall #display
#TERMINATION
li a7,10 #parameter
ecall #terminate
Output screenshot:
Hope this helps