Page 1 of 1

Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 4:09 am
by Benjamin1996
Hello everyone!
I've been reading all of the posts I could find here on the forum, asking questions about their getLine() functions, but I haven't been able to resolve my problem by reading any of them.
Anyway, MY problem is that my getLine() function, doesn't seem to be returning anything.
Or, nonetheless, I can't seem to make it print out the returned string..
Here's all of the functions I feel you need to see:

Code: Select all

char checkBit(char value, short position){
    return (value & (1 << position)) > 0;
}

char getKey(){
    char status = in(0x0064);
	
    while(checkBit(status, 0) == 0){
        status = in(0x0064);
    }
	
    return in(0x0060);
}

char* getLine(short row, short column){
    short cursorPosition = column - 1;
    char scanCode = 0;
    char* line;
	
    while(scanCode != 0x001c){
       scanCode = getKey();
	    char ascii = handleScanCode(scanCode);
	    *line++ = ascii;
		
	    if(ascii != 0){
	        printChar(ascii, row, column);
	        column += 2;
	        out(0x03d4, 0x000e);
	        out(0x03d5, row);
	        out(0x03d4, 0x000f);
	        out(0x03d5, cursorPosition++);
       }
    }
	
    return line;
}
And by the way, my handleScanCode(char scanCode) function, is just a switch statement switching between the value of "scanCode" and returns the corresponding ASCII character.
This is how I call the function and prints out the string:

Code: Select all

    char* command = (char*)getLine(0, 4);
    print(command, 1, 0);
Help will really be appreciated! :)

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 6:11 am
by Gigasoft
You're not initializing "line" anywhere. Besides, the cursor position in CRTC register 14 and 15 is an address, not the row and the column.

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 6:38 am
by Benjamin1996
Gigasoft wrote:You're not initializing "line" anywhere. Besides, the cursor position in CRTC register 14 and 15 is an address, not the row and the column.
The following is the new function (there's no difference):

Code: Select all

char* getLine(short row, short column){
    short cursorPosition = column - 1;
    char scanCode = 0;
    char buffer[20];
    char* line = buffer;
	
    while(scanCode != 0x001c){
       scanCode = getKey();
	    char ascii = handleScanCode(scanCode);
	    *line++ = ascii;
		
	    if(ascii != 0){
	        printChar(ascii, row, column);
	        column += 2;
	        out(0x03d4, 0x000e);
	        out(0x03d5, row);
	        out(0x03d4, 0x000f);
	        out(0x03d5, cursorPosition++);
       }
    }
	
    return line;
}

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 8:00 am
by Gigasoft
And what do you suppose happens to "buffer" when the function returns, and another function is called? Don't you think you should be proficient in C before trying to write an OS in C?

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 8:21 am
by Benjamin1996
Gigasoft wrote:And what do you suppose happens to "buffer" when the function returns, and another function is called? Don't you think you should be proficient in C before trying to write an OS in C?
What exactly do you mean? That "buffer" gets redeclared or?

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 8:30 am
by Lithorien
Benjamin1996 wrote:
Gigasoft wrote:And what do you suppose happens to "buffer" when the function returns, and another function is called? Don't you think you should be proficient in C before trying to write an OS in C?
What exactly do you mean? That "buffer" gets redeclared or?
Look up C and C++ scoping rules.

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 8:51 am
by Combuster
is just a switch statement switching between the value of "scanCode" and returns the corresponding ASCII
eww...

You know that that is about the most ugly approach you can have? How much programming experience do you actually have?

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 9:19 am
by Benjamin1996
Combuster wrote:
is just a switch statement switching between the value of "scanCode" and returns the corresponding ASCII
eww...

You know that that is about the most ugly approach you can have? How much programming experience do you actually have?
I have been programing for about 3 years in Java as the primary thing, and I know that I could use the scan code as an index in an array, and I probably will. My current handleScanCode function, is just something I threw together real quick, to test it..

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 9:30 am
by Benjamin1996
Lithorien wrote:
Benjamin1996 wrote:
Gigasoft wrote:And what do you suppose happens to "buffer" when the function returns, and another function is called? Don't you think you should be proficient in C before trying to write an OS in C?
What exactly do you mean? That "buffer" gets redeclared or?
Look up C and C++ scoping rules.
Ahh, I know what he means now, sorry. I just didn't get his point. Of course I know that a variables scope is only inside the brackets it resides in. So just to be sure: You're saying that once I return my variable and calls the "print" function "buffer" is out of scope?

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 12:41 pm
by Gigasoft
Actually, this has nothing to do with scope, but with lifetime. Scope applies to names in the source code, while lifetime applies to the variables themselves.

"buffer" is a local variable, meaning that it is associated with a specific invocation of a function and only valid during that invocation. In Java, local variables may only be accessed from the function invocation they belong to because pointers to variables do not exist in Java, but in C, the address of a variable can be taken and passed to another function, which is what you're doing. When returning, the stack frame containing the variable "buffer" is unreserved, and the memory is used again by the function "print".

Re: Defect getLine(short row, short column) function!

Posted: Sun Apr 25, 2010 6:36 pm
by Lithorien
Gigasoft wrote:Actually, this has nothing to do with scope, but with lifetime. Scope applies to names in the source code, while lifetime applies to the variables themselves.

"buffer" is a local variable, meaning that it is associated with a specific invocation of a function and only valid during that invocation. In Java, local variables may only be accessed from the function invocation they belong to because pointers to variables do not exist in Java, but in C, the address of a variable can be taken and passed to another function, which is what you're doing. When returning, the stack frame containing the variable "buffer" is unreserved, and the memory is used again by the function "print".
Thanks for the terminology correction - lifetime is, in fact, what I was refering to. Not scope.

Thanks Gigasoft. :)