0

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)?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
KiYugadgeter
  • 3,796
  • 7
  • 34
  • 74
  • You need to provide more context. Also, please indicate what architecture you're asking about. – icecreamsword Sep 19 '16 at 00:47
  • 2
    I pulled out my old PDP-11 assembly language book (by Thomas Frank). It has been a while, but I can confirm that SP (aka R3) supports pre-decrement and post-decrement. (as well as increment variants). It supports 1 and 2 byte increment/decrement. -(sp) would pre-decrement in a similar fashion as peter described for the m68k. – Michael Petch Sep 19 '16 at 01:15
  • for example, There is `-(sp)` in m40.s where definition of _idle. – KiYugadgeter Sep 19 '16 at 01:21
  • Was it something like `_idle: mov PS,-(sp)` ? – Michael Petch Sep 19 '16 at 01:24
  • @MichaelPetch Yes. why move PS to minus stackpointer? what is -(sp)? – KiYugadgeter Sep 19 '16 at 01:30
  • 1
    It works like `push` in other architectures . you decrement _sp_ first and then move data to that location. `mov PS,-(sp)` moves the WORD value from memory location pointed to by `PS` to the memory location *sp*-2 . My recollection on most PDP-11 assemblers is that _MOV_ assumes WORD (2 byte values), _MOVB_ is byte. – Michael Petch Sep 19 '16 at 01:39

2 Answers2

3

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.

  • In a MOVE.L, it decrements by 4. (like C *(sp-=4))
  • In a MOVE.W, it decrements by 2.
  • In a 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).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
0

-(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.

wfjm
  • 1,528
  • 10
  • 12