Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS stack pointer

Tags:

c

mips

Feeling a little silly here, trying to work out what this line is doing:

addiu   sp,sp,-24

I understand that it is setting the stack pointer, my confusion is the addiu part. It's adding the UNSIGNED -24 to the stack pointer, and storing it back in the stack pointer.

Does sp take the value of the 16 bit -24(FFE8) (the "immediate" value of the addiu operation is 16 bits wide) or does it extend the sign to the full 32 bits?

Also, is sp=0 prior to this instruction? It is the first instruction in a trivial piece of c code.

I'm implementing a MIPS core as a personal project, and this has me a little stumped.

like image 447
Zack Newsham Avatar asked Jul 13 '26 17:07

Zack Newsham


1 Answers

The addiu instruction includes a 16-bit immediate operand. When the ALU executes the instruction, the immediate operand is sign-extended to 32 bits. If two's complement overflow occurs during the addition, it is ignored.

addiu   d,s,const        # $d <—— s + const. 
                         # Const is 16-bit two's comp. sign-extended to 32 bits
                         # when the addition is done. No overflow trap.
like image 102
LPs Avatar answered Jul 15 '26 06:07

LPs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!