0% found this document useful (0 votes)
6 views

RISC-V Lab-8-1

The document discusses how to implement loops in RISC-V Assembly language since it lacks built-in loop instructions. It explains the use of labels and branch instructions for controlling loop execution and provides example programs for printing numbers and performing tasks using loops. Additionally, it lists lab tasks that involve printing even and odd numbers and displaying strings using loops.

Uploaded by

Javeria Fazal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

RISC-V Lab-8-1

The document discusses how to implement loops in RISC-V Assembly language since it lacks built-in loop instructions. It explains the use of labels and branch instructions for controlling loop execution and provides example programs for printing numbers and performing tasks using loops. Additionally, it lists lab tasks that involve printing even and odd numbers and displaying strings using loops.

Uploaded by

Javeria Fazal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMPUTER ORGANIZATION AND ASSEMBLY LAB-08

Loops in RISC-V Assembly

In high-level languages, loops (like for, while, do-while) automate repeated


execution of code blocks.

However, RISC-V (a RISC architecture) doesn't have built-in loop instructions.


Instead, loops are constructed using:

Labels to mark positions in the code.

Branch instructions (bne, blt, bge, beq, etc.) to control when to jump back to the
loop.

This gives manual control over:

 Initialization
 Condition check
 Update/step
 Loop body execution
Program to Print Numbers from 1 to 5

li a0, 10 # print newline

Loads the ASCII value of newline (10) into a0 so it can print a line break.

li a7, 11 # syscall code for print_char

Loads the system call number 11 into a7, which corresponds to printing a character.

addi t0, t0, 1 # i = i + 1

Increments the loop counter t0 by 1.


ble t0, t1, loop # if i <= 5, repeat loop

Checks if t0 is less than or equal to t1 (i.e., i <= 5). If true, it jumps back to the loop label
to repeat.

Program to Print Number’s from 10 to 0 in decrement Order


Lab Tasks

1: Print all even numbers from 2 to 20 using a loop.

2: Print all Odd numbers from 1 to 30 using a loop.

3: Display the string "RISC-V Loop\n" three times using a loop.

4: Write a loop that sums numbers from 1 to 10, then print the final sum.
5: Write a Program which Prints even number’s from 30-2
6: write a program which print’s odd number’s from 30-1

You might also like