once more, getline

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
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

once more, getline

Post 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 :lol:
return (*linak);
}
I can write here my puts and putch functions
Attachments
MS VPC07 FortOS screen with error
MS VPC07 FortOS screen with error
mistake.PNG (19.99 KiB) Viewed 2087 times
Sorry for my bad English...
INF1n1t
Member
Member
Posts: 60
Joined: Fri Dec 22, 2006 5:32 pm
Location: Somewhere Down...

Post 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.
I think, I have problems with Bochs. The biggest one: Bochs hates me!
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

putch, puts

Post 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);
}
}
Sorry for my bad English...
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:

Post 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.
"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 ]
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

...

Post by Cmaranec »

If i terminate the string with '\0', it writes the same.

btw. i am corrected "char getline()" to "unsigned char getline()"
Sorry for my bad English...
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: ...

Post by Alboin »

Cmaranec wrote:btw. i am corrected "char getline()" to "unsigned char getline()"
No no. To "char *getline()".
C8H10N4O2 | #446691 | Trust the nodes.
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:

Post 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).
"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 ]
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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).
:oops: Oh...I was thinking he was using malloc or something. Sorry...
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post 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?
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

compiler

Post by Cmaranec »

I am using DJGPP - GCC compiler and NASM for compiling ASM parts.
Sorry for my bad English...
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post 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. ;)
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
Post Reply