Menu

#3487 stack variables being overwritten

closed-fixed
None
other
5
2023-01-21
2022-09-17
Under4Mhz
No

The below code produces 0 instead of 330. Looking at the assembler, it appears the stack position -6(ix) is reused to store the variables plane and then sprite.

If I remove the inner brace or remove y, it will correctly start the stack count from -10(ix).

Verbose asm attached.

//mult.c
///GPL 2.0 or later
void EnemyInit() {}
void EnemyHit() {}
void EnemyProcess() {}

#include <stdint.h>
#include <stdio.h>

// point
typedef struct {

    int16_t x;
    int16_t y;

} maths_point;

void TextNumber( uint8_t x, uint8_t y, uint16_t value );

void EnemyRender() {

    maths_point plane;

    int16_t posX = 2208;
    int16_t posY = 3680;

    plane.x = 0;
    plane.y = 63;

    uint8_t y = 16;

    {
        int16_t enemyX = 117 * 16;
        int16_t enemyY = 250 * 16;

        maths_point sprite;
        sprite.x = enemyX - posX;
        sprite.y = enemyY - posY;

        int16_t planeYspriteX = plane.y * sprite.x;
        int16_t planeXspriteY = plane.x * sprite.y;
        int16_t planeSpriteDiff = planeXspriteY - planeYspriteX;
        int16_t transformY = planeSpriteDiff / 64; // this is actually the depth inside the screen, that what Z is in 3D

        TextNumber( 0,16,transformY);
    }
}
//mult2.c:
///GPL 2.0 or later
#include <stdio.h>
#include <stdint.h>

void TextNumber( uint8_t x, uint8_t y, uint16_t value ) {

    printf( "%d", value );
}

void main() {

    printf("Start\n");
    EnemyRender();
    printf("End\n");
}

#ifdef __SDCC
__sfr __at 0xff sif;
int putchar( int c ) {

    sif = 'p';
    sif = c;

    return c;
}
#endif

// sdcc -mz80 --fverbose-asm -c ./mult.c && sdcc -mz80 --fverbose-asm -c ./mult2.c && sdcc -mz80 mult.rel mult2.rel  -o mult.ihx && ucsim_z80 -I if=outputs[0xff] mult.ihx
sdcc -mz80 --fverbose-asm -c ./mult.c && sdcc -mz80 --fverbose-asm -c ./mult2.c && sdcc -mz80 mult.rel mult2.rel  -o mult.ihx && ucsim_z80 -I if=outputs[0xff] mult.ihx
uCsim 0.7.5, Copyright (C) 1997 Daniel Drotos.
r
Start
0End
gcc mult.c mult2.c && ./a.out 
mult2.c: In function main:
mult2.c:12:5: warning: implicit declaration of function EnemyRender Start
330End
sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.9 #13701 (Linux)
3 Attachments

Related

Wiki: NGI0-Entrust-SDCC
Wiki: SDCC 4.3.0 Release

Discussion

  • Philipp Klaus Krause

    I can reproduce the issue using sdcc from current svn on my amd64 Debian GNU/Linux testing system.

     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    Fixed in [r13807].

     

    Related

    Commit: [r13807]


Log in to post a comment.

MongoDB Logo MongoDB