Can't clear screen. it clears half of the screen

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
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Can't clear screen. it clears half of the screen

Post by dc740 »

Hi, I'm following this tutorial: http://www.osdever.net/bkerndev/
I'm doing it completely in 32 bit assembler (cause I really like it :)). But I can't manage to clear the entire screen.
This is the code, and the result:

This is how it's done in C (I didn't copy the entire function)

Code: Select all

    /* Sets the entire screen to spaces in our current
    *  color */
    for(i = 0; i < 25; i++)
        memsetw (textmemptr + i * 80, blank, 80);
And this is my code in assembler:

Code: Select all

global cls ;clear screen
cls:
        xor eax,eax
        mov ah, [text_attributes]
        mov al,'A'         ;0x20 | I replaced the BLANK char, to verify where it's writing on the screen

        ;now we have to go from row 0 to 24 in memory to blank the screen
        ;Index = [(y * width) + x]
        ;ecx will be Y and X will be vga_memory (0xB8000). width is 80
        mov ecx,24
        push eax ;remember that eax has the 'A' char with the attributes (foreground color... etc)
        .fill_memory:
                mov eax,ecx ;move to the next row
                mov ebx,80
                mul ebx
                add eax,[video_memory] ;now eax =  [(ecx * 80) + video_memmory]

                mov edi, eax ;this is the memory location were it should write
                pop eax      ;this has the blank character!
                push ecx     ;keep it safe!!! you don't want to mess with the loop
                mov ecx, 80  ;this is the length of each row
                rep stosw      ;write 80 'A' in memory, then 
                pop ecx         ;restore ecx
                push eax;      ;save the 'A' character
        loop .fill_memory   ; decrease ecx and continue with the next row
        pop eax ;clean the stack ;)
ret

The result? Bochs says:
, BSS=0x3000Sarting up... AAAAAAAAAAAAAAAAAA... and writes 13 rows of 'A' instead of 25


Any suggestions?


Thanks for reading this far!
User avatar
f2
Member
Member
Posts: 311
Joined: Mon Jun 15, 2009 10:01 am
Location: France

Re: Can't clear screen. it clears half of the screen

Post by f2 »

Hi dc740,

You can do much faster than that:

Code: Select all

	mov   ecx, (80 * 25)
	mov	edi, [video_memory]
	mov	al, 'A'
	mov	ah, [text_attributes]
	rep	stosw
It's faster and it works well!
"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Can't clear screen. it clears half of the screen

Post by Creature »

I'm guessing that you're using some kind of memset function to clear the screen. Remember that the standard console screen has 80x25 cells, which are all 2 bytes long, not 1 byte. This makes every cell 1 word (that's why Bran probably uses memsetw, which stands for copying word per word instead of a byte per byte). Every console cell has 2 bytes, the first is the actual character (1 byte) and the second one is the colour value (1 byte). The screen is 80x25 = 2000 cells long and thus 80x25x2 = 4000 bytes long.

This is usually the problem if only half of the screen is cleared, I hope it's of any help.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Can't clear screen. it clears half of the screen

Post by gravaera »

I've only just finished going through some tutorials on ASM, and although this may not help much, I do feel some small need to say it: I was able to read through that code and understand every line. Maybe not as a compound incremental thing, so that I understood the whole bit of code as a whole, but I could understand the statements individually.

That's a start :)

You might want to try Tommy's code up there, or just use plain C coded method for now. When you get better, you'll look through that code, and be like: "Well...wasn't I a n00b..."

-All the best
gravaera
Last edited by gravaera on Sat May 26, 2012 5:05 pm, edited 1 time in total.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Can't clear screen. it clears half of the screen

Post by dc740 »

you guys rock!!!! =D>

I love this forum! on my first day I received a lot of replies and you fixed my problem on just a few hours!

As you said, you could do it even faster. I was so focused on converting from C to asm that I forgot the logic behind it! I just cleared the entire screen without problems. Don't know where is the problem with the old code. I guess I should look into it to learn from my mistakes. but anyway I was able to do it thanks to Tommy's code.

Thank you all. you have a great community here! :)
Post Reply