# Lab 4
Name: T.Venkat Kaushal
Roll No.: CS22B058
---
## Question 1
**Code**
```assembly=
.data
.word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
.text
#address
add x1 x0 x0
lui x1 0x10000
addi x1 x1 0x000
#s: x2
addi x2 x0 3
#i: x3
addi x3 x0 10
for_loop_1:
blt x3 x0 end_loop_1
lw x4 0(x1)
addi x1 x1 4
add x2 x2 x4
addi x3 x3 -1
j for_loop_1
end_loop_1:
li a7 10
ecall
```
**Observation:**
This is where you will write your observations
* The answer is stored in the register x2
---
## Question 2
**Code**
```assembly=
.data
.word 1, 3, 4, 2, 4
str1: .string "true"
str2: .string "false"
.text
#initialized address
addi x1 x0 0
lui x1 0x10000
addi x1 x1 0x000
#size: x2
addi x2 x0 5
#count: x3
addi x3 x0 0
#i : x4
addi x4 x4 0
for_loop_1:
bge x4 x2 end_loop_1 # condition for loop
lw x5 0(x1) #load word
if_1:
andi x6 x5 1 # x6 contains 0 or 1
addi x7 x0 1 # x7 = 1
bne x6 x7 end_if_1 # condition x6 == 1
addi x3 x3 1 # count++
if_2:
addi x8 x0 3
bne x3 x8 end_if_2
addi x9 x0 1
addi x4 x4 1
addi x1 x1 4
j end_loop_1
end_if_1:
addi x3 x0 0 # count = 0
j if_2
end_if_2:
addi x9 x0 0
addi x4 x4 1
addi x1 x1 4
j for_loop_1
end_loop_1:
add x0 x0 x0
beq x9 x7 print_true
la a0 str2
li a7 4
ecall
li a7 10
ecall
print_true:
la a0 str1
li a7 4
ecall
li a7 10
ecall
```
**Observation:**
* The answer is displayed in the console.
---
## Question 3
**Code**
```assembly=
.data
.word 1 2 3 4 5 6
.text
#initialized the address(1 - n)
add x1 x0 x0
lui x1 0x10000
addi x1, x1, 0x000
#size:x2 = n
addi x2 x0 3
#initialized the address(n - 2n)
add x3 x0 x0
lui x3 0x10000
#x4 = 2n
slli x4 x2 2
add x3 x3 x4
#initialized the address
add x31 x0 x0
lui x31 0x10000
slli x10 x2 1
slli x5 x10 2
add x31 x31 x5
#i:x6
addi x6 x0 0
#j:x7
add x7 x0 x2
for_loop_1:
bge x6 x2 end_loop_1
bge x7 x4 end_loop_1
lw x8 0(x1)
sw x8 0(x31)
addi x1 x1 4
addi x31 x31 4
lw x9 0(x3)
sw x9 0(x31)
addi x3 x3 4
addi x31 x31 4
addi x7 x7 1
addi x6 x6 1
j for_loop_1
end_loop_1:
li a7 10
ecall
```
**Observation:**
* See the memory in Ripes, the content is present in data section after the address next to the end of last element address.
---
## Question 4
**Question:*
**Two Sum**: Given an vector, and a target, return indexes of two elements in the vector whose sums equals target. Assume the answer exist always.
**Code**
```assembly=
.data
.word 3 2 4
.text
#address
add x1 x0 x0
lui x1 0x10000
addi x1 x1 0x000
#target: x2
add x2 x0 x0
addi x2 x2 6
#i: x3
addi x3 x0 0
#size: x4
addi x4 x0 3
for_loop_1:
bge x3 x4 end_loop_1
#j: x5
addi x5 x3 1
for_loop_2:
bge x5 x4 end_loop_2
#condition
slli x6 x3 2
slli x7 x5 2
add x8 x6 x1
add x9 x7 x1
lw x10 0(x8)
lw x11 0(x9)
add x12 x10 x11
bne x12 x2 end_if_1
li a7 10
ecall
end_if_1:
addi x5 x5 1
j for_loop_2
end_loop_2:
addi x3 x3 1
j for_loop_1
end_loop_1:
li a7 10
ecall
```
**Observation:**
* The indexes are(i, j) , located in the registers x3,x5 respectively, those should be returned.
---
## Question 5
**PseudoCode**
```assembly=
.data
node_struct:
.word 0 // Value to be stored in the data
.word 0 // Pointer to the next node
init_linkedlist:
addi x1 x0 0
la x1, node_struct // Allocating memory for head node
sw zero 0(x1) // Store value in the head node
sw zero 4(x1) // Store next pointer in the head node
mv x2 x1 // Save head pointer, and let other pointer points it.
ret
insert_at_end:
mv x3 x2 // Load LinkedList
la x1, node_struct // Allocating new node
addi x5 x0 5 // memory to be inserted
mv x4 x5 // Load memory to be inserted
sw x4 , 0(x1) // Store value in the node
sw zero, 4(x1) // Store next pointer in the new node(NULL)
traverse_loop:
lw x6, 4(x3) // Load next pointer
beqz x6, insert_done // If next pointer is NULL , exit loop
mv x3, x6 // move to next node
j traverse_loop
insert_done:
mv x6, x1 // Update last node's next to point to the new node
sw x3, 4(x2)
ret
print_loop:
lw x1, 0(t1) // Load the value of the current node
jal ra, print_function // we can write a print function as we traverse
lw x2, 4(x2) // load next pointer
bnez x2, print_loop // if next node, if not NULL, repeat loop
ret
```
**Observation:**
1. We can simulate the working of the linked list to an extent using RISC-V assembly language and Ripes.
---
## Question 6
**Observation:**
1. When malloc() allocates the block, it allocates extra bytes to hold an integer containing the size of the block.
2. This integer is located at the beginning of the block; the address actually returned to the caller points to the location just past this length value.
3. When a block is placed on the (doubly linked) free list, free() uses the bytes of the block itself in order to add the block to the list.
Reference: "The Linux Programming Interface" Textbook.
---