s_werbenmanjensen
s_werbenmanjensen

Reputation: 21

MIPS: Using labels with memory offsets like $sp, doable? Bad practice?

I have a programming situation where a stack would work beautifully for my purposes. I was wondering if it was possible to use memory offset values in a similar manner to say, $sp, when addressing this allocated space.

IE:

.data
char_stack: .space 24

.text
main:
addi $t0, $zero, 18
sw $t0, 2(char_stack)

To save a value to the second byte of character stack? If my values are wrong but the idea is right, please let me know about the idea - the values are purely exemplary of what I wish to do.

Is this terrible practice? Something I should really avoid doing? If it isn't, but there isn't a way to do this, is the closest approximation using 'la' and an increment register?

(I'm using SPIM to simulate MIPS)

Also, I'm dealing with writing most of this in an exception handler (weird assignment) so I can't really make use of $SP all that effectively.

Upvotes: 2

Views: 266

Answers (1)

Bregalad
Bregalad

Reputation: 634

Why would it be a terrible practice ? I don't understand what do you fear. If you were describing the exact problem you're willing to solve with a custom stack then maybe we'd have better inisght of whether this is terrible practice. In this case your stack has space for 24 bytes, not a terribly big stack but depending on what you're trying to do it could be sufficient.

However the addressing mode you seem to be using offset(offset) is wrong and doesn't exist, on MIPS only offset(register) is available. (You could use offset(zero) to simulate it, but anyway I don't think it's what you wanted to do here.)

Instead, you'd want to initialize a register (stack pointer) to char_stack once in your program, and then increase it as you push data on your stack, decrease it as you pop.

I insist that sp is just an alias for r29 which is conventionally the stack pointer, but any other register is suitable as a local pointer to another stack.

If you only use this custom stack in a part of your program and want the register to be free for general usage elsewhere in the rest of your program, store it to a dedicated place in memory when you leave, and load it back when you enter back in your routine that uses the custom stack.

Upvotes: 2

Related Questions