---
# System prepended metadata

title: Lab 4

---

# Lab 4
Name: Havish Jadav
Roll No.: cs22b026

---
## Question 1
**code**
```assembly=
 .data 
.word 1 3 2 4 5 9 7 6 8 10 11
base: .word 0x10000000
.text
lw x16 base
addi x1 x0 3
addi x5 x0 11
loop:
beq x5 x0 exit
lw x10 40(x16)
add x10 x10 x1     
sw x10 40(x16)
addi x16 x16 -4
addi x5 x5 -1
j loop

exit:
    addi x0 x0 0
lw x16 base
    addi x2 x0 0
    addi x7 x0 11
print_loop:
    beq x2 x7 print_exit
    lw x5 0(x16)
    addi a0 x5 0
    li a7 1
    ecall
    addi a0 x0 32
    li a7 11
    ecall
    addi x16 x16 4
    addi x2 x2 1
    j print_loop
print_exit:
    li a7, 10
    ecall
```
---
## Question 2
**Code** 
```assembly=
#here there are 10 elements present in the array and there exists three consecutive odds no.s in the array
 .data 
.word 1 3 2 4 5 9 7 11 8 10
base: .word 0x10000000
.text
lw x16 base
addi x1 x0 1
addi x5 x0 10
addi x8 x0 3
loop:
beq x5 x0 exit3
lw x10 36(x16)
andi x13 x10 1
sw x10 36(x16)
addi x16 x16 -4
addi x5 x5 -1
beq x13 x1 oddcounter
beq x13 x0 evencounter      
j loop
oddcounter:
    beq x15 x8 exit2
    addi x15 x15 1
    j loop
evencounter:
    addi x15 x0 0
    j loop
exit2:
.data
str4:.string "Yes"
.text
la a0 str4
li a7 4
ecall
j exit
exit3:
.data
str3:.string "No"
.text
la a0 str3
li a7 4
ecall
exit:
    addi x0 x0 0
```
_ _ _
## Question 3
**Code** 
```assembly=
.data
.word 1 2 3 4 5 6 7 8 9 10
base1: .word 0x10000000
.word 0 0 0 0 0 0 0 0 0 0 
base2: .word 0x1000000
str1: .string "TRUE"

.text
addi x1 x0 5       #No of array element
addi x2 x0 0        #x2 is i
lw x16 base1
lw x6 base2
loop:
    beq x2 x1 exit
    lw x4 0(x16)
    lw x5 20(x16)
    sw x4 0(x6)
    sw x5 4(x6)
    addi x16 x16 4
    addi x6 x6 8
    addi x2 x2 1
    j loop
exit:
lw x6 base2
addi x7 x0 0
add x9 x1 x1
print_loop:
    bge x7 x9 end_print
    lw x11 0(x6)
    addi a0, x11, 0               
    li a7, 1
    ecall                     
    addi a0, x0, 32           
    li a7, 11               
    ecall
    addi x6 x6 4
    addi x7 x7 1
    j print_loop
end_print:
li a7, 10               
    ecall
```
---
## Question 4
**Code**
```assembly=
#here is the implementation of two sum

#Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.



#here the target is stored in x29 and the no. of element are stored in x4

.data
.word 4 6 8 2 9 10
base: .word 0x10000000
.text
addi x3 x0 0
addi x18, x0, 0
addi x21, x0, 1
addi x12, x0, 0
addi x8 x0 4
addi x4, x0, 6
addi x29 x0 10
add x7, x0, x0
addi x31, x0, -1
lw x16, base
lw x10, 0(x16)

loop:
bge x18, x4, exit3 #pointer i < size
addi x12 x0 0 # pointer j > i + 1
lw x10, 0(x16)
loop1:
bge x12, x4, exit1
lw x11, 4(x16)
add x24 x10 x10
beq x24 x29 pass1
addi x12, x12, 1
add x24 x11 x10
beq x24 x29 pass
addi x16, x16, 4
j loop1

exit1:
j minus
return:
addi x16 x16 4
addi x18, x18, 1
j loop
exit2:

minus:
    mul x31 x4 x8
    sub x16 x16 x31  
    j return 
pass1:
.data
str4:.string "["
.text
la a0 str4
li a7 4
ecall
add a0, x0, x18
li a7, 1
ecall
.data
str5:.string ","
.text
la a0 str5
li a7 4
ecall
addi x16, x16, 4
addi x25, x25, 1
addi a0 x0 32
li a7 11
add a0, x0, x18
li a7, 1
ecall
.data
str6:.string "]"
.text
la a0 str6
li a7 4
ecall
j exit3

pass:
.data
str40:.string "["
.text
la a0 str40
li a7 4
ecall
add a0, x0, x18
li a7, 1
ecall
.data
str50:.string ","
.text
la a0 str50
li a7 4
ecall
addi x16, x16, 4
addi x25, x25, 1
addi a0 x0 32
li a7 11
add a0, x0, x12
li a7, 1
ecall
.data
str60:.string "]"
.text
la a0 str60
li a7 4
ecall
j exit3

exit3:   
addi x0, x0, 0

```
___

## **Question 5**
**Code**
```assembly=
.data
node_size:  .word 8 

.text
Node:
    .word 0
    .word 0   

create_linked_list:
    addi a1 x0 5
    li t1, 0 
create_loop:
    la t2, Node  
    add t2, t2, t1 
    sw zero, 0(t2) 
    sw zero, 4(t2)     
    addi t1, t1, 8  
    blt t1, x7, create_loop
    ret
append_to_linked_list:
    la t1, Node  
    lw t2, 4(a0) 
    beqz t2, append_node  
    add a0, t2, zero  
    j traverse_loop 
append_node:
    la t2, Node  
    li t3, 8  
    mv a2, a0  
traverse_loop:
    lw t2, 4(a0)
    beqz t2, append_node  
    add a0, t2, zero 
    j traverse_loop  
    
```    
___
    
    
    
    A node is a structure containing two components: data and a pointer to the next node. The data is where you store the information you want in the list, and the next pointer points to the next node in the sequence.

You need a pointer to the first node in your list, often called the head. Initially, set the head to zero or null, indicating an empty list.

When you want to add a new node to the end of the list:

- Allocate memory for a new node (using a memory allocation function).
- Set the data field of the new node.
- Find the last node in the linked list and update its next pointer to point to the new node.

To move through the list and access or modify data:

- Start from the head and follow the next pointers until you reach the end (where the next pointer is null).
- Process the data in each node as needed.


## **Question 6**
**Code**
```cpp=
free(void* ptr){
    if ptr == nullptr{
        return;
    }
    brk(ptr);
    return;
}
//if the ptr is deferencing then the function returns.
//if not , then the pointer memory is cleared from whichever address it is pointer to.Thus, clearing every memory it is accessed from the base ptr.
```
___
```
___


    