## LAB 4
##### Name: Aseem Anand
##### Roll No. : CS22B008
# Question 1
## Code
.data
arr:
.word 1,2,3,4,5,6,7,8,9,10,11
.text
main:
la s0 arr
li s1 3
li s2 10
li s3 -1
loop1:
beq s2 s3 exit
lw s4 40(s0)
add s4 s4 s1
sw s4 40(s0)
addi s2 s2 -1
addi s0 s0 -4
exit:
li a7 10
# Question 2
## Code
.data
arr:
.word 1,2,34,3,4,5,7,23,12
str1:.string "true"
str2:.string "false"
space:.string " "
.text
main:
la s0 arr
li s1 0
li s2 8
loop:
lw s3 0(s0)
bge s1 s2 done
lw s4 4(s0)
lw s5 8(s0)
isODD:
blt s3 zero firstOdd
beq s3 zero Even
li s6 2
sub s3 s3 s6
j isODD
Even:
addi s0 s0 4
addi s1 s1 1
j loop
firstOdd:
blt s4 zero secondOdd
beq s4 zero Even
li s6 2
sub s4 s4 s6
j firstOdd
secondOdd:
blt s5 zero Ptrue
beq s5 zero Even
li s6 2
sub s5 s5 s6
j secondOdd
done:
la a0 str2
li a7 4
ecall
Exit:
li a7 10
ecall
Ptrue:
la a0 str1
li a7 4
ecall
Exit1:
li a7 10
ecall
# Question 3
## Code
.data
arr:
.word 2,5,1,3,4,7
arr1:
.word 0,0,0
arr2:
.word 0,0,0
space:.string " "
.text
main:
la s0 arr
la s1 arr1
la s2 arr2
li s3 0
li s6 3
li s4 6
loop:
beq s3 s6 loop1
lw s5 0(s0)
sw s5 0(s1)
addi s3 s3 1
addi s1 s1 4
addi s0 s0 4
j loop
loop1:
beq s3 s4 halt1
lw s5 0(s0)
sw s5 0(s2)
addi s3 s3 1
addi s0 s0 4
addi s2 s2 4
j loop1
halt1:
li s3 0
addi s0 s0 -24
addi s1 s1 -12
addi s2 s2 -12
loop2:
beq s3 s6 halt2
lw s5 0(s1)
sw s5 0(s0)
addi s1 s1 4
addi s0 s0 4
lw s5 0(s2)
sw s5 0(s0)
addi s3 s3 1
addi s2 s2 4
addi s0 s0 4
j loop2
halt2:
li s3 0
addi s0 s0 -24
done:
beq s3 s4 exit
lw a0 0(s0)
li a7 1
ecall
la a0 space
li a7 4
ecall
addi s0 s0 4
addi s3 s3 1
j done
exit:
li a7 10
ecall
# Question 4
## Code
.data
arr:
.word 2,7,11,15
space: .string " "
.text
main:
li s0 9 #target loaded
la s1 arr
li s2 0
li s3 0
li s4 4 #size of array
loop:
bne s2 s4 halt1
halt1:
addi s3 s2 1
lw s5 0(s1)
sub s7 s0 s5
loop1:
addi s8 s1 4
beq s3 s4 halt
if:
lw s6 0(s8)
beq s7 s6 print
addi s3 s3 1
addi s8 s8 4
j loop1
halt:
addi s2 s2 1
addi s1 s1 4
j loop
print:
add a0 a0 s2
li a7 1
ecall
la a0 space
li a7 4
ecall
li a0 0
add a0 a0 s3
li a7 1
ecall
exit:
li a7 10
ecall
# Question 5
## PseudoCode
.data
node:
.word 0 # data field
.word 0 # next pointer field
head:
.word 0 # head pointer
newline:
.string " "
str:
.string "->"
.text
main:
# Initialize head pointer to null
la x10, head
li x11, 0
sw x11, 0(x10)
# Insert nodes into the linked list
li x12, 5 # Number of nodes to insert
li x13, 1 # Initial data value
li x14, 0 # Counter
insert_loop:
bge x14, x12, end_insert_loop
# Insert node with data value x13
mv a0, x13 # Argument: data value
jal insert_node
# Increment data value for next node
addi x13, x13, 1
# Increment counter
addi x14, x14, 1
j insert_loop
end_insert_loop:
# Print the linked list
la a0, head # Load head pointer
jal print_list
# Exit
li a0, 10 # Exit code
ecall # System call to exit
#Function to insert a node with given data value
insert_node:
# Arguments:
# a0 = data value
# Allocate memory for the new node
li a7, 9 # syscall 9 - sbrk (allocate heap memory)
li a1, 8 # Allocate space for two words (data and next pointer)
ecall
# Store the data value in the new node
sw a0, 0(a7)
# Load the old head's address
la a1, head
lw a2, 0(a1)
# Store the old head's address in the next pointer field of the new node
sw a2, 4(a7)
# Update the head pointer to point to the new node
sw a7, 0(a1)
ret
#Function to print the linked list
print_list:
# Argument:
# a0 = address of head pointer
loop:
lw a1, 0(a0) # Load data field of current node
lw a2, 4(a0) # Load next pointer field of current node
#Print data value
mv a0, a1
li a7, 1 # syscall 1 - print integer
ecall
#Print arrow to indicate the next node
la a0, str
li a7, 4 # syscall 4 - print string
ecall
#Check if we have reached the end of the list
beqz a2, end_loop
#Move to the next node
mv a0, a2
j loop
end_loop:
la a0 newline # Print newline
li a7 4 # syscall 4 - print string
ecall
ret
# Question 6
### PseudoCode
void free(void* ptr) {
// Do nothing if the pointer is null
if (ptr == NULL) {
return;
}
// Step 1: Determine the size of the memory block pointed to by ptr
// This could be stored in a header before the memory block itself
// Step 2: Optionally, validate that ptr points to a valid memory block
// This could involve checking if ptr falls within the range of the allocated heap memory
// Step 3: Perform any necessary bookkeeping to track freed memory
// This could involve marking the memory block as free or updating data structures
// Step 4: Coalesce adjacent free memory blocks if needed
// This could involve merging the freed block with neighboring free blocks to avoid fragmentation
// Step 5: Optionally, release memory back to the system if certain conditions are met
// This could involve checking if releasing memory is beneficial and calling brk() or munmap()
// Step 6: Update heap-related pointers or data structures
// This could involve updating pointers to indicate the freed memory block is available
// Step 7: Optionally, perform any additional cleanup or optimization tasks
// This could involve defragmentation, memory compaction, or other memory management techniques
}
}