No. Recall that a stack has a "last-in, first-out" organization. Unneeded memory is always returned from the top of the stack.
A stack is an easily managed structure. Only a few memory addresses are needed to keep track of it. (Some of these addresses are in the stack pointer and in the frame pointer registers.) As a program executes, the stack grows and shrinks as subroutines are called and exited.
The heap is more like a book shelf. Books are constantly being taken off the shelf from various locations, leaving gaps, and then later returned, filling the gaps.
Here is how a SPIM program requests a block of memory from SPIM's heap. The program uses code 9 for get more space.
li $a0,xxx # $a0 contains the number of bytes you need. # This must be a multiple of four. li $v0,9 # code 9 == allocate memory syscall # call the service. # $v0 <-- the address of the first byte # of the dynamically allocated block
You don't know in advance what range of addresses you will get
back for the allocate memory request.
The SPIM service returns the first address of a contiguous block
with as many bytes as you requested in $a0
.
(This is similar to a call to malloc()
in "C".)
In a full operating system, is there a service for deallocating memory, for returning a previously allocated block to the system?