0

I was debugging a low level program, I had to ensure that my array exp have all values I expect it to have. So I wrote a code snippet that prints my array to help debug it. Problem is standard library isn't available so I need to use my own code. My code snippet:

int test=0;char val[5]={};
    int l=0;
    for(int k=0;exp[k]!=0;k++)
    {
        test=exp[k];                      //taking in value
        int_to_ascii(test, val);          
        print_char(val,2*l,0xd);
        for(int m=0;val[m]!='\0';m++)//clearing out val for next iteration
            val[m]='\0';
        l=k*0xA0;                    //next line
    }

exp is an integer array.. code for int_to_ascii:

if(src==0)
    {
        stack[i++]='\0';            //pushing NULL and 0 to stack
        stack[i++]='0';
    }
    else
    {
        while(src!=0)   //converting last digit and pushing to stack
        {
            stack[i]=(0x30+src%10);
            src/=10;
            i++;
        }
    }
    i--;
    len=i;
    while(i>=0)
    {
        dest[len-i]=stack[i];  //popping and placing from left to right
        i--;                   //inorder not to get reversed.
    }

print_char works because I use it to print entire window and interface. It basically takes char* szarray,int offset,int color.

I was yelling at my computer for nearly 2 hours because I thought my array is incorrect but it shouldn't be, but the actual problem was in this debug code.It doesn't print exp[0]. When it should output:

    20480
    20530
    5

It just prints

    20530


    5

I even tried brute forcing values into exp[0]. If I give any value except 20480 into that, it will print invalid characters into first entry,like this:

    20530P\.        ;not exact value, demonstration purpose only


    5

I think something is off in int_to_ascii, but that also is extensively used in other parts without any problems.

Any one have any idea with it?

  • 1
    You probably need to increase the length of the `val` array by 1 to leave room for a null terminator. – Ian Abbott Jul 29 '20 at 17:17
  • Please add a check to be sure `k` doesn't read past end of the 5 char array - assert or otherwise. Same for `m`. – Michael Dorgan Jul 29 '20 at 17:21
  • In my emulator uninitialized memory is always 0. So it 'has' a null terminator. Still tried changing it to `val[6]` but no change. –  Jul 29 '20 at 17:22
  • 1
    Also, you `src == 0` code is setting a NUL terminator (the `\0`) first, then placing the ascii `0` after. I'd also change the `0x30` to `'0'` for clarity. – Michael Dorgan Jul 29 '20 at 17:23
  • @MichaelDorgan I tried brute forcing values to exp[1], to which it shows the expected change in first entry. So `exp[1]` and `exp[2]` is printed, and no more. –  Jul 29 '20 at 17:24
  • @MichaelDorgan When I pop it back, it will get it the desired order, right? –  Jul 29 '20 at 17:32
  • 1
    Your `int_to_ascii` function doesn't seem to add a null terminator unless `src` is 0 at the start. – Ian Abbott Jul 29 '20 at 18:47

0 Answers0