# Lab 4 Name: Bilwani k Roll No.: CS22B013 ## Question 1 ```assembly= .data .word 0x19 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0x10 0x11 base: .word 0x10000000 .text addi x10,x0,3 #s addi x11,x11,10 #i=10 lw x16, base Loop: lw x15, 0(x16) add x15, x15, x10 sw x15,0(x16) addi x16,x16,4 addi x11,x11,-1 bge x11,x0,Loop ``` ## Question 2 ```assembly= .data arr0: .word 0x1 0x2 0x34 0x3 0x4 0x5 0x7 0x23 0x12 arr1: .word 0x2 0x7 0x3 0x1 str_true: .string "True\n" str_false: .string "False\n" base: .word 0x10000000 .text addi x10, x0, 2 # i = 2 lw x16, base # Load base address of the array ProcessArrays: # Process arr0 la x18, arr0 # Load address of arr0 jal CheckConsecutiveOdds # Process arr1 la x18, arr1 # Load address of arr1 jal CheckConsecutiveOdds # End of program li a7, 10 # Load system call number for exit ecall CheckConsecutiveOdds: addi x10, x0, 2 # i = 2 lw x16, base # Load base address of the array Loop: lw x11, 0(x18) # Load arr[i] into x11 rem x12, x11, x0 # x12 = arr[i] % 2 bnez x12, FoundOdd # If arr[i] is odd, branch to FoundOdd # Move to the next iteration addi x10, x10, 1 # Increment i addi x18, x18, 4 # Move to the next element of the array bge x10, x0, Loop # Loop if i >= 0 # If not found, print False la a0, str_false # Load address of False string li a7, 4 # Load system call number for printing string ecall ret FoundOdd: lw x13, -4(x18) # Load arr[i-1] into x13 lw x14, -8(x18) # Load arr[i-2] into x14 rem x15, x13, x0 # x15 = arr[i-1] % 2 rem x17, x14, x0 # x16 = arr[i-2] % 2 bnez x12, PrintTrue # If all are odd, print True # Move to the next iteration addi x10, x10, 1 # Increment i addi x18, x18, 4 # Move to the next element of the array bge x10, x0, Loop # Loop if i >= 0 # If not found, print False la a0, str_false # Load address of False string li a7, 4 # Load system call number for printing string ecall ret PrintTrue: la a0, str_true # Load address of True string li a7, 4 # Load system call number for printing string ecall ret ``` ## Question 3 ```assembly= .data nums: .word 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 # Input array nums 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 s1, 0x10000028 main: li x4, 0 li x5, 5 la a0, str1 li a7, 4 ecall la a0, nums li a1, 20 li a7, 4 ecall Func: la a0, str3 li a7, 4 ecall mv a0, x1 li a7, 1 ecall beq x5, x4, Exit lw a0, 0(x1) sw a0, 0(s1) la a0, str4 li a7, 4 ecall mv a0, a0 li a7, 1 ecall lw a0, 0(x1) sw a0, 20(s1) addi x1, x1, 4 addi s1, s1, 8 addi x4, x4, 1 j Func Exit: la a0, str2 li a7, 4 ecall la a0, 0x10000028 li a1, 20 li a7, 4 ecall li a7, 10 ecall ``` ## Question 4 ```assembly= twoSum: addi sp, sp, -32 addi x1, x0, 0 la x2, nums la x3, map la x4, nums_end jal initialize_map loop1: bge x1, x4, no_solution lw x5, 0(x2) addi x6, x0, -1 jal find_complement beq x6, x0, loop_end la x7, result sw x1, 0(x7) sw x6, 4(x7) la a0, result addi sp, sp, 32 jr ra loop_end: addi x1, x1, 1 addi x2, x2, 4 j loop1 no_solution: addi a0, x0, 0 addi sp, sp, 32 jr ra initialize_map: beq x1, x4, end_initialize lw x5, 0(x2) sw x1, 0(x3) addi x1, x1, 1 addi x2, x2, 4 addi x3, x3, 8 j initialize_map end_initialize: jr ra find_complement: lw x6, 0(x3) beq x6, x0, no_complement beq x6, x5, complement_found addi x3, x3, 8 addi x6, x6, 1 j find_complement complement_found: lw a0, 4(x3) jr ra no_complement: addi x3, x3, 8 j find_complement .data nums: .word 1, 2, 3, 4, 5 nums_end: .word 5 map: .space 40 result: .space 8 ``` ## Question 5 ```assembly= .data newline: .string "\n" .text main: # Initialize the linked list la x5, elem1 la x6, elem2 la x7, elem3 la x8, elem4 # Link the elements together sw x6, 4(x5) sw x7, 4(x6) sw x8, 4(x7) # Print all elements of the linked list la a0, elem1 jal ra, print_list # Insert a new element into the linked list la x9, new_elem lw x10, 4(x5) sw x9, 4(x5) sw x10, 4(x9) # Print all elements of the updated linked list la a0, elem1 jal ra, print_list # Exit program li a7, 10 # Exit syscall ecall print_list: # Load the address of the head of the list into a1 mv a1, a0 loop: # Load the data of the current node into a1 lw a1, 0(a1) # Print the value of the current node mv a0, a1 li a7, 1 ecall # Print in newline la a0, newline li a7, 4 ecall # Get the next pointer lw a0, 4(a0) bnez a0, loop # If the next pointer is not zero, continue to the next node ret .data elem1: .word 1, 0 elem2: .word 2, 0 elem3: .word 3, 0 elem4: .word 4, 0 new_elem: .word 99, 0 ``` ## Question 6 ```assembly= void free(void *ptr) { if (ptr is NULL) { // Cannot free NULL pointer return; } // Assume we have a structure to keep track of allocated memory blocks struct MemoryBlock { void *address; size_t size; struct MemoryBlock *next; }; // Traverse the linked list to find the block to be freed struct MemoryBlock *current = head; // head points to the beginning of the list struct MemoryBlock *previous = NULL; while (current != NULL && current->address != ptr) { previous = current; current = current->next; } // If the block is found if (current != NULL) { // Remove the block from the linked list if (previous != NULL) { previous->next = current->next; } else { head = current->next; // Update head if the first block is to be freed } // Free the memory block brk(current->address); // Free the memory block structure free(current); } else { // Block not found, handle error or log a message // For example: printf("Memory block not found\n"); } }