Section 05 Part 04 – The SP (Stack Pointer)

 

 “A whole stack of memories never equal one little hope.” ~Charles M. Schulz

 

 

 

Introduction

 

There will be times where the 68k will need to store some important information, and there will also be times where “you” will need to store some important information.

 

The stack is designed exactly for this purpose.

 

 

 

The Stack

 

The stack is a memory space where important information can be stored temporarily.  You can decide where in 68k memory the stack is, and can change the address of the stack at any time.

 

The address register a7 is used as the Stack Pointer (SP), and you’ll need to decide where in 68k memory you want the stack to be.  There are two ways of setting the stack; using a7:

 

          movea.l   #$00100000,a7

 

Or by using SP:

 

          movea.l   #$00100000,sp

 

Both are acceptable, and are exactly the same.  a7 is used as the SP, so a7 is the SP.

 

 

 

Storing

 

In the above example, I’ve set the stack address to 68k offset 100000, however, memory is stored inside the stack in reverse order.  Here’s an example of saving data into the stack (I’ve used SP, but you can use a7 if you prefer):

 

          move.w    d0,-(sp)

          move.l    d1,-(sp)

 

Please note how SP is treated as an address register, and also note the “-“ symbol.  Starting with the first instruction, a word of d0 is saved into the stack, d0 contains FF000020, and so 0020 is saved into the stack:

 

Offset

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

000FFFD0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

000FFFE0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

000FFFF0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

20

00100000

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

etc

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

As you can see above, the word was saved into memory at offset 0FFFFE and 0FFFFF.  This is because the stack is moved back before the 0020 is saved into memory.  After this, the SP will be at offset 0FFFFE.

 

The next instruction moves a long-word of data from d1 into the stack, so we’ll pretend that d1 contains FEDCBA98, FEDCBA98 is saved into the stack:

 

Offset

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

000FFFD0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

000FFFE0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

000FFFF0

00

00

00

00

00

00

00

00

00

00

FE

DC

BA

98

00

20

00100000

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

etc

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Once again, the SP is moved back from offset 0FFFFE to 0FFFFA, and FEDCBA98 is saved into memory.

 

 

 

Reloading

 

Loading the data back out of the stack is done in reverse order:

 

          move.l    (sp)+,d1

          move.w    (sp)+,d0

 

The first instruction will load a long-word from the stack at offset 0FFFFA back into d1.  FEDCBA98 is copied out of the stack and put back into d1.  The SP is then moved forwards to offset 0FFFFE.

 

The next instruction loads a word from the stack at offset 0FFFFE back into d0.  0020 is copied out of the stack and put back into d0.  The SP is moved forwards to the offset 100000 once again.

 

 

 

Summary

 

It is probably worth mentioning that when you load data out of the stack; you’re only “copying” the data out.  This means that even though the address of the SP moves forwards, a copy of the data is still technically inside the stack:

 

Offset

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

000FFFD0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

000FFFE0

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

000FFFF0

00

00

00

00

00

00

00

00

00

00

FE

DC

BA

98

00

20

00100000

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

00

etc

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This data will be overwritten when you (or the 68k) stores something new into the stack later on.

 

Speaking of the 68k storing information, the next part will teach an instruction that causes the 68k to use the stack.  This will give you a good example of why the stack exists.

 

 

 

Previous Part

Main Page

Next Part