Question: # Constants . data MEMORY _ SIZE: . word 4 0 9 6 # Total memory size CHUNK _ SIZE: . word 3 2 #

# Constants
.data
MEMORY_SIZE: .word 4096 # Total memory size
CHUNK_SIZE: .word 32 # Size of each memory chunk
NUM_CHUNKS: .word 128 # Total number of chunks
MEMORY_POOL: .space 4096 # Memory pool
msg1: .asciiz "
Enter Size of Variable: "
msg3: .asciiz "Enter operation to be performed (1-allocation,2-deallocate): "
error_msg: .asciiz "
Invalid input exitig the program."
result1: .asciiz "Memory alocated
"
result2: .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 1
# Error message
allocation_failed_msg: .asciiz "Memory allocation failed.
"
# Data section
allocated_chunks: .space 128 # Array to store allocation status of each chunk (0 for free, 1 for allocated)
.text
# Allocation function
main:
again:
li $v0,4
la $a0,msg3
syscall
li $v0,5
syscall
move $t2,$v0
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
move $t0,$v0 #$t0= number1
beq $t2,1,allocate_memory
beq $t2,2,deallocate_memory
li $v0,4
la $a0,allocation_failed_msg
syscall
b exit
allocate_memory:
# Load arguments from registers
lw $a0,0($sp) # Load size requested
lw $t0, CHUNK_SIZE # Load chunk size
div $a0, $t0 # Divide size by chunk size
mflo $t1 # Quotient contains number of chunks needed
# Traverse memory pool to find free space
li $t2,0 # Initialize loop counter
la $t3, MEMORY_POOL # Load address of memory pool
check_consecutive:
add $t6, $t2, $t5 # Calculate address of current chunk
li $v0,1 # Print integer syscall code
move $a0, $t6 # Value to print (address in $t6)
syscall # Print the value
lw $t7, allocated_chunks($t6) # Load allocation status of current chunk
li $v0,1 # Print integer syscall code
move $a0, $t7 # Value to print (value loaded in $t7)
syscall # Print the value
beq $t7,0, found_space # If chunk is free, continue checking
bge $t5, $t1, next_chunk # If not enough free chunks found, go to next chunk
addi $t5, $t5,1 # Increment counter
j check_consecutive
find_free_chunk:
lw $t4, allocated_chunks($t2) # Load allocation status of current chunk
beq $t4,1, next_chunk # If chunk is allocated, go to next chunk
# Check if enough consecutive chunks are available
li $t5,0 # Initialize counter for consecutive free chunks
found_space:
# Mark consecutive chunks as allocated
move $t8, $t2 # Store starting address of allocation
li $t9,1
# Set allocated status
allocate_chunk_loop:
sw $t9, allocated_chunks($t8) # Mark chunk as allocated
addi $t8, $t8,1 # Move to next chunk
addi $t5, $t5,-1 # Decrement counter
bgtz $t5, allocate_chunk_loop # Loop until all chunks are allocated
# Calculate and return address of allocated memory
mul $t0, $t0, $t2 # Calculate offset
la $v0, MEMORY_POOL # Base address of memory pool
add $v0, $v0, $t0 # Add offset
j end_allocation
next_chunk:
addi $t2, $t2,1 # Move to next chunk
bne $t2, $t1, find_free_chunk # Loop until all chunks are checked
# Allocation failed, raise exception or handle error
j allocation_failed
end_allocation:
jr $ra # Return to calle
b next1
next1:
li $v0,4
la $a0,result1
syscall
# Deallocation function
deallocate_memory:
# Load arguments from registers
lw $a0,0($sp) # Load address of memory to deallocate
la $t0, MEMORY_POOL # Base address of memory pool
sub $t1, $a0, $t0 # Calculate offset from base address
lw $t2, CHUNK_SIZE # Load chunk size
div $t1, $t2 # Divide offset by chunk size
mflo $t3 # Quotient contains index of chunk to deallocate
# Mark corresponding chunk as free
li $t4,0 # Set free status
sw $t4, allocated_chunks($t3)
jr $ra # Return to caller
b next2
b next2
next2:
li $v0,4
la $a0,result2
syscall
# Exception handling
allocation_failed:
# Handle allocation failure (e.g., raise exception or print error message)
li $v0,4 # Print string syscall
la $a0, allocation_failed_msg # Load address of error message
syscall
# Terminate program
beq $v0,$t1,exit
li $v0,10 # Exit syscall
syscall
exit:
# Constants . data MEMORY _ SIZE: . word 4 0 9 6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!