Question: # Constants . data MEMORY _ SIZE: . word 4 0 9 6 # Total memory size CHUNK _ SIZE: . word 3 2 #
# Constants
data
MEMORYSIZE: word # Total memory size
CHUNKSIZE: word # Size of each memory chunk
NUMCHUNKS: word # Total number of chunks
MEMORYPOOL: space # Memory pool
msg: asciiz
Enter Size of Variable:
msg: asciiz "Enter operation to be performed allocation,deallocate:
errormsg: asciiz
Invalid input exitig the program."
result: asciiz "Memory alocated
result: asciiz "The memory along with the variable is now free
repeat: asciiz
Do you want to perform another calculation?
Enter y for yes or n for no
char: space
# Error message
allocationfailedmsg: asciiz "Memory allocation failed.
# Data section
allocatedchunks: space # Array to store allocation status of each chunk for free, for allocated
text
# Allocation function
main:
again:
li $v
la $amsg
syscall
li $v
syscall
move $t$v
li $v
la $amsg
syscall
li $v
syscall
move $t$v #$t number
beq $tallocatememory
beq $tdeallocatememory
li $v
la $aallocationfailedmsg
syscall
b exit
allocatememory:
# Load arguments from registers
lw $a$sp # Load size requested
lw $t CHUNKSIZE # Load chunk size
div $a $t # Divide size by chunk size
mflo $t # Quotient contains number of chunks needed
# Traverse memory pool to find free space
li $t # Initialize loop counter
la $t MEMORYPOOL # Load address of memory pool
checkconsecutive:
add $t $t $t # Calculate address of current chunk
li $v # Print integer syscall code
move $a $t # Value to print address in $t
syscall # Print the value
lw $t allocatedchunks$t # Load allocation status of current chunk
li $v # Print integer syscall code
move $a $t # Value to print value loaded in $t
syscall # Print the value
beq $t foundspace # If chunk is free, continue checking
bge $t $t nextchunk # If not enough free chunks found, go to next chunk
addi $t $t # Increment counter
j checkconsecutive
findfreechunk:
lw $t allocatedchunks$t # Load allocation status of current chunk
beq $t nextchunk # If chunk is allocated, go to next chunk
# Check if enough consecutive chunks are available
li $t # Initialize counter for consecutive free chunks
foundspace:
# Mark consecutive chunks as allocated
move $t $t # Store starting address of allocation
li $t
# Set allocated status
allocatechunkloop:
sw $t allocatedchunks$t # Mark chunk as allocated
addi $t $t # Move to next chunk
addi $t $t # Decrement counter
bgtz $t allocatechunkloop # Loop until all chunks are allocated
# Calculate and return address of allocated memory
mul $t $t $t # Calculate offset
la $v MEMORYPOOL # Base address of memory pool
add $v $v $t # Add offset
j endallocation
nextchunk:
addi $t $t # Move to next chunk
bne $t $t findfreechunk # Loop until all chunks are checked
# Allocation failed, raise exception or handle error
j allocationfailed
endallocation:
jr $ra # Return to calle
b next
next:
li $v
la $aresult
syscall
# Deallocation function
deallocatememory:
# Load arguments from registers
lw $a$sp # Load address of memory to deallocate
la $t MEMORYPOOL # Base address of memory pool
sub $t $a $t # Calculate offset from base address
lw $t CHUNKSIZE # Load chunk size
div $t $t # Divide offset by chunk size
mflo $t # Quotient contains index of chunk to deallocate
# Mark corresponding chunk as free
li $t # Set free status
sw $t allocatedchunks$t
jr $ra # Return to caller
b next
b next
next:
li $v
la $aresult
syscall
# Exception handling
allocationfailed:
# Handle allocation failure eg raise exception or print error message
li $v # Print string syscall
la $a allocationfailedmsg # Load address of error message
syscall
# Terminate program
beq $v$texit
li $v # Exit syscall
syscall
exit:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
