STM8 Assembly startup code: Access Core registers to initialize stack pointer

66 views Asked by At

I am currently trying to write startup code in assembly for an STM8 without any vendor libraries. More specifically, I am using an STM8S103f3p6. I am still inexperienced in assembly and with startup code, but I am confident to know what has to be done at the start of a µC. But apparently I don't know, HOW it is done. For assembly, I am currently using the STM8-AS which is based on GNU AS. At the very start of my reset_handler, I want to set the stack pointer to the end of RAM 0x0003FF like shown in the datasheet (direct download). But the reference manual states that the stack pointer (SPH and SPL) can not be accessed by memory access instructions. But setting the SP in debug mode can't be correct, can it? I am unsure what to make of that enter image description here

1

There are 1 answers

0
the busybee On BEST ANSWER

According to the data sheet table 9, the SP holds 0x03FF on reset. So you don't need to set it explicitly in this case. The programming manual says the same in chapter 3.2:

After an MCU reset the Stack Pointer is set to its upper limit value.

However, if you want to load a value into the SP, you can use only one of these instructions according to the programming manual (chapter 7.4 on LDW):

  • LDW SP, X
  • LDW SP, Y

The registers X and Y can be loaded before with immediate values, for example LDW Y, #$03FF.

The complete sequence with Y is:

        LDW Y, #$03FF
        LDW SP, Y