0
votes

I was wondering about the use of stack point in Mips. My professor has told us than when we want to "create" space for a word in MIPS that we are going to need for instance in a recursive function we do it by : addi $sp, $sp, -4 which generally makes sense.
But we have also , said that when we want to free - up this space we move the stack pointer back to it's initial address by addi $sp, $sp, 4. When , we type this instuction what really happens to the this memory adress? Ok. I get it. The stack pointer , will no longer poitning out this value but 4 bytes , down ("deeper") in memory. But why does this means that , the space in memory is now free? I hope I made , clear my question.
To make sure let, me give you an example:

     li $a0, 6
     addi $sp, $sp, -4
    sw $a0, 0($sp) # let's suppose [a0] = 5 , now we loaded this value in memory adress 0($sp)
    la $t1, 0($sp) # load the current adress of sp in t1
    .....
    .....
    lw $a0, 0($sp) # why do we need this instruction?
    addi $sp , $sp, 4
    lw $t2, 0($t1) # i'm curious to see what's here

I tried, to print the result in $t2, and it printed 6 , so we don't literally free - up memory?

2
I'm not sure what you're trying to ask, but the CPU has no concept of "free" or "used" memory. Changing SP just changes SP. Keeping track of what the program uses (or does not use) is entirely the programmer's responsability.Mat
Changing the value in stack pointer register informs other software what stack memory is in use (claimed) and what isn't (e.g. is free or unclaimed). This is all by software convention; rules that are understood by assembly programmers and compilers; these rules allow for software libraries. The MIPS hardware instruction set architecture does not in any way favor the stack pointer register over the other integer registers. In theory we could switch to using a different register as the stack pointer (if every one of interest agreed).Erik Eidt

2 Answers

1
votes

When we change the stack pointer register's value, that is, by software agreement, an announcement to ourselves and other software of either claiming or releasing stack memory.  Of course, the memory is still there.

I tried, to print the result in $t2, and it printed 6 , so we don't literally free - up memory?

It did free up the memory conceptually, but of course, the memory is physically still there.  It is just that no one else used the memory in between your freeing it and your consulting it to print.  Though it happens to work in your experiment, in general this is an unsafe operation — that memory has been marked as being in the free & unclaimed area of the stack memory.  At some point that memory may be overwritten, so your code should not depend (on or even look at memory below that stack pointer).  The mechanisms that allow overwriting memory below the stack pointer are varied, but further function calling on the same thread, as well as signal handling may use unclaimed stack memory (if they do they should first claim the memory, use as needed, and later release it).

0
votes

The concept of free memory doesn't mean that the memory doesn't exist or is erased but it means that the memory is not used or claimed by another software and can be used by new programs if needed and as needed.

When your stack pointer moves to point to another memory address, it just points to a new memory location and forgets the previous one. That previous memory is now flagged as free and can be overwritten. However, until that overwrite happens your memory is going to continue to hold the previous value, which is what you see when you print it.