Page 1 of 1
once more, getline
Posted: Tue Apr 24, 2007 12:06 pm
by Cmaranec
Hello, i am programming PM OS FortOS in C and ASM. I am creating function getline(); and it doesn't work fine. I don't know, if i have bad getch();, getline();, puts(unsigned char *text); or putch(unsigned char ch); function... Can you help me please?
my getch() function:
char getch()
{
unsigned char in;
unsigned char oin;
outportb(0x60,0xf4);
outportb(0x64,0x60);
outportb(0x64,0x20);
oin=inportb(0x60);
do {
in=inportb(0x60);
} while(in == oin);
return kbdus[in];
}
my getline() function:
char getline()
{
char linak[128];
unsigned char cteno;
int citac = 0;
labb:
cteno = getch();
if(cteno == '\n') goto konec;
linak[citac] = cteno;
putch(cteno);
citac++;
goto labb;
konec:
puts(linak); // this is writing an idiocy
return (*linak);
}
I can write here my puts and putch functions
Posted: Tue Apr 24, 2007 2:56 pm
by INF1n1t
Before one week, I was on to this (a keyboard driver). Well, it's not finished yet, but I can give you several tips:
1. It is not just about reading characters through ports. Do not think, that 0x30 would be character '0'. Do you know anything about scan codes? Your keyboard works with scan codes (the codes it sends to the port 0x60 are scan codes). You must write your own routine to translate these scan codes, to ASCII characters.
2. It is clear that the video memory (80x25 I'm talking about) takes two bytes for a character. The first byte is the ASCII code of the character and the next byte is the attribute (color of the character for example). You didn't post your putch procedure, so I'm not sure, whether you know about the attribute bytes.
putch, puts
Posted: Wed Apr 25, 2007 1:26 pm
by Cmaranec
here are my putch and puts functions:
void putch(unsigned char c)
{
unsigned short *where;
unsigned att = attrib << 8;
if(c == 0x08)
{
if(csr_x != 0) csr_x--;
}
else if(c == 0x09)
{
csr_x = (csr_x + 8 ) & ~(8 - 1);
}
else if(c == '\r')
{
csr_x = 0;
}
else if(c == '\n')
{
csr_x = 0;
csr_y++;
}
else if(c >= ' ')
{
where = textmemptr + (csr_y * 80 + csr_x);
*where = c | att;
csr_x++;
}
if(csr_x >= 80)
{
csr_x = 0;
csr_y++;
}
scroll();
move_csr();
}
void puts(unsigned char *text)
{
int i;
for (i = 0; i < strlen(text); i++)
{
putch(text);
}
}
Posted: Wed Apr 25, 2007 1:50 pm
by Combuster
From the list of most notorious errors in C code:
- Not null terminating a string
yup, you forgot to add a '\0' to the end of the string before giving it to puts.
Regarding the rest:
- char getline() {...}
getline should hand over a string. Not a character, no?
- goto labb;
Yuck. goto's are evil.
- One True Bracing Style
I think the people here can agree that NOT identing is potentially the worst bracing style. we have the
Code: Select all
tag for that, not [quote]
[i]- linak[citac] = cteno; [/i]
You mind posting code in english? That would save us from having to reverse-engineer variable names.
...
Posted: Wed Apr 25, 2007 2:37 pm
by Cmaranec
If i terminate the string with '\0', it writes the same.
btw. i am corrected "char getline()" to "unsigned char getline()"
Re: ...
Posted: Wed Apr 25, 2007 2:52 pm
by Alboin
Cmaranec wrote:btw. i am corrected "char getline()" to "unsigned char getline()"
No no. To "char *getline()".
Posted: Wed Apr 25, 2007 2:55 pm
by Combuster
no, even void getline(char * p)
otherwise you end up returning an array that goes out of scope the moment you return it (another common error).
Posted: Wed Apr 25, 2007 3:11 pm
by Alboin
Combuster wrote:no, even void getline(char * p)
otherwise you end up returning an array that goes out of scope the moment you return it (another common error).
Oh...I was thinking he was using malloc or something. Sorry...
Posted: Wed Apr 25, 2007 5:30 pm
by B.E
Well first fix up your getLine to use a while loop (and break statments). Secondly, it look to me that your writting a stack pointer to the screen. What compiler are you using?
compiler
Posted: Fri Apr 27, 2007 7:09 am
by Cmaranec
I am using DJGPP - GCC compiler and NASM for compiling ASM parts.
Posted: Fri Apr 27, 2007 9:26 am
by inflater
I am using DJGPP - GCC compiler and NASM for compiling ASM parts.
To be more specific - you are using code from Bran's Kernel Development Tutorial.
linak[citac] = cteno;
Translation: Liner[Counter] = The readed;
Sorry guys as I can't help with this...
If your OS is in text mode for now (well I think it is
), you can try to find some disassembled BIOS on the net and copy the 16-bit code from there. It shouldn't do any serious problems as you aren't using BIOS interrupts, but the copied code.