ST7 Introduction

        .cr     st7         To load this cross overlay

The STMicroelectronics ST7 is the core of many different micro controllers. Even customer specific derivatives can be created around this powerful 8-bit core.
I have included this cross overlay upon request and it is available in version 3 only.

Programming Model

The programming model in the picture below shows the most important registers of the ST7 processor. I only include a little summary about the features of the ST7's programming model here. It is not my intention to make the original documentation obsolete, so please refer to the original documentation for further details.

ST7 programming model

The small amount of registers is generously compensated by a huge variety of addressing modes, including short/long, indirect and indexed addressing modes. Especially the short addressing mode helps in accessing important data quickly and efficiently. In most cases the assembler will help you by selecting the most economical addressing mode.

The Accumulator A

This register has the traditional role of Accumulator and is used for all computational purposes.

The Index registers X and Y

These two registers can be used as pointers and offsets, or for temporary storage of any 8-bit value. Instructions using the Y register usually require one more byte of code compared to the same instruction using the X register. At the same time they will take one more instruction cycle to execute.

The Condition Code Register

The CC register holds all the system flags. Most flags reflect the status of the machine after mathematical or logical instructions.

The CCR contains 5 system flags:

1Always reads as 1
1Always reads as 1
1Always reads as 1
HHalf Carry flag
IInterrupt mask flag
NNegative sign flag
ZZero flag
CCarry Flag

All interrupts are disabled when the I bit is set.
The three unused bits always read as "1".

The Stack Pointer

The 6 least significant bits of the stack pointer point to the next free location on the stack. The 10 most significant bits of the stack pointer are preset to a specific location in system RAM. The actual value is determined during production of the chip.

After an MCU reset, or after the Reset Stack Pointer instruction (RSP), the Stack Pointer is set to its upper value.
After each push the pointer is decremented. Before each pull the pointer is incremented.
When the lowest stack location is written the stack pointer wraps around to the upper limit. Subsequent pushes will therefore overwrite previous values, almost certainly crashing the program.

A subroutine call pushes 2 bytes on the stack. An interrupt call pushes 5 bytes on the stack.

The Program Counter

The program counter PC is normally incremented after fetching each instruction or operand byte during program execution. The only way you can change this behaviour is with the jump, subroutine and return instructions. Also interrupts can change the program counter's value.


SB-Assembler Version 3 can show you the cycle times of each instruction when the TON list flag is switched on. The numbers presented are the number of clock cycles each instruction takes.

Reserved Words

The single letters A, X, Y and S are better not used as label names to avoid confusion with the similar named registers.

The Indirect addressing mode takes the form ([address],X), where address is a zero page address. However the processor can have a short or a long address stored in that location to determine the EA. There is no way for the assembler to know whether the short or long mode is to be used. ST has decided to postfix address with the letters .W in case the long mode is intended.
Therefore labels should not end with the letters .W, otherwise the assembler may get confused when using it in indirect/indexed addressing mode. Thus ([label.W],X) will use the ZP mode of label, which stores the first half of the destination. Label + 1 will then store the second half of the destination.

Special Features

Indirect addressing mode

I personally don't like the postfixing notation for the long version of the indirect addressing mode. Therefore the assembler also accepts an alternative notation. The notation ({label},X) is the same as the original notation ([label.W],X). Both notations mean that the ZP location label stores a long address.
The original notation is still accepted to make the assembler compatibility reasons.

Forced short and long addressing modes

Short addressing mode is also called zero page addressing mode, because the ST7 can only use memory page 0 when using short addressing mode. With short addressing mode you specify a memory location that can be addressed with only one byte (instead of 2 for all other memory locations).

The SB-Assembler automatically selects short addressing mode when that mode is available and the high byte of the address is $00 (being the zero page). We only know for sure that the high byte of the address is $00 if there was no unresolved label in the expression identifying the address. If a forward referenced label is used in an address expression we automatically assume the worst case situation and opt for long addressing mode (2 bytes address field).
You may override this automatic selection of addressing mode by preceding the address field with a < or a > symbol.
The < symbol forces the assembler to use short addressing mode, even if the address expression contains a forward referenced label.
On the other hand the > symbol will force the assembler to use the long addressing mode, even if the address could be resolved to a short address.
However a Range Error will be reported if you try to force to use the short addressing mode where the high byte of the address isn't zero.


0010-           LABEL      .EQ   $10           A short page address
0000-BB 10                 ADD   A,SHORT       Appears to be short address
0002-CB 00 11              ADD   A,FORWARD     Length unknown. Use worst case
0005-BB 11                 ADD   A,<FORWARD    Length unknown. Force short mode
0007-CB 00 11              ADD   A,>$0011      Is a short, but force long mode
0011-           FORWARD    .EQ   $11           A zero page address

Overlay Initialization

Three things are set while initializing the ST7 overlay every time it is loaded by the .CR directive.

  • Code memory is selected
  • Big endian model is selected for 16-bit addresses and for the .DA and .DL directives. This means that words or long words are stored with their high byte first.
  • The maximum program counter value is set to $FFFF.

Differences Between Other Assemblers

There are some differences between the SB-Assembler and other assemblers for the ST7 family of processors. These differences require you to adapt existing source files before they can be assembled by the SB-Assembler. This is not too difficult though, and is the (small) price you have to pay for having a very universal cross assembler.

  • Not all assemblers will understand forced short and long addressing modes.
  • Long indirect addressing mode notation ({label},X) may be unique for the SB-Assembler. Other assemblers may not recognize it. Use the original notation ([label.W],X) in case the other assembler complains about it.
  • The obvious differences in notation of directives common to all SB-Assembler crosses.
  • Don't forget that the SB-Assembler does not allow spaces in or between operands. Only Version 3 will allow one space after each comma separating operands in the operand field.