Close

A gotcha with SDCC and stm8

ken-yapKen Yap wrote 03/28/2026 at 05:50 • 2 min read • Like

When I updated my SDCC version from 4.2 to 4.5 I was vaguely aware of a change in the calling convention for STM8 MCUs, so I rebuilt the ST Standard Peripherals Library following instructions that I had developed a few years ago so that the library would have the same calling convention as my application code.

What I missed was that I had an inline assembler routine to delay a few µs. Since the calling convention had changed the first argument was passed in A, not on the stack. The effect of the error was that I was seeing occasional random glitches in the display due to the 74HC595 not getting enough time to load bits some of the time. The following one line deletion fixed that:

diff --git a/display.c b/display.c
index 34ed872..31bb77c 100644
--- a/display.c
+++ b/display.c
@@ -153,11 +153,11 @@ static void display_fill(void)
 }
 
 // taken from https://github.com/unfrozen/stm8_libs/blob/master/lib_delay.c
+// modified for calling convention 1
 static void delay_usecs(uint8_t usecs)
 {
        usecs;
 __asm
-       ld      a, (3, sp)
        dec     a
        clrw    x
        ld      xl,a

You can find details of the calling convention, versions 1 and 0, in the SDCC manual, part of its documentation.

Like

Discussions