---
# System prepended metadata

title: Lab x

---

# Lab 4
Name:K.V.Sai Maneesh
Roll No.:cs22b030

---
## Question 1
```assembly=
.data
array:.word 3,5,8,7,1,11,36,28,43,24,15
base:.word 0x10000000
str1:.string " "
.text
addi x2 x0 10
lw x5 base
addi x5 x5 40
addi x4 x0 3
Loop:
    blt x2 x0 exit
    add x8 x0 x0
    lw x14 0(x5)
    add x8 x8 x14
    add x8 x8 x4
    addi x5 x5 -4
    addi x2 x2 -1
    j print
print:
    add a0 x8 x0
    li a7 1
    ecall
    la a0 str1
    li a7 4
    ecall
    j Loop
exit:
    la a0 str1
    li a7 4
    ecall

```
## Question 2
```assembly=
li x3 0
lw x5 base
li x8 3
li x24 0
li x15 1
Loop:
     beq x12 x2 exit
     lw x6 0(x5)
     addi x2 x2 1
     addi x5 x5 4
     add x7 x0 x6
     andi x7 x7 1
     bne x7 x0 inc
     beq x7 x0 reset
inc:
     addi x3 x3 1
     beq x3 x8 update
     j Loop
reset:
     add x3 x0 x0
     j Loop
exit:
     bne x3 x15 False
     li a7 10
     ecall
update:
     li x24 1
     la a0 true
     li a7 4
     ecall
     li a7 10
     ecall
False:
     la a0 false
     li a7 4
     ecall

```
## Question 3
```assembly=
.data
array: .word 4,2,7,3,5,9,8,1
base: .word 0x10000000
base1: .word 0x30000000
str: .string " "

.text
li x8,8 
lw x15,base
lw x16,base1
lw x18,base1
li x4,4
li x2,0
li x3,0
addi x13,x15,16
loop:
    beq x2,x4,print
    addi x2,x2,1
    lw x10,0(x15)
    lw x11,0(x13)
    sw x10,0(x16)
    addi x16,x16,4
    sw x11,0(x16)
    addi x16,x16,4
    addi x15,x15,4
    addi x13,x13,4
    j loop
print:
    beq x3,x8,exit
    lw x10,0(x18)
    li a7,1
    mv a0,x10
    ecall
    li a7,4
    la a0,str
    ecall
    addi x18,x18,4
    addi x3,x3,1
    j print
exit:
   li a7 10
   ecall

```
## Question 4
```assembly=
.data
array:.word 3,4,2,6,3,7,5,3,2
base:.word 0x10000000
str:.string " "
.text
addi x2 x0 9
lw x5 base
addi x3 x0 3  # remove all occurences of 3 in the given array
Loop:
    beq x4 x2 exit
    lw x6 0(x5)
    addi x5 x5 4
    addi x4 x4 1
    bne x6 x3 print
    j Loop
print:
    li a7 1
    addi a0 x6 0
    ecall
    li a7 4
    la a0 str
    ecall
    j Loop
exit:
    li a7 10
    ecall

```
## Question 5
```assembly=
# a class is initialised with a node and pointer to its next node and data in it
#if the head node is empty initialize the head node with new node and append it to the initialized Linked list
#while performing traversal if we encounter any NULL node,exit from the traversal

# Define the text section
.text
main:
    # Initialize the linked list
    # Load the address of the first element into a register
    la x10, elem1
    # Link the elements together if needed (skipped in this pseudo-code for brevity)

    # Add a new element to the linked list
    # Load the address of the new element into a register
    la x11, new_elem
    # Traverse the linked list to find the last element
    la x12, elem1
traverse_loop:
    # Load the next pointer of the current element
    lw x13, 4(x12)
    # Check if the next pointer is null (end of the list)
    beq x13, x0, found_last_element
    # Move to the next element
    mv x12, x13
    # Repeat the loop
    j traverse_loop
found_last_element:
    # Store the address of the new element in the next pointer of the last element
    sw x11, 4(x12)

    # End of the program
    # (In a real program, you might perform other operations or print the list, etc.)
    # Exit syscall
    li a7, 10        # Exit syscall
    ecall


```
## Question 6
```assembly=
free:
    addi sp, sp, -4         # Adjust stack pointer
    sw ra, 0(sp)            # Save return address
    
    # Load argument
    mv a0, a0               # Assuming pointer to be freed is passed in a0

    
    lw ra, 0(sp)            # Restore return address
    addi sp, sp, 4          # Restore stack pointer
    ret                     # Return from function

```