OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Nov 23, 2020 5:06 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Error in debugging code
PostPosted: Thu Jul 30, 2020 9:52 am 
Offline
Member
Member

Joined: Mon Apr 20, 2020 11:02 am
Posts: 68
IN the middle of implementing a low level BASIC like "Thing"(not sure what to call it, for now I call it executer), when I encountered this error. I was trying to implement negative number support in the code. In the "source code" it executes(not the source of the executer itself) negative numbers are simply number prefixed by a negative sign. My "executer" should convert this into actual negative numbers and store in arrays. There was some problem with that, so I wrote a bunch of debug-print code to check the issue.
In this code I am just printing an integer array to check if it contains the value it should contain..(array should not contain any negative value at this point)
Code:
    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);                              //converting to ASCII 
        print(val,2*l,0xd);                                   //my print function-takes a pointer, an offset, and a color value
        for(int m=0;val[m]!='\0';m++)             //clearing out val for next iteration
            val[m]='\0';
        l=k*0xA0;                                           //next line
    }

I tried to debug using this code, tried changing lot of stuff, but nothing. Soon I realized that I already fixed the main code but the debug print was actually buggy! It doesn't print exp[0], no matter what I try.For example, when I expect the output to be:
Quote:
    20480
    20530
    5

but it always do
Quote:
20530 ;omits the first value


5

If I try to force some other value into exp[0], then
Quote:
20530P\. ;invalid values, there is no way this can happen


5



This is my int_to_ascii code:
Code:
    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.
    }

I don't see a way from these code snippets that it should omit exp[0] or effect exp[1] when value of exp[0] is changed, when actually exp contains all desired values and my main code works. As it's just a debug-prints it's not super important, but still I would like to know the cause of this behavior.

_________________
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.


Top
 Profile  
 
 Post subject: Re: Error in debugging code
PostPosted: Thu Jul 30, 2020 11:05 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2474
Code:
char val[5]={};

If the numbers you're printing have five digits, where is your null terminator?

Code:
l=k*0xA0; //next line

That sets it to the current line, not the next line. So if you print entry 0, then you move the cursor to line 0... where will entry 1 be printed?


Top
 Profile  
 
 Post subject: Re: Error in debugging code
PostPosted: Thu Jul 30, 2020 11:17 am 
Offline
Member
Member

Joined: Mon Apr 20, 2020 11:02 am
Posts: 68
Octocontrabass wrote:
That sets it to the current line, not the next line. So if you print entry 0, then you move the cursor to line 0... where will entry 1 be printed?

hey that makes sense! I think the second entry overwrites the first entry, has to check that next day... I mainly use while loops and in that I increment before setting values... maybe habit got me

_________________
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group