flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Problem with rendering text in text editor.

Author
Thread Post new topic Reply to topic
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 27 Mar 2014, 04:10
Hi,
Not really if this belongs to the "Main" forum or the "OS Construction Forum",
but this question belongs to the "My OS" category so I guess it's better to post it here.
I am in the process of writing a text editor for my 32-bit OS, I am taking some ideas from the MikeOS text editor, with some changes, got a really weird problem with the editor explained later in this post.
Basically the editor works like this:
-It has an internal variable called skip lines, which keeps a track of how much lines to skip (as I can't render the whole text file on a 90x60 screen).
-The render text function sees the number of lines to skip, and keeps ignoring the text, until ECX is 0, (note: ECX register is set to the number of lines to skip). It then calls display_loop which displays the actual text.
-The display_loop function will keep out putting the text until the end of screen (Screen Y = 58 ) has been reached or EOF and grabs input.
-There are some variable reference in the code
last_byte_addr - Last byte of the file (Load Address + File Size)
skip_lines - Lines to skip
cursor_byte - Pointer to the current user location in the file
cur_X - Cursor Location (X)
cur_Y - Cursor Location (Y)
Now the code Smile
Code:
render_text:
        ;; print text editor message blah, blah
        call setup_screen
        ;; display text from 0, 3
        mov dh, 3 ; X
        mov dl, 0 ; Y
        call os_move_cursor
        ;; ESI should point to start of file
        mov esi, LOAD_BUFFER                    
        ;; lines to skip
        mov dword ecx, [skip_lines]             
redraw:
        ;; 0 lines to skip??
        cmp ecx, 0                      
        ;; cool. display the text then
        je display_loop          
        ;; okay maybe not, decrement one line to skip
        dec ecx                                 
.skip_loop:
        ;; grab a char from ESI, increment esi, and put the char in AL
        lodsb                   
        ;; newline char?
        cmp al, NEWLINE
        ;; keep skipping characters if not
        jne .skip_loop                          
        ;; if we found a newline char jump back to redraw and check if 
        ;; we have any lines pending
        jmp redraw
display_loop:
        ;; ESI now points to the start of text to display
        lodsb                   
        ;; newline char?
        cmp al, NEWLINE
        jne skip_return
        ;; grab the cursor position
        call os_get_cursor_pos
        ;; set X to 0
        mov dl, 0       
        call os_move_cursor
skip_return:
        ;; get the cursor position
        call os_get_cursor_pos          
        ;; reached the maximum X position? (90)
        cmp dl, MAX_X
        je .no_print
        ;; print the character if not so
        call os_print_char
.no_print:
        mov dword ebx, [last_byte_addr]
        ;; EOF?
        cmp esi, ebx            
        ;; skip and get input
        je near get_input
        ;; Last Y co-ordinate?
        call os_get_cursor_pos          
        cmp dh, MAX_Y - 2
        ;; get input if so
        je get_input                    
        ;; or get back to display loop
        jmp display_loop
.....................
;; set user cursor position, validate input etc.
.....................
;; here's the go_down routine
go_down:
        pushad
        ;; ECX should point to pointer of the current point in the file (????????)
        mov dword ecx, [cursor_byte]
        ;; ESI should point to the place we loaded the file
        mov esi, LOAD_BUFFER
        ;; And now ESI points to the location of the current pointer of the...?????
        add esi, ecx                            
.loop:
        ;; check if the user is trying to scroll down after he/she has reached
        ;; EOF
        inc esi
        cmp dword esi, [last_byte_addr]
        ;; do nothing if so.
        je .do_nothing          
        ;; cool, get the real pointer back
        dec esi
        ;; into AL
        lodsb           
        ;; increment pointer
        inc ecx                         
        ;; newline??
        cmp al, NEWLINE
        ;; loop then
        jne .loop       
        ;; cool. set the pointer ..of... .. to ECX
        mov dword [cursor_byte], ecx
        ;; nowhere to go???
.nowhere_to_go:
        ;; pop all regs
        popad
        ;; EODB (End of Display Buffer)???
        cmp byte [cur_Y], MAX_Y - 3             
        ;; scroll the entire file down Smile
        je .scroll_file_down
        ;; else increment the cur_Y
        inc byte [cur_Y]                        
        ;; set cur_X to 0
        mov byte [cur_X], 0
        ;; and render text again
        jmp render_text
.scroll_file_down:
        ;; cool one more line to skip Smile
        inc dword [skip_lines]                  
        ;; set cur_X to 0
        mov byte [cur_X], 0
        ;; render text
        jmp render_text                         
.do_nothing:
        ;; else just exit :S
        popad
        jmp render_text
    

Now the screen output (this happens at exactly the same position for every
text file)
Image
And to make matters worse this is the output after scrolling down the text file.
Image
So yeah, the problem IMO is in the render_text function, but I am tired of finding out where???
Cheers,
sid

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 27 Mar 2014, 04:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 19726
Location: In your JS exploiting you and your system
revolution 27 Mar 2014, 04:58
You didn't post all the code so we can neither see all of what you are doing or even assemble it for testing. Maybe there is problem with os_move_cursor or some other function we can't see so we don't know.
Post 27 Mar 2014, 04:58
View user's profile Send private message Visit poster's website Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 339
Location: Asia, Singapore
sid123 27 Mar 2014, 07:32
Hi,
The os_move_cursor function performs a call to the kernel.
It looks like this:
Code:
os_set_cursor_pos:
        push ebx
        push eax
        ;; the kernel expects BL = X and BH = Y
        ;; added for convenience since people 
        ;; who come from a RM background tend
        ;; to use DL/DH
        mov bl, dl
        mov bh, dh
        mov ah, 0x19
        int 0x50
        pop eax
        pop ebx
        ret
os_move_cursor equ os_set_cursor_pos
    

If people want to test it....
Below is an attached tar file which contains a 32MB Disk Image formatted with FAT16, it contains the kernel binaries and the applications. I have put the sources of the editor in another directory called edit.
I have compressed the tar with gzip resulting in 225KB only Smile
Run for qemu like:
Code:
qemu -hda boot16.img 
    

Then in the shell type:
Code:
cd bin
./edit.znx linux.txt
    

If you want to quit press 'Q'. Right now it doesn't do much except scrolling through the text file.


Description: The binaries and sources for edit.
Download
Filename: bin-27032014-3-03.tar.gz
Filesize: 225.52 KB
Downloaded: 321 Time(s)


_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 27 Mar 2014, 07:32
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.