# LAB 4 Name : Guru Rohith.G Roll No: CS22B022 ## Question 1: **Code** ```assembly= .data array: .word 1,2,3,5,6,7,9,11,13,14,4 s: .string " " .text la x1 array la x5 array addi x2 x0 3 addi x4 x0 11 addi x1 x1 40 addi x9 x0 0 Loop: lw x14 0(x1) add x14 x14 x2 sw x14 0(x1) addi x1 x1 -4 #addi x5 x5 -4 addi x9 x9 1 #addi x8 x0 -1 blt x9 x4 Loop print: lw x8 0(x5) add a0 x8 x0 li a7 1 ecall la a0 s li a7 4 ecall addi x5 x5 4 addi x9 x9 -1 bne x9 x0 print ``` ![Screenshot 2024-02-22 150227](https://hackmd.io/_uploads/rJVaE9Ena.png) ---------------------------------------- ## Question 2: **Code** ```assembly= .data array: .word 1, 3, 4, 6, 8, 9, 13, 5 true: .string "True\n" false: .string "False\n" .text la x1, array addi x2, x0, 8 # Size of array addi x3, x0, 0 # Counter for elements addi x5, x2, -2 # i<=5 addi x7, x0, 0 # result = 0 consecutive: lw x4, 0(x1) lw x6, 4(x1) lw x8, 8(x1) and x4, x4, x3 and x6, x6, x3 and x8, x8, x3 and x4, x4, x6 and x8, x8, x6 and x4, x4, x8 beq x4, x0, printf # If any of the elements is not odd, print "False" addi x1, x1, 4 # next element addi x3, x3, 1 # i = i+1 addi x7, x7, 1 # result+=1 beq x7, x5, printt j consecutive printf: la a0, false li a7, 4 ecall j exit printt: la a0, true li a7, 4 ecall exit: li a7, 10 ecall ``` ![Screenshot 2024-02-22 163724](https://hackmd.io/_uploads/rJ7bjiN2p.png) **for 1, 3, 4, 6, 8, 92, 13, 5** ``` assembly .data array: .word 1, 3, 4, 6, 8, 92, 13, 5 true: .string "True\n" false: .string "False\n" .text la x1, array addi x2, x0, 8 # Size of array addi x3, x0, 0 # Counter for elements addi x5, x2, -2 # i<=5 addi x7, x0, 0 # result = 0 consecutive: lw x4, 0(x1) lw x6, 4(x1) lw x8, 8(x1) and x4, x4, x3 and x6, x6, x3 and x8, x8, x3 and x4, x4, x6 and x8, x8, x6 and x4, x4, x8 beq x4, x0, printf # If any of the elements is not odd, print "False" addi x1, x1, 4 # next element addi x3, x3, 1 # i = i+1 addi x7, x7, 1 # result+=1 beq x7, x5, printt j consecutive printf: la a0, false li a7, 4 ecall j exit printt: la a0, true li a7, 4 ecall exit: li a7, 10 ecall ``` ![Screenshot 2024-02-22 164139](https://hackmd.io/_uploads/rkNW2jN3p.png) --- ## Question 3: **Code** ```assembly= .data array: .word 1 3 5 7 2 4 6 8 shuffle: .word 0 0 0 0 0 0 0 0 str: .string " " .text la x1 array la x2 shuffle la x3 shuffle addi x10 x0 0 #i=0 addi x11 x0 4 #4 elements in first half addi x12 x0 8 #size of array Loop: lw x6 0(x1) sw x6 0(x2) lw x6 16(x1) sw x6 4(x2) addi x1 x1 4 # next addi x2 x2 8 addi x10 x10 1 # i++ beq x10 x11 print #i=n j Loop print: lw x6 0(x3) addi a0 x6 0 li a7 1 ecall la a0 str li a7 4 ecall addi x3 x3 4 addi x12 x12 -1 beq x12 x0 exit j print exit: li a7 10 ecall ``` ![Screenshot 2024-02-22 220011](https://hackmd.io/_uploads/r1UiUxShT.png) --- --- ## Question 4: # Palindrome number: **Code** ``` assembly = .data num: .word 1234321 true: .string "True\n" false: .string "False\n" .text lw x0, num li x10, 10 isPalindrome: mv x2, x0 li x3, 0 # Initialize reversed number to 0 reverse: beqz x0, compare # If x0 =0,jump to compare rem x4, x0, x10 #last digit of x0 add x3, x3, x4 # Add th last digit to the reversed num mul x3, x3, x10 # Multiply the reversed number by 10 div x0, x0, x10 # Remove the last digit from x0 j reverse compare: beq x2, x3, printt #num = reversed num then jump to true la a0, false # Load the address of false string li a7, 4 ecall j end_function printt: la a0, true li a7, 4 ecall end_function: li a7, 10 ecall ``` ![Screenshot 2024-02-22 184318](https://hackmd.io/_uploads/BJ1tO64hp.png) --- --- ## Question 5: **1.Define Node Structure**: Which has two parts, data and pointer to store address of node. Node: Data pointer to next node **2.Memory Allocation** : Allocate memory dynamically to create nodes. **3.Insertion**: Create new node and set its data value. ***if insertion is at first position*** then point head to newly added nodes address and newnode pointer to node which is pointed previously by head. ***if insertion is at middle*** then based on which position we are inserting ,store this data in temp node to copy address of node at which we are inserting and then store address of next node in pointer of newly added node. **4.Deletion**: Select which node to be deleted.Then update pointer of previous node with address of next node of node which is going to be deleted. **5.Traversal**: We start from head and access next nodes using address stored in pointer of each node until we reach NULL. **6.Searching**: start from head. If current node matches the value return that node OR traverse along the list and check for the value till pointer points to NULL. --- ## Question 6: 1. **Memory Allocation:** We can use two HashMaps to keep track of memory blocks.One HashMap records allocated memory blocks,mapping their addresses to their sizes.Another HashMap tracks available memory blocks. When the malloc() function is called: It requests memory from the operating system using sbrk(), asking for more memory than needed to minimize system calls. If sbrk() succeeds, it updates the first HashMap to map the allocated memory's address to its size.It also marks this memory as used in the second HashMap-Finally, it returns a pointer to the allocated memory. 2. **Memory Deallocation (free() function):** The free() function in programming is used to release memory that was previously allocated using malloc(). When you allocate memory with malloc(), it's like claiming a piece of space to store data. After you're done using that space, you can free it up with the free() function. Free() works by checking the size of the memory block you want to free and then marking it as available for future use. Sometimes, there might be other nearby free memory blocks. Free() can combine these blocks to create larger free areas, which helps keep the memory organized. Additionally, if the computer no longer needs that memory, free() can return it to the system for other programs to use.