Memory Allocation in MIPS. Can anyonek tell me if this code uses \"First-bit\" o
ID: 3839285 • Letter: M
Question
Memory Allocation in MIPS. Can anyonek tell me if this code uses "First-bit" or Best-fit" Trying to understand how this stuff works.
# This program ask a user if they
# want to allocate memory
# or deallocate.
.data
newline: .asciiz " "
prompt1: " Please select if you want to Allocate or Deallocate: "
prompt2: "a - Allocate "
prompt3: "d - Deallocate "
promptl: "p - Print "
promptp: "l - Load "
prompt4: "q - Quit the program "
prompt5: " Choose a character: "
prompt6: " What do you want to save into the memory? "
prompt7: " How much do you want to deallocate "
prompt8: " Error that is too large! "
prompt9: " Enter value to save "
goodbye: " Goodbye!"
.text
add $s1, $zero, $zero
li $s2, 0
# Prompt Function
prompt:
li $v0, 4
la $a0, prompt1 #Please select if you want to Allocate or Deallocate:
syscall
li $v0, 4
la $a0, prompt2 #[A] - Allocate
syscall
li $v0, 4
la $a0, prompt3 #[D]- Deallocate
syscall
li $v0, 4
la $a0, promptl #[L]- Load
syscall
li $v0, 4
la $a0, promptp #[Q] - Print
syscall
li $v0, 4
la $a0, prompt4 #[Q] - Quit the program
syscall
##############################
# User Input
#Ask
getinput:
li $v0, 4 #Choose a character:
la $a0, prompt5
syscall
li $v0, 12 #Read a single character from console
syscall
move $s0, $v0
beq $s0, 97 , allocate #If you press 'a', allocate
beq $s0, 100 , deallocate #If you press 'd', deallocate
beq $s0, 108 , load #If you press 'l', load
beq $s0, 112 , suma #If you press 'p', print
beq $s0, 113 , exit #If you press 'q', exit
li $v0, 4 #If you press something random
la $a0, newline #Display new line
syscall
j getinput #And ask for a new character
#####################################################
# Allocate memory function
allocate:
li $v0, 4
la $a0, prompt6 #What do you want to save into the memory?
syscall
#Read number
addi $v0, $zero, 5 #Reads number in register $v0
syscall
move $t1, $v0 # Sets register $v0 to register $t1
la $a0,newline # system call to print
li $v0,4 # out a newline
syscall
#Prints to check if its an integer....not a charcater
add $a0, $zero, $t1
addi $v0, $zero, 1
syscall
li $t4, 2048
slt $t5, $t1, $t4 #t5 = 1
beq $t5, $zero, error #else t5 = 0
li $t2, 32
div $t1, $t2
loopa:
addi $sp, $sp, 128
addi $t1, $t1, -1
bgtz $t1,loopa #branch if t1 is greater than loopa
move $s3,$v0 #set contents s0 to v0
#test if memory is allocating
#debugging statement
li $t0, 40
sw $t0, 0($s3)
lw $a0, 0($s3)
li $v0, 1
syscall
j getinput
#Load
load:
li $v0, 4
la $a0, prompt9 #Ask user to save a number
syscall
li $v0, 5
syscall
move $t1, $v0
#addi $s0, $s0, 4 # increments the array
sw $t1, 0($s0)
#addi $s0, $s0, 4 # increments the array
addi $s2, $s2, 1
j getinput
suma: add $t5, $zero, $zero # sets $t5 to 0
move $t6, $s2 #move contents of t6 to s2
sum: slti $t0, $t6, 1
bne $t0, $zero, getinput
lw $t7, 0($s3)
add $a0, $zero, $t7
addi $v0, $zero, 1
syscall
addi $s0, $s0, 4
addi $t6, $t6, -1
j sum
###############################################
# Deallocate memory
deallocate:
li $v0, 4
la $a0, prompt7
syscall
#Read number
addi $v0, $zero, 5
syscall
#Print to check
add $a0, $zero, $v0
addi $v0, $zero, 1
syscall
la $a0,newline # system call to print
li $v0,4 # out a newline
syscall
move $t1, $v0
li $t2, 32 #??
div $t1, $t2
#Deallocation loop
loopd:
addi $sp, $sp, -128
addi $t1, $t1, -1
bgtz $t1,loopd
j getinput
error:
#Print string
li $v0, 4
la $a0, prompt8
syscall
la $a0,newline # system call to print
li $v0,4 # out a newline
syscall
j getinput #And ask for a new character
#########################################
# Exit function
exit:
li $v0, 4 #Say
la $a0, goodbye #Goodbye!
syscall
li $v0, 10 #Terminate Program
syscall
Explanation / Answer
# Store input numbers num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))