PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How to check if the stack is empty in MIPS?

 October 28, 2022     is-empty, mips, stack     No comments   

Issue

So I need to check if the stack is empty before returning a value that is being calculated. if its not empty, then I will raise an error.

How can I check if the stack is empty or not? Compare $sp with null, how? or if starting address of $sp is always the same, should I hard code it? (if address of $sp equal to 270346..3, then empty) (this feels very wrong)

Any help would be appreciated Thanks


Solution

You should never check if the call stack is "empty" — this concept doesn't really make sense — the call stack is supposed to always be there.


However, if you are pushing a dynamic number of things onto the stack, and later popping them all of them off, you can either:

  • Capture the value of stack pointer before any of the dynamic pushing, and then as part of dynamic popping compare the current stack pointer with that previously captured stack pointer — when they are equal, there's nothing to pop.

  • Alternately, start a count at zero before any dynamic pushing, and add to that count as items are pushed, decrement the count as they are popped — whenever the count is zero, then there's nothing to pop.


On the other hand, if you are writing a program that takes control directly from the simulator or from the operating system, these are special in that there is no one to return to.  Because of this, usually we use custom startup code for that, and such startup code is not a classic function.

Any code that is written as a function can assume that it was called, and thus can return to its caller.



Answered By - Erik Eidt
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing