# Lab 4
Name: AKSHATHA.R.H
Roll No.: CS22B003
---
## Question 1
```assembly=
.data
.word 8 3 1 7 3 3 9 0 9 6 1
base: .word 0x10000000
str1: .string " "
str2: .string "\n"
.text
lw x1,base
li x2,11 # int i=11
li x4,0 # i=0
li x2,3 # int s=3
li x5,4
li x10, 0x10000000
Loop1:
beq x4,x3,main
lw x6,0(s1)
add a0,x6,x0
li a7 1
ecall
la a0 str1
li a7 4
ecall
addi x4,x4,1
addi x10,x10,4
j Loop1
main:
li x4,0 # i=0
Loop2: beq x3,x4,End
lw x11,40(x1)
add x11,x11,x2
sw x11,40(x7)
sub x7,x7,x5
addi x3,x3,-1
j Loop2
End:
la a0 str2
li a7 4
ecall
li x10, 0x10000000
li x3,11
li x4,0
Loop3:
beq x4,x3,Exit
lw x6,0(x10)
add a0,x6,x0
li a7 1
ecall
la a0 str1
li a7 4
ecall
addi x4,x4,1
addi x10,x10,4
j Loop3
Exit:
li a7 10
```
---
## Question 2
```assembly=
.data
arr: .word 7,6,5,4,3,2,1
str: .string "True"
str1: .string "False"
.text
la x1 arr
addi x2 x0 6 #x2 is n
addi x3 x0 1 #x3 0
addi x20 x0 1 #and with
addi x21 x0 0 #counter
addi x11 x0 3
loop:
beq x3 x2 exit
addi x2 x2 -1
lw x4 0(x1)
addi x1 x1 4
andi x5 x4 1
beq x5 x20 count
addi x21 x0 0
count:
addi x21 x21 1
beq x21 x11 exit2
j loop
exit:
la a0 str1
li a7 4
exit2:
la a0 str
li a7 4
ecall
```
---
## Question 3
```assembly=
.data
arr: .word 1,2,3,4,5,6,7,8
base: .word 0x10000000
arr1: .word 0,0,0,0
arr2: .word 0,0,0,0
str2: .string " "
.text
lw x1,base
la x2,arr2
la x12,arr2
la x18,arr1
la x19,arr1
addi x3,x0,4
addi x4,x0,0
addi x21,x0,0
addi x22,x0,0
addi x5,x0,4
Loop:
beq x3,x4,exit1
lw x6,0(x1)
sw x6,0(x18)
addi x18,x18,4
addi x1,x1,4
addi x4,x4,1
j Loop
exit1:
beq x21,x5,exit
lw x7,0(x1)
sw x7,0(x12)
addi x12,x12,4
addi x21,x21,1
addi x1,x1,4
j exit1
exit:
beq x22,x5,exit2
lw x27,0(x19)
addi a0,x27,0
li a7,1
ecall
la a0,str2
li a7,4
ecall
addi x19,x19,4
lw x28,0(x2)
addi a0,x28,0
li a7,1
ecall
la a0,str2
li a7,4
ecall
addi x2,x2,4
addi x22,x22,1
j exit
exit2:
addi a7,x0,10
ecall
```
---
## Question 4
```assembly=
**TWO SUM QUESTION **
.data
.word 2,4,6,8,1,3,5,9
base: .word 0x10000000
comma: .string ","
.text
lw x2,base
li x5,0 # i=0
li x3,9
li x10,13 # target=13
li x4,0
i:
beq x5,x3,exit
addi x4,x5,1
addi x5,x5,1
lw x6,0(x2)
addi x2,x2,4
add x11,x2,x0
sub x6,x10,x6
j:
beq x4,x3,i
lw x12,0(x11)
addi x11,x11,4
beq x6,x12,success
addi x4,x4,1
j j
success:
sub x6,x10,x12
add x10,x6,x0
li a7 1
ecall
addi a7 x0 4
la a0 comma
ecall
add x10,x12,x0
li a7 1
ecall
addi a7 x0 10
ecall
exit:
addi a7 x0 10
ecall
```
---
## QUESTION 5
Several steps are involved in the RISC-V assembly language linked list implementation algorithm:
Define a node's structure, which has a data field and a pointer to the node after it.
Setting the start pointer to null or a sentinel node is a common way to initialize a linked list.
The process of inserting a new node at the beginning of the list involves allocating memory for it, setting its data field, updating its next pointer to point to the list's current start, and then updating the start pointer to point to the new node.
Execute a traversal loop that begins at the beginning of the list, processes each node (printing or manipulating data, for example), and then advances to the next node until the list's end is reached.
---
## QUESTION 6
a) There should be a pointer to the user-allocated memory block that the free() function may access.
b)First, we should essentially retain the memory blocks' state (e.g., whether they are allocated or free). This needs to be completed during the allocation and deallocation phases. During allocation, we need to mark certain blocks as occupied (maybe using a flag or something similar). and we must release it throughout the deallocation process.
c)Thus, we can set this block's status to free once we obtain its pointer.
d)In addition, after using frre on the memories, we must keep a record of each one. so that we can utilize that for allocation after first verifying in this list that the necessary conditions are satisfied.
e)Thus, we can add that memory block to the list of all previously frozen memories after using free on a specific memory.