---
# System prepended metadata

title: Lab 4_CS22B036

---

# Lab 4
Name: M V Sonith
Roll No.: CS22B036

---
## Question 1

**Code** 
```assembly=
.data
array: .word 1 2 3 4 5 6 7 8 9 10 11
base: .word 0x10000000
.text
addi x20 x0 3
lw x24 base
addi x26 x24 -4
lw x21 base
addi x21 x21 40
addi x22 x0 0
loop:
    beq x21 x26 exit
    lw x25 0(x21)
    add x25 x25 x20
    sw x25 0(x21)
    addi x21 x21 -4
    j loop
exit:
    li a7 10
    ecall
    
```




<!-- **Observation:** (if required)
This is where you will write your observations
* Line 1
* Line 2
* Line3 -->

---
## Question 2

**Code** 
```assembly=
.data
array: .word 1 2 3 5 5 6
base: .word 0x10000000
str: .string "YES"
str1: .string "NO"
.text
lw x20 base
addi x29 x0 0 #counter for odd numbers
addi x19 x0 1
addi x28 x0 6 #loading array size
addi x30 x0 3 #for checking
add x21 x0 x20
addi x22 x0 0 # intializing i for loop
loop:
    beq x29 x30 exit #checks count =3
    beq x22 x28 exit # checks i< array size
    lw x25 0(x21)
    andi x25 x25 1 #and with 1 to find out even or odd 
    beq x25 x19 counter_increment
    addi x29 x0 0  #resetting counter
    addi x22 x22 1 #increment i
    addi x21 x21 4 # updating address
    j loop
    counter_increment:
        addi x29 x29 1 #incrmenting counter
        addi x22 x22 1
        addi x21 x21 4
        j loop
exit: #for printing answer
    beq x29 x30 true
    la a0 str1
    li a7 4
    
    ecall
    li a7 10
    ecall
true:
    la a0 str
    li a7 4
    ecall
    li a7 10
    ecall
    

```

**Observation/Output:** 

![Screenshot from 2024-02-20 18-39-50](https://hackmd.io/_uploads/BJvv_mz2T.png)




---
## Question 3


**Code** 
```assembly=
.data 
array: .word 1 3 5 7 9 2 4 6 8 10 
size: .word 10
base: .word 0x10000000
str: .string "The new array is \n"
str2: .string " "
answer_array: .word 0 0 0 0 0 0 0 0 0 0
.text
li x31 1
li x12 2
li x13 10
la x20 array
la x15 answer_array
la x21 answer_array
mul x21 x13 x12
addi x22 x20 0
add x23 x20 x21
la x25 answer_array
addi x26 x0 0
loop:
    beq x26 x13 exit
    andi x27 x26 1
    beq x27 x31 odd
    lw x28 0(x22)
    sw x28 0(x25)
    addi x22 x22 4
    addi x25 x25 4
    addi x26 x26 1
    j loop
    odd:
        lw x28 0(x23)
        sw x28 0(x25)
        addi x23 x23 4
        addi x25 x25 4
        addi x26 x26 1
        j loop
exit:
    addi x11 x0 0
    li a7 4
    la a0 str
    ecall
   
    print_loop:
        beq x11 x13 exir
        li a7 1
        lw a0 0(x15)
        ecall
        li a7 4
        la a0 str2
        ecall
        addi x15 x15 4
        addi x11 x11 1
        j print_loop
 exir:
     li a7 10
     ecall 
    
```

**Observation/Output:** 

![Screenshot from 2024-02-20 18-57-59](https://hackmd.io/_uploads/H1afKmfna.png)

---
## Question 4
**Link to LeetCode Question**
[Question Link](https://leetcode.com/problems/reverse-bits/description/)

**Code** 
```assembly=
.data
number: .word  0x18000000
base: .word 0x10000000
.text
la x21 number
lw x22 0(x21)
add x23 x0 x22
addi x24 x0 0
addi x30 x0 0
addi x31 x0 32
loop:
    beq x24 x31 exit
    andi x27 x23 1
    srli x23 x23 1
    slli x30 x30 1
    add x30 x30 x27
    addi x24 x24 1
    j loop
exit:
    li a7 1
    addi a0 x30 0
    ecall
    li a7 10
    ecall
    
    
    



```

**Observation/Output:** 
![Screenshot from 2024-02-20 20-28-08](https://hackmd.io/_uploads/B1RQA4z3a.png)

---
## Question 5
**Algorithm/Pseudocode**
The Structure can be like 
|   Address    |       |
|--------------|-------|
|   NodeAddress      | Datatype(.word or string)       |
|   NodeAddress+x    | Datatype(.word or string)       |
|   NodeAddress+y    | Datatype(.word or string)       |
| . | .|
| . | .|
| . | .|
| Last Address| a word which stores address of next node| 

**Explanation**
For a node we store it members contiguous  from Node address and last memeber will be a word which will have value of next address


---

## Question 6
**Algorithm/Pseudocode**

``` c=
//inside free
brk(ptr);
```
**Explanation**
As brk() function set program break to given address, giving the ptr address sets programm break to its address making it inaccessible as  we can't access data in  data segment after program break 

---
<!-- ## Question 1

**Code** (if required)
```assembly=
.data
demo: .string "This is where you can display your assembly code" 
```

<!-- ```c=
int main()
{
 printf("This is where you can display your C++ code\n");
}
``` -->
<!-- _You can use either one of these according to the question_ -->

<!-- [//]: <> (**1 a**) -->
<!-- Your answer

**1 b**
Your answer

**1 c**
Your answer -->


<!--**Observation:** (if required)
This is where you will write your observations
* Line 1
* Line 2
* Line3

--- -->
