# Lab 4 Name: I.Kalyan Anudeep Roll No.: CS22B025 --- ## Question 1 **Code** (if required) ```assembly= .data demo: .string "This is where you can display your assembly code" ``` ```c= .data .word 0x1 0x1 0x5 0x6 0x7 0x5 0x6 0x4 0x4 0x8 0x6 base: .word 0x10000000 str: .string " " .text lw x16 base addi x4 x0 3 addi x5 x0 1 addi x8 x0 1 addi x7 x0 12 Loop1: lw x6 40(x16) addi x6 x6 3 sw x6 40(x16) addi x16 x16 -4 addi x5 x5 1 bne x7 x5 Loop1 lw x16 base Loop2: lw x1 0(x16) li a7 1 addi a0 x1 0 ecall li a7 4 la a0 str ecall addi x16 x16 4 addi x8 x8 1 bne x8 x7 Loop2 ``` _You can use either one of these according to the question_ **1 a** Your answer **1 b** Your answer **1 c** Your answer **Observation:** (if required) This is where you will write your observations * Line 1 * Line 2 * Line3 --- ## Question 2 **Code** (if required) ```assembly= .data demo: .string "This is where you can display your assembly code" ``` ```c= .data .word 0x2 0x3 0x2 0x5 0x5 0x7 0x8 base: .word 0x10000000 str: .string "true" str1: .string "false" str2: .string "" .text addi x8 x8 7 #no of elements addi x7 x0 -1 #to make a counter for number of elements lw x16 base odd1: addi x7 x7 1 addi x16 x16 4 lw x1 -4(x16) Loop1: addi x1 x1 -2 addi x6 x0 0 beq x7 x8 exit beq x1 x0 odd1 blt x1 x0 odd2 bne x1 x0 Loop1 addi x6 x0 0 j exit odd2: addi x7 x7 1 addi x16 x16 4 lw x1 -4(x16) Loop2: addi x1 x1 -2 addi x6 x0 0 beq x7 x8 exit beq x1 x0 odd1 blt x1 x0 odd3 bne x1 x0 Loop2 addi x6 x0 0 j exit odd3: addi x7 x7 1 addi x16 x16 4 lw x1 -4(x16) Loop3: addi x1 x1 -2 addi x6 x0 1 beq x7 x8 exit beq x1 x0 odd1 blt x1 x0 exit bne x1 x0 Loop3 exit: beq x6 x0 e1 j e2 e1: li a7 4 la a0 str1 ecall j exi e2: li a7 4 la a0 str ecall exi: li a7 4 la a0 str2 ecall ``` _You can use either one of these according to the question_ **2 a** Your answer **2 b** Your answer **2 c** Your answer **Observation:** (if required) This is where you will write your observations * Line 1 * Line 2 * Line3 --- _And so on_ --- ## Question 3 **Code** (if required) ```assembly= .data demo: .string "This is where you can display your assembly code" ``` ```c= .data .word 0x1 0x4 0x2 0x5 0x4 0x1 base: .word 0x10000000 arr1: .word 0x0 0x0 0x0 arr2: .word 0x0 0x0 0x0 str: .string " " .text lw x16 base addi x1 x0 6 addi x3 x0 0 loop: addi x1 x1 -2 addi x3 x3 1 bne x1 x0 loop #in x3 we can get half the no of elements addi x22 x0 0 lw x16 base la x13 arr1 la x14 arr2 loop2: lw x21 0(x16) addi x16 x16 12 lw x2 0(x16) addi x16 x16 -12 sw x21 0(x13) sw x2 0(x14) addi x16 x16 4 addi x13 x13 4 addi x14 x14 4 addi x22 x22 1 bne x22 x3 loop2 la x13 arr1 la x14 arr2 addi x15 x15 0 loop3: lw x7 0(x13) li a7 4 la a0 str ecall li a7 1 addi a0 x7 0 ecall li a7 4 la a0 str ecall lw x8 0(x14) li a7 1 addi a0 x8 0 ecall addi x13 x13 4 addi x14 x14 4 addi x15 x15 1 bne x15 x3 loop3 ``` _You can use either one of these according to the question_ **3 a** Your answer **3 b** Your answer **3 c** Your answer **Observation:** (if required) This is where you will write your observations * Line 1 * Line 2 * Line3 --- ## Question 4 **Code** (if required) ```assembly= .data demo: .string "This is where you can display your assembly code" ``` ```c= addi x1 x0 4 #dividend addi x2 x0 3 #divisor addi x3 x0 0 Loop: sub x1 x1 x2 blt x1 x0 exit addi x3 x3 1 bne x3 x0 Loop exit: li a7 1 addi a0 x3 0 ecall ``` _You can use either one of these according to the question_ **4 a** Your answer **4 b** Your answer **4 c** Your answer **Observation:** (if required) This is where you will write your observations * Line 1 * Line 2 * Line3 --- ## Question 5 **Code** (if required) ```assembly= .data demo: .string "This is where you can display your assembly code" ``` ```c= ``` _You can use either one of these according to the question_ **5** 1.First I need to store every data element as a charcter in a register as it is only 8 bits or 1 byte(suppose in x1) 2.Same as above,store address of the next node we need in a another register(suppose in x2) 3.Now store character as a first 8 bit in another register(suppose in x3) 4.Now store first 24 bits or 3 bytes in the register from the place after occupied by the character(same as above) 5.Lilke above linked list is implemented **Observation:** (if required) This is where you will write your observations * Line 1 * Line 2 * Line3 --- ## Question 6 **Code** (if required) ```assembly= .data demo: .string "This is where you can display your assembly code" ``` ```c= ``` _You can use either one of these according to the question_ **6** 1.As the program break pointer is at last of the program when malloc is implemented the break pointer which points to the starting address of the allocated memory and an extra memory should be before the pointed memory which should store the memory elemnts allocated 2.Then when free is implemented then free knows how many memory elements to be deallocated and deallocates those many number of elements **Observation:** (if required) This is where you will write your observations * Line 1 * Line 2 * Line3