CA Session 6 Summary - muneeb-mbytes/computerArchitectureCourse GitHub Wiki

MOVE Instruction

  • MOVE instruction is used to transfer data from one register to register
  • Move instructions are supported by MIPS, RISC-V
  • It is a pseudo instruction
  • An instruction requires two operands
  • The first operand is generally the destination, which contains data in a register or memory location and the second operand is the source
  • Source contains either the data to be delivered (immediate addressing) or theaddress (in register or memory) of the data. Generally, the source data remains unaltered after the operation

The MOV instruction may have one of the following five forms :

  • MOV register, register
  • MOV register, immediate
  • MOV memory, immediate
  • MOV register, memory
  • MOV memory, register

MIPS Move Instruction:

SYNTAX:

image

EXAMPLE:

image

RISCV Move Instruction:

SYNTAX:

image

EXAMPLE:

image

BRANCH Instruction

A branch instruction in a computer program makes the CPU switch to a different set of instructions, instead of just following the usual order. This is crucial for creating loops and conditionals, letting the program decide what to do based on certain conditions. In computer architecture, a branch instruction changes the flow of the program by altering where the CPU should go next. This is important for making loops, if-else conditions, and function calls work properly.

PSEUDO Instructions:

Pseudo instructions are simplified commands in assembly language that aren't real machine instructions. They make programming easier, and the assembler converts them into actual machine instructions.

Branch if less than (blt):

image

The blt (Branch if Less Than) pseudo instruction in assembly language makes the program jump to a different part if one number is less than another. It's not a real machine instruction, so the assembler changes it into real instructions that the computer can run.

Example:
In MIPS Assembly: (Pseudo Instruction)
blt $t0, $t1, label (branch to label if $t0 < $t1)
This could be translated into: (Real Instruction)
slt $at, $t0, $t1 (Set on Less Than: Set $at to 1 if $t0 < $t1).
bne $at, $zero, label (Branch if Not Equal: Branch to label if $at is not zero).

Explanation:
slt $at, $t0, $t1: This instruction sets the temporary register $at (which stands for "assembler temporary") to 1 if the value in register $t0 is less than the value in register $t1. Otherwise, it sets $at to 0. (Checks if $t0 is less than $t1 and sets $at to 1 if it is). bne $at, $zero, label: If $at is not zero (meaning $t0 is less than $t1), it jumps to label.

SOME OTHER COMMON PSEUDO INSTRUCTIONS:

  • bgt (Branch if Greater Than): bgt $t0, $t1, Label
    • Jumps to label if $t0 is greater than $t1.
  • ble (Branch if Less Than or Equal): ble $t0, $t1, Label
    • Jumps to label if $t0 is less than or equal to $t1.
  • bge (Branch if Greater Than or Equal): bge $t0, $t1, Label
    • Jumps to label if $t0 is greater than or equal to $t1.
  • beq (Branch if Equal): beq $t0, $t1, Label
    • Branches if two registers are equal.

Sum of Array code in C, MIPS and RISCV

image

Pictorial respresentation of C code:

image

This code calculates the sum of the elements in an array named Array_a. It starts by setting a sum variable to 0 and a count variable to 0. Then, it goes through each element in the array, adding them to the sum until it has processed all the elements in the array (up to n elements). The loop continues because of an if statement and a goto statement, which sends the program back to the label Label_L as long as the condition (count < n) is true.

Pictorial representation of MIPS Instructions:

image

The code initializes a sum variable $s0 to 0 and a counter variable $tO to 0. It then enters a loop labeled 'L'. Inside the loop, it multiplies the counter by 4, adds the result to the base address of array A ($s1), loads the value at that address into $t2, and adds this value to the sum. After that, the counter is incremented by 1. The loop continues as long as the counter remains less than n ($s2).

Pictorial representation of RISCV Instructions:

image

The code initializes the sum ($s0) and iterator ($t0) to 0, and sets the count ($t1) to 0. Then, it enters a loop where it adds the result to the base address of array A ($s1), loads the value at that address into $t2, and adds it to the sum ($s0). The counter is incremented, and the loop continues until the counter is no longer less than n ($s2).

Pointer VS Index

In computer architecture, especially within Reduced Instruction Set Computer (RISC) architectures like MIPS (Microprocessor without Interlocked Pipeline Stages), the concepts of pointers and indices are crucial for understanding how memory addressing works. Both pointers and indices are used to access elements in memory, but they have different roles and uses.

Pointers

Definition: A pointer is a variable that stores the address of another variable or memory location.
Use Case: Pointers are used for dynamic memory allocation, accessing arrays, and for passing large structures or arrays to functions without copying the entire data.
Memory Addressing: In MIPS, pointers are typically stored in registers and can be used directly to access memory locations. For example, using load (lw) and store (sw) instructions.

Example for MIPS:
#Assume $t0 holds the address of the array
lw $t1, 0($t0) #Load the word at address in $t0 into $t1

Indices:

Definition: An index is an integer value used to calculate the offset within an array.
Use Case: Indices are commonly used to access elements in an array. The index specifies the position relative to the start of the array.
Memory Addressing: Indices need to be multiplied by the size of the elements (e.g., 4 bytes for a 32-bit integer) to calculate the correct memory address. In MIPS, this often involves arithmetic operations to compute the address.

Example:
#Assume $t0 holds the base address of the array and $t1 holds the index
sll $t2, $t1, 2 # Multiply index by 4 (shift left logical)
add $t3, $t0, $t2 # Add base address to the offset
lw $t4, 0($t3) # Load the word at the calculated address into $t4.

Comparison and Usage in MIPS

Direct Access (Pointers):
Advantages: Fast access to memory locations, especially useful for linked data structures (e.g., linked lists, trees).
Example: Using pointers to traverse a linked list.

Indexed Access (Indices):
Advantages: Simple and intuitive way to access elements in an array, allows easy iteration.
Example: Accessing elements in an array with a loop

JUMP Instructions:

There are 2 types of jump instructions in RISC V

  • Jump and link
  • Jump and link register

Jump and link (jal):

Jump-and-link instructions allow the program to jump to a function located anywhere in the address space. This instruction jumps to a specified address and stores the return address in the register $ra.
$ra is the return address register

Syntax for calling the function:
jal function_label

Jump and link register (jalr):

The jump and link register instruction comes with I-format. The immediate instruction will be of 12 bits. The jalr instruction is an MIPS instruction which is used to jump to an address stored in a register and also save the return address in another register

Syntax:
jalr $t1, $t2 // Jump to the address contained in register $t2 and store the return address in register $t1

⚠️ **GitHub.com Fallback** ⚠️