# Lab 4
Name: M Akash
Roll No.: CS22B037
---
## Question 1
**Code**
```assembly=
.data
.word 2 9 8 4 1 0 1 5 2 4 1
base: .word 0x10000000
str1: .string "Initial Array is \n"
str2: .string "Modified Array is \n"
str3: .string " "
str4: .string "\n"
.text
lw x1,base
li t0,3 # int s=3
li t1,11 # int i=11
li t2,0 # i=0
li t3,4
li t4,1
la a0 str1
li a7 4
ecall
li s1, 0x10000000
Print1:
beq t2,t1,main
lw t5,0(s1)
add a0,t5,x0
li a7 1
ecall
la a0 str3
li a7 4
ecall
addi t2,t2,1
addi s1,s1,4
j Print1 # prints Array
main: li t2,0 # i=0
Func: beq t1,t2,End
lw s0,40(x1)
add s0,s0,t0
sw s0,40(x1)
sub x1,x1,t3
sub t1,t1,t4
j Func
End:
la a0 str4
li a7 4
ecall
la a0 str2
li a7 4
ecall
li s1, 0x10000000
li t1,11
li t2,0
Print2:
beq t2,t1,Exit
lw t5,0(s1)
add a0,t5,x0
li a7 1
ecall
la a0 str3
li a7 4
ecall
addi t2,t2,1
addi s1,s1,4
j Print2 # prints Array
Exit:
li a7 10
```
**Output:**
Initial Array is
2 9 8 4 1 0 1 5 2 4 1
Modified Array is
5 12 11 7 4 3 4 8 5 7 4
---
## Question 2
**Code**
```assembly=
.data
.word 6 8 7 1 0 1 3 5 9
base: .word 0X10000000
true: .string "True"
false: .string "False"
.text
addi x12 x0 9 #array size
addi x4 x0 3
addi x11 x0 0 # x11=number of ones
lw x16 base
loop:
lw x10 0(x16)
addi x16 x16 4
addi x12 x12 -1
andi x10 x10 1
beq x12 x0 exit
bne x10 x0 inc
addi x11 x0 0
j loop
inc:
addi x11 x11 1
beq x11 x4 success
j loop
success:
addi a7 x0 4
la a0 true
ecall
addi a7 x0 10
ecall
exit:
addi a7 x0 4
la a0 false
ecall
addi a7 x0 10
ecall
```
**Output:** True
---
## Question 3
**Code**
```assembly=
.data
.word 0 1 2 3 4 5 6 7 8 9
base: .word 0x10000000
str1: .string "Initial Array is \n"
str2: .string "Modified Array is \n"
str3: .string " "
str4: .string "\n"
.text
lw x1,base
li t1,10 # int i=10
li t2,0 # i=0
la a0 str1
li a7 4
ecall
li s1, 0x10000000
Print1:
beq t2,t1,main
lw t3,0(s1)
add a0,t3,x0
li a7 1
ecall
la a0 str3
li a7 4
ecall
addi t2,t2,1
addi s1,s1,4
j Print1 # prints Array
main: li t2,0 # i=0
li t1,5
Func: beq t1,t2,End
lw a0,0(x1)
sw a0,0(s1)
lw a0,20(x1)
sw a0,4(s1)
addi x1,x1,4
addi s1,s1,8
addi t2,t2,1
j Func
End:
la a0 str4
li a7 4
ecall
la a0 str2
li a7 4
ecall
li s1, 0x10000028
li t1,10
li t2,0
Print2:
beq t2,t1,Exit
lw t5,0(s1)
add a0,t5,x0
li a7 1
ecall
la a0 str3
li a7 4
ecall
addi t2,t2,1
addi s1,s1,4
j Print2 # prints Array
Exit:
li a7 10
```
**Output:**
0 1 2 3 4 5 6 7 8 9
0 5 1 6 2 7 3 8 4 9
---
## Question 4
**1. 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.
**Code**
```assembly=
.data
.word 0 1 2 3 4 5
base: .word 0x10000000
true: .string "True"
false: .string "False"
comma: .string ","
.text
lw x1,base
li t0,0 #i=0
li t1,6 #array size
li s0,6 #target sum=6
li t3,0 #j=0
Loop1:
beq t0,t1,exit # logic is i=0 to i<6
addi t3,t0,1
addi t0,t0,1
lw t2,0(x1)
addi x1,x1,4
add a1,x1,x0
sub t2,s0,t2
Loop2:
beq t3,t1,Loop1 # search for target-arr[i] in array
lw t4,0(a1)
addi a1,a1,4
beq t2,t4,success
addi t3,t3,1
j Loop2
success:
sub t2,s0,t4
add a0,t2,x0
li a7 1
ecall
addi a7 x0 4
la a0 comma
ecall
add a0,t4,x0
li a7 1
ecall
addi a7 x0 10
ecall
exit:
addi a7 x0 4
la a0 false
ecall
addi a7 x0 10
ecall
```
**Output:** 1,5
---
## Question 5
**Psuedo Code**
```assembly=
# Define the structure of a node
node:
.word 0 # head=0 initially
.word 0 # pointer to next node
# define head
.global head
# initialize the linked list
init:
# head=NULL
li a0, 0
sw a0, head
# inserting in a linked list
insert:
la a1, node # memory
li t0, 0
lw t1, head
loop: # traversing the loop
lw t2, 0(t1)
beq t2, t0, end # end of list
lw t1, 0(t1) # mmove to next node
j loop
end:
sw a1, 0(t1) # inserting new node
```
---
## Question 6
**Psuedo Code:**
```C=
if(ptr==null) then return;
//goto the ptr block using Traversing in Linked List and
// delete that block using Deletion in Linked List
if(block not found) then return;
else free(block)
//we can use '-' operator to sbrk to deallocate the memory
```