# Lab 4 Name: Anuj Sharma Roll No.: CS22B007 --- ## Question 1 ```assembly= .data arr: .word 1 5 10 39 5 40 3 9 6 90 base: .word 0x10000000 space: .string " " .text addi x3 x0 3 #x3 is s addi x5 x0 10 #x5 is i addi x21 x0 10 #x21 is length of array addi x13 x0 -1 lw x20 base # x20 is base pointer addi x4 x20 40 #x4 is pointer to array last element loop: beq x5 x13 exit #i > -1 lw x11 0(x4) add x11 x11 x3 #x[i] = x[i] + s sw x11 0(x4) addi x4 x4 -4 addi x5 x5 -1 j loop exit: addi x4 x20 0 addi x5 x0 0 ##code complete## loop1: #printing array lw x11 0(x4) addi a0 x11 0 li a7 1 ecall la a0 space li a7 4 ecall addi x4 x4 4 addi x5 x5 1 bne x5 x21 loop1 #i > -1 li a7 10 ecall ``` --- ## Question 2 ```assembly= .data arr: .word 1 2 3 4 5 6 7 8 9 10 length: .word 10 base: .word 0x10000000 true: .string "true\n" false: .string "false\n" .text lw x3 length #x3 is length of array lw x20 base #x20 is base of array addi x2 x20 0 #pointer to array addi x5 x0 0 #number of consecutive odd addi x9 x0 1 addi x4 x0 0 # i = 0 addi x15 x0 3 loop: beq x4 x3 exit1 lw x11 0(x2) and x12 x11 x9 #and with 1 bne x12 x9 if addi x5 x5 1 beq x5 x15 exit2 #if no. of odd is 3 j else if: addi x5 x0 0 else: addi x4 x4 1 addi x2 x2 4 j loop exit1: la a0 false li a7 4 ecall li a7 10 ecall exit2: la a0 true li a7 4 ecall li a7 10 ecall ``` --- ## Question 3 ```assembly= .data arr: .word 1 2 3 4 5 6 7 8 9 10 n: .word 5 base: .word 0x10000000 space: .string " " .text lw x3 n #x3 is n value lw x20 base #x20 is base of array addi x2 x20 0 #pointer1 to array addi x12 x20 0 #pointer2 to array addi x4 x0 0 # i = 0 loop1: beq x4 x3 e1 addi x4 x4 1 addi x12 x12 4 j loop1 e1: addi x4 x0 0 loop: beq x4 x3 exit1 lw x11 0(x2) lw x13 0(x12) addi a0 x11 0 li a7 1 ecall la a0 space li a7 4 ecall li a7 10 addi a0 x13 0 ecall la a0 space li a7 4 ecall addi x4 x4 1 addi x2 x2 4 addi x12 x12 4 j loop exit1: li a7 10 ecall ``` --- ## Question 4 Selected Question link https://leetcode.com/problems/search-insert-position/description/ ```assembly= .data arr: .word 1 2 3 4 6 7 8 9 10 11 n: .word 10 target: .word 5 base: .word 0x10000000 .text lw x3 n #x3 is length of array lw x20 base #x20 is base of array lw x5 target addi x2 x20 0 #pointer to array addi x4 x0 0 # i = 0 loop: beq x4 x3 exit1 lw x11 0(x2) bge x11 x5 exit1 addi x4 x4 1 addi x2 x2 4 j loop exit1: addi a0 x4 1 li a7 1 ecall ``` --- ## Question 5 ```assembly= # assume starting node x14 and ending node x15 initially both are equal and pointing to memory newNode: #argument x10 and return x11 #assume x20 cointains address to free unused memory sw x0 0(x20) #initialize value to 0 sw x10 4(x20) #initialize address to x10 addi x11 x20 0 #return address to memory through x11 addi x20 x20 8 #increase pointer of free memory jr x1 #return insertNode: addi x10 x0 -1 #set argument as -1, assume -1 to be null jal x1 newNode #create new node sw x11 4(x15) #set address of node to next of last node addi x15 x11 0 #update last node jr x1 #return traverse: addi x4 x14 0 #initialize x4 to starting node for traversal loop: #excess memory at x4 sw x4 4(x4) #go to next address beq x4 x15 exit #if node is equal to last node end loop j loop jr x1 #return ``` --- ## Question 6 ```cpp= #include <unistd.h> // Define a block structure to store information about each allocated block struct Block{ size_t size; struct Block* next; }; // Initial heap block static Block* heap = NULL; // Function to allocate memory void* malloc(size_t size) { if (size==0) { return NULL; } // Check if the heap is uninitialized if (heap==NULL) { heap=sbrk(0); heap->size=0; heap->next=NULL; } // Iterate through the linked list to find a free block // this is added by free function if it is called Block* current= heap; while (current->next!=NULL) { if (current->next->size >= size) { // Found a free block with sufficient size Block* allocatedBlock = (Block*)((char*)current->next + sizeof(Block)); allocatedBlock->size = size; return (void*)allocatedBlock; } current = current->next; } // No free block found, extend the heap current->next=sbrk(sizeof(Block) + size); if (current->next== (void*)-1) { return NULL; // Failed to extend the heap } current->next->size=size; current->next->next=NULL; return (void*)(current->next); } // Function to deallocate memory void free(void* ptr) { if (ptr == NULL) { return;//ptr is null } // get the original block address Block* block = (Block*)((char*)ptr - sizeof(Block)); // aqdd the block back to the free list block->next = heap->next; heap->next = block; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up