How to write a Simple RISC Assembly Program?

RISC stands for “Reduced Instruction Set Computer”. It is one of the paradigms used to design an ISA (Instruction Set Architecture). It implements very simple instructions having a simple and regular structure. The number of instructions is also less about 64-128.

Examples: IBM, ARM, etc.

Assembly Language is a low-level programming language that is specific to a particular ISA and uses very simple statements that correspond to one machine instruction.

It is a highly efficient programming language for writing codes for operating systems and device drivers.

Program stands for a “Computer Program” which is a set of statements written by the programmers following the correct syntax to perform a certain task on computers.

Also Read Types of Looping statements in C/C++

Simple RISC Assembly Instructions

Any simple RISC assembly statement has two main parts:

  1. Instruction also called “Opcode” stands for Operation Code. It has some special meaning to the machine.
  2. Operands these can be a resister, some memory locations, or a constant also called an “immediate”.

In simple RISC there are a total of 21 assembly instructions, but here we will see the very basics ones.

  • mov instruction is used to transfer the contents of a resister or an immediate to another resister. It is the most commonly used instruction while writing simple RISC assembly programs.

Example: mov r0, 5 or mov r0, r1

There are in total 6 arithmetic instructions in simple RISC

  • add instruction is used to add the contents of a resistor and an immediate or the contents of two resistors and assigns the result to a new resister.

Example: add r1, r0, 9 or add r2, r1, r0 (r1 = r0 + 9 or r2 = r1 + r0)

  • sub instruction is used to subtract the contents of a resister or an immediate from another resister and assigns the result to a new resister.

Example: sub r1, r0, 7 or sub r2, r1, r0 (r1 = r0 – 7 or r2 = r1 – r0)

  • mul instruction is used to multiply the contents of a resister and an immediate or the contents of two resistors and assigns the product to a new resister.

Example: mul r1, r0, 5 or mul r2, r1, r0 (r1 = r0 x 5 or r2 = r1 x r0)

  • div instruction is used to divide the contents of a resister by the contents of an immediate or a resister and assigns it to a new resister.

Example: div r1, r0, 6 or div r2, r1, r0 (r1 = r0 / 6 or r2 = r1 / r0)

  • mod instruction is used to divide the contents of a resister by the contents of an immediate or a resister and assigns the remainder to a new resister.

Example: mod r1, r0, 3 or mod r2, r1, r0

  • cmp instruction is a compare instruction used to compare the contents of two resisters. It makes use of a special resister called flags.

Example: cmp r0, r1 (r0 > r1 or r0 < r1 or r1 == r0)

The value of flags.E = 0 & flags.GT = 1 when the contents of the first resister are greater than the second one, flags.E = 1 & flags.GT = 0 when the contents of the first and the second resisters are equal, and flags.E = 0 & flags.GT = 0 when the contents of the first is less than the second one.

NOTE: flags resisters stores the value of the last comparison.

Conditional Branch Statements

  • beq .label stands for (branch if equal) branch to .label i.e. transfer the control of execution to the indicated label when flags.E = 1
  • bgt .label stands for (branch if greater than) branch to .label i.e. transfer the control of execution to the indicated label when flags.GT = 1

NOTE: The values of flags resisters are only set by cmp instruction so to use the conditional branch statements we must use cmp instruction before these.

Let’s write a simple RISC assembly program to calculate the factorial of a number

@ This is a factorial program

mov r0, 1     /* r0 will store the final factorial result */
mov r2, r1    /* r1 is the number whose factorial has to be calculated */

.loop:           /* starting of the loop */

   mul r0, r0, r2  /* r0 = r0 x r2 */
   sub r2, r2, 1   /* r2 = r2 - 1 */
   cmp r2, 1
   bgt .loop

The final value of r0 will give the required result i.e. factorial of a number (r1)

Output:

r1 = 5

r0 = 120

Related posts

Five Ways to Calculate Power in C++

File Permissions in Linux

Implementing Stacks and Queues