**LAB4** **NAME:** Satyannarayana banna **ROLL NO:** CS22B011 --- **Answer:1** ```assembly= .data array: .word 1,2,3,4,5,6,7,8,9,10,11 .text addi x1,x0,3 # intialise s to 3 addi x2,x0,10 # initialise i to 10 la x3,array #loading address of x10 into x3 addi x3,x3,36 addi x4,x0,-1 loop: lw x5,0(x3) add x5,x5,x1 sw x5,0(x3) addi x2,x2,-1 addi x3,x3,-4 bge x2,x4,loop ``` --- **Answer:2** ```assembly= .data array: .word 1,2,3,3,5,6,7,8,9,10,11 .text la x1,array addi x2,x0,11 addi x3,x0,0 addi x7,x0,1 addi x8,x0,0 addi x9,x0,0 addi x10,x0,3 loop: lw x4,0(x1) andi x5,x4,1 beq x5,x0,notodd addi x3,x3,1 addi x9,x9,1 addi x1,x1,4 bge x3,x10,fi bgt x9,x2,exit ble x9,x2,loop notodd: add x3,x0,x0 addi x9,x9,1 addi x1,x1,4 bgt x9,x2,exit j loop fi: addi x8,x0,1 exit: addi x11,x0,1 ``` --- **Answer:3** ```assembly= .data a: .word 1,2,3,4,5,6 s1: .string "There is no pair such\n" s2: .string "Exited\n" .text la x1 a la x2 a la x11 a addi x3 x0 6 addi x4 x0 6 addi x14 x0 4 addi x5 x0 0 addi x6 x0 1 psum: lw x7 0(x1) lw x8 4(x2) add x10 x7 x8 addi x2 x2 4 addi x6 x6 1 beq x10 x3 print beq x6 x4 sum bne x5 x4 psum sum: addi x1 x1 4 la x2 a addi x5 x5 1 beq x5 x4 print2 j psum print: sub x13 x2 x11 div x13 x13 x14 add a0 x13 x0 li a7 1 ecall li a7 4 ecall sub x13 x1 x11 div x13 x13 x14 add a0 x13 x0 li a7 1 ecall j exit print2: la a0 s1 li a7 4 ecall exit: la a0 s2 li a7 4 ecall ``` --- **Answer:4** Question 20 in leetcode ```assembly= .data s1: .string "true" s2: .string "false" .text .global main main: addi sp, sp, -4 la a0, s jal ra, isValid li a7, 10 ecall isValid: addi sp, sp, -4 li s0, 0 loop: lbu a1, 0(a0) beq a1, x0, check_empty addi a0, a0, 1 li t0, '(' beq a1, t0, push_parenthesis li t0, '{' beq a1, t0, push_parenthesis li t0, '[' beq a1, t0, push_parenthesis li t0, ')' beq a1, t0, pop_and_compare li t0, '}' beq a1, t0, pop_and_compare li t0, ']' beq a1, t0, pop_and_compare j loop push_parenthesis: sb a1, 0(sp) addi sp, sp, -1 j loop pop_and_compare: lbu t0, 0(sp) addi sp, sp, 1 beq a1, t0, loop la a0, s2 jr ra check_empty: beq s0, sp, return_true la a0, s2 jr ra return_true: la a0, s1 jr ra ``` --- **Answer:6** ```assembly= .data .align 3 FREE_LIST: .word 0 .text malloc: addi sp, sp, -4 sw ra, 0(sp) mv a1, a0 li a7, 214 ecall mv a0, a0 lw ra, 0(sp) addi sp, sp, 4 ret free: addi sp, sp, -4 sw ra, 0(sp) mv a0, a0 lw ra, 0(sp) addi sp, sp, 4 ret ``` --- **Answer:5** Linking: In a linked list, each node stores a value and a reference (address) to the next node. We start by setting the head variable to the address of the first node. Each node's address is stored in consecutive memory locations, ensuring that we can easily traverse the list. Delete: To delete a node, we adjust the references of the surrounding nodes. If we want to delete the first node, we simply update the head variable to point to the next node. If we want to delete any other node, we update the reference in the previous node to skip over the node to be deleted. Insert: When inserting a new node, we allocate memory for it and update the references accordingly. If we want to insert at the beginning of the list, we update the head variable to point to the new node, making it the new first node. If we want to insert at any other position, we traverse the list to find the desired position. Once found, we update the references of the surrounding nodes to include the new node. ---