reading text files

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
vishal
Posts: 17
Joined: Sun Nov 11, 2007 3:30 pm

reading text files

Post by vishal »

for reading text files on the disk and displying them in the screen
should i directly read it from the floppy disk (i.e locate its cluster in the FAT table and put a di pointer to the text file's clusters and read the text file and disply it on the screen)
or do you guys sugggest something else??
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

That won't work. When you put data in the textmode VGA screen buffer at memory address 0xB8000, every other byte needs to be something called an "attribute" byte.
If you just read a text file into that memory area, it will only display every other byte as a character. The other bytes will be interpreted as screen colors.

If you want to read a text file and display it, you need to read it into a buffer that you allocate. Then you can copy that buffer, one character at a time, to 0xb8000 -- adding the extra attribute byte for every character that you copy.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
bewing wrote:If you want to read a text file and display it, you need to read it into a buffer that you allocate. Then you can copy that buffer, one character at a time, to 0xb8000 -- adding the extra attribute byte for every character that you copy.
There's also control characters (tab, newline, etc) and scrolling to worry about - a simple "lodsb; stosw" loop won't work.

Lately I've been using pre-scanning for this, to improve performance. The idea is you check the text in the buffer and find out how many lines you need to scroll, then scroll once, then draw the text last. For example (with 80 * 25 text mode), this means if there's 20 lines of text in the buffer you scroll once (instead of 20 times), and if there's 200 lines of text in the buffer you skip 175 lines of text completely, clear the buffer, and then draw 25 lines of text (with no scrolling at all).
vishal wrote:for reading text files on the disk and displying them in the screen
should i directly read it from the floppy disk (i.e locate its cluster in the FAT table and put a di pointer to the text file's clusters and read the text file and disply it on the screen)
or do you guys sugggest something else??
Definately not. Your code should be split into at least 4 logical pieces - a disk driver, the file system code, the code to copy data from a file to the virtual screen, and a video driver.

Something about the way you wrote the question makes me think you're writing a bootable application instead of an OS.... ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
vishal
Posts: 17
Joined: Sun Nov 11, 2007 3:30 pm

Post by vishal »

thanks for the replies



for running executables file i just copy them into memory and pass the control there and later return the control to the kernel

so for allocating buffer for text file can i copy it into memory(RAM) and then read the file from there

for real mode :
use ah=0eh/int10h for every character in the memory
for protected mode:
copy each character in the screen memory and setting the attribute character along with every character to normal while displaying the last set of lines of file that will be displayed on the screen (after scrolling)

right i am coding this in assembly language :cry:

@ brendan
you can really say that because i am right now at the very basic level operating system development.....
vishal
Posts: 17
Joined: Sun Nov 11, 2007 3:30 pm

Post by vishal »

i am sorry but i am a little confused over this

allocating a buffer refers to :
1) variable/array in program (code)
2) copying it to the memory (like for any executable)
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

If you are still needing to worry about the memory addressing of video addressing, I recommend to not worry about displaying a text file on screen right now. The basic idea is abstracting the memory layout behind an interface. (i.e., device drivers.) This is just my opinion, though.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
vishal
Posts: 17
Joined: Sun Nov 11, 2007 3:30 pm

Post by vishal »

there is no confusion from my side about the video addressing ...i have been reading stuff about it...all i want to know to that if i directly read from the file on the disk character by character and store it in al and display it by int 10h or write to video memory is enough??
is this the best way to do it or anything else is required???
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

When you take a byte from memory, if your in real mode you can test for 0 and if not 0, you can pass it to int 10h function 0eh, you should also test for tabs (09h), as this function does not deal with them.
Then that will work fine.
But if your do this from Pmode, you will need to write your own function, which will need to test for command char eg:back space (08h), bell (07h), carriage return (0dh) and line feed(0ah) etc. also you need to test for extended ASCII char's eg: 80h-ffh.
On top of this you need to add attribute byte, plus scroll the screen.
But if you have more line in the file than your screen res, you need to wait for a keypress to scroll it, or people will not be able to read it, before it scrolls up the screen.
If you need example code here it is
Attachments
text.asm
(3.18 KiB) Downloaded 72 times
Post Reply