Defect getLine(short row, short column) function!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Benjamin1996
Member
Member
Posts: 78
Joined: Sat Apr 10, 2010 7:00 am
Location: Denmark

Defect getLine(short row, short column) function!

Post 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! :)
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

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

Post 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.
Benjamin1996
Member
Member
Posts: 78
Joined: Sat Apr 10, 2010 7:00 am
Location: Denmark

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

Post 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;
}
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

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

Post 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?
Benjamin1996
Member
Member
Posts: 78
Joined: Sat Apr 10, 2010 7:00 am
Location: Denmark

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

Post 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?
User avatar
Lithorien
Member
Member
Posts: 59
Joined: Tue Oct 27, 2009 1:40 pm
Location: Hanover, PA

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

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Benjamin1996
Member
Member
Posts: 78
Joined: Sat Apr 10, 2010 7:00 am
Location: Denmark

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

Post 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..
Benjamin1996
Member
Member
Posts: 78
Joined: Sat Apr 10, 2010 7:00 am
Location: Denmark

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

Post 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?
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

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

Post 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".
User avatar
Lithorien
Member
Member
Posts: 59
Joined: Tue Oct 27, 2009 1:40 pm
Location: Hanover, PA

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

Post 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. :)
Post Reply