sdcc -v
3.6.2 #9667 (MINGW64)
sdcc is incorrectly reducing the subtraction of two independent calls to a function to zero.
Here is a test program showing the problem:
int RND0(void); int RND1(void); int Q; void main(void) { Q = RND0() - RND0(); }
sdcc -mz80 -S test.c
_main:: ;test2.c:9: Q = RND0() - RND0(); ld hl,#0x0000 ld (_Q),hl ret ~~~~~~ As you can see the difference of the two function calls has been replaced with zero. sdcc is not considering that function calls have side effects. In this case the function is a random number generator with the difference of two successive calls not being zero. If the program is changed to this: ~~~~~~ int RND0(void); int RND1(void); int Q; void main(void) { Q = RND0() - RND1(); }
The expected code is generated with both function calls being made and the difference computed.
Fixed in revision #9676.