-1

How can I identify what is the corresponding C code written for instruction in MIPS, ADDIU sp sp 0xFFE0? Using MPLAB IDE I can find out this address but am unable to find out, how does it work?

Ink
  • 1
  • 2
  • 2
    It's unclear what you need. Consult instruction set reference manual that will tell you what each instruction is doing and then you can code up the C equivalent. Note that `sp` being the stack pointer it's not normally manipulated directly by C code. That is likely allocating space for locals, and generated by a compiler as needed by e.g. `int foo[8];` – Jester Aug 27 '21 at 23:56
  • 1
    That's misleading / broken disassembly. It can't be adding `0xFFE0` to SP in one instruction because that value doesn't fit in a 16-bit signed immediate. Presumably it's actually adding `0xFFFFFFE0` to SP, i.e. 2's complement `-32` to reserve 32 bytes of stack space. A better disassembler would show you the actual value, not the encoding from the machine code. (if you want that, look at the machine code hexdump). – Peter Cordes Aug 28 '21 at 00:35
  • please provide a minimal example that demonstrates the problem. – old_timer Aug 28 '21 at 05:43
  • stack activity generated by a C compiler does not come directly from your code, it indirectly comes from your code. Cant explain it without a complete example (A simple C example and the corresponding compiler version and command line, along with the disassembly you are confused about (for that function)) – old_timer Aug 28 '21 at 05:45
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 01 '21 at 08:27

1 Answers1

1

There is no way to directly manipulate the stack pointer in C.

The stack pointer is for allocating storage that has function local scope/lifetime, since this storage must necessarily be deallocated upon function completion.

Since the addiu in question uses a constant, we can guess that a compiler or assembly programmer computed some fixed amount of stack space that is needed and allocated that.  We cannot say for what purpose though, as there are several.

Many of the reasons to use stack space are hidden from C programmers.  For example, let's take the snippet:

int a(), b();
int f() {
    return a() + b();
}

In order to perform the addition inside f, first a must be called, then b is called, but before b can be called, a's return value must be stored for the later addition.  (The C language allows b to be called first, then a, but still b's result will have to be saved somewhere.)

There are two practical places to store a's return value while b is being invoked, though, both of them require a word of stack space.  Such can be called expression temporaries, which are simply not seen by C programmers (not all expression temporaries require memory, some can use registers alone).

Further since MIPS uses a CPU register to capture the return address parameter, which is a value that needs to be saved so that another (set of) call(s) (a and b) can be made (using that same register), and still have f return to its caller later.  Another word of stack space is needed for this.  And once again, the return address is invisible to C programmers — though C programmers are aware of the notion of the call stack, the call stack is not really directly accessible with C language statements.

Beyond the hidden uses of stack memory, user arrays and local variables sometimes are allocated stack storage.

Any of the above usages could end up requiring stack space; the compiler would normally sum all such usages and allocate stack space once in function prologue and release it once in eppilogue.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53