Calculation error using local int variable

Bug reports (if you don't/won't have a Github account)
Post Reply
siggi
Well known member
Posts: 344
Joined: Thu Jul 26, 2007 9:06 am

Calculation error using local int variable

Post by siggi »

The current (and former versions) of z88dk do a wrong calculation of integer variables, when other local variables are stored on the stack:
Here is a ZX81 test program, which counts "link_no" down from 2 to -1. But when the expected value should be 0, -256 is printed! :(

Code: Select all

//zcc +zx81 -startup=2 -create-app -vn -o testint.bin testint.c
#include <stdio.h>
#include <zx81.h>
#include <string.h>

void main(void)
{
   int line = 0;
   int link_no = 0;
   int checkline;
   char show_dir;
   char key;
   char is_link, is_prog, wait_for_return, wait_for_key_released, show_len;
   char addr[10];


  link_no++;
  link_no++;
  printf("link_no(2)=%d\n", link_no);
  link_no--;
  printf("link_no(1)=%d\n", link_no);


  link_no--;
  printf("link_no(0)=%d\n", link_no);
  link_no--;
  printf("link_no(-1)=%d\n", link_no);

}
The printed values are
2
1
-256
-1

Siggi
stefano
Well known member
Posts: 2144
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

It was due to an optimization rule to gain little speed coming probably from me, now fixed.
stefano
Well known member
Posts: 2144
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

I just put in that optimization again in a fixed version.
Gains only one byte but should make the program run slightly faster.
stefano
Well known member
Posts: 2144
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

Just for your information, I spotted another problem on the latest optimization rules which did impact on the built libraries too.. I'm fixing it.
siggi
Well known member
Posts: 344
Joined: Thu Jul 26, 2007 9:06 am

Post by siggi »

The problem seems to be solved in the current nightly build.
But there is another problem:
sometimes the conversion from char=-1 to int results in 255.
I compiled with -O3 ...

Sorry, I could not make a small demo program to show that effect :-(

Siggi
stefano
Well known member
Posts: 2144
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

It could have to do with other opt rules I've just fixed.
Would you be so kind to try again tomorrow after the next build ?
siggi
Well known member
Posts: 344
Joined: Thu Jul 26, 2007 9:06 am

Post by siggi »

Done.
And the bug seems to be fixed :)
Siggi
stefano
Well known member
Posts: 2144
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

Great !
Phew.. I've been polling the site waiting for your answer 'till now ..
Post Reply