What's the meaning of -(sp)
?
I am reading code of Unix V6, on PDP-11 the old OS.
I already know the meaning of sp
, this is stack pointer.
Is this same as -1(sp)
?
What's the meaning of -(sp)
?
I am reading code of Unix V6, on PDP-11 the old OS.
I already know the meaning of sp
, this is stack pointer.
Is this same as -1(sp)
?
This answer was written before the question was clarified to PDP-11. I guessed that it looked like an m68k pre-decrement addressing mode.
Michael Petch confirms that PDP-11's -(SP)
syntax decrements by 1 or 2 bytes depending on operand size, so it was almost certainly the inspiration for Motorola's syntax.
This page explains that -(A0)
syntax means to pre-decrement the pointer by the width of the operand.
MOVE.L
, it decrements by 4. (like C *(sp-=4)
)MOVE.W
, it decrements by 2.MOVE.B
, it decrements by 1. (Unless the address register is A7, aka SP, in which case it still decrements by 2 so it stays aligned!)Similarly, +(A0)
works the same way as a post-increment operator.
They work like x86's PUSH (pre-decrement the stack pointer) and POP (post-increment the stack pointer).
-(sp)
means decrement the register sp
before its used.
Because sp
is the stack pointer this is the usual way to write to the stack
mov r0,-(sp)
will for example save (or push) the register r0 on the stack, and
mov (sp)+,r0
will pop the value and restore the register again.