Page 1 of 2

String Writing Troubles

Posted: Sun Aug 11, 2002 11:56 am
by srg
[attachment deleted by admin]

Re:String Writing Troubles

Posted: Sun Aug 11, 2002 12:29 pm
by Slasher
I just browed over the code
1. in set_cursor_ position its supposed to be
position = (row * width*2) + col*2
2. Rewrite your fill screen function and just pass it the character to fill the screen with and the attribute to use.
try not to be doing complicated manipulations, just makes debugging harder and it take 4,000 character (80*25*2) to fill the screen

Re:String Writing Troubles

Posted: Sun Aug 11, 2002 2:52 pm
by crazybuddha
I would like to help, but I don't understand the question. Could you rephrase it?

Re:String Writing Troubles

Posted: Sun Aug 11, 2002 4:44 pm
by srg
My Appologies

Basically, I am mystified as to why this code is not working, I have tried and tried to get it to work and I need some help.

The first reply to my post said that I should have used position = (row * width*2) + col*2
rather than
position = (row * width) + col

Not trying to say anything about his abilities but would you say that this is better? If this is the reason why it is not working then what is the equasion to go the other way round for my Get_Cursor_Pos procedure

Thanks
Steven Graham

Re:String Writing Troubles

Posted: Sun Aug 11, 2002 5:06 pm
by crazybuddha
If I were you, I'd create a simpler version and start to add features back in, one at a time. Write one character, then two, then a string (with/without LF/CR), then add the cursor. I'm pretty sure your troubles will become obvious. And if not, you will be able to say "It breaks RIGHT HERE, why is that?".

In fact, if I had the time to run your code, this is exactly how I would do it, rather than just reading through it, hoping to find the problem(s).

Regarding the positioning jazz, the point (I believe) is accounting for the fact that there are two bytes for each position on the screen - attribute, then character. So width is actually 160 bytes in 80x25. Height is still always just 25.

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 2:38 am
by srg
Right, I think I have found the routine with all the problems, it is my Print_Char routine

I am still mystified as to why it doesn't work (BTW I havn't changed the algorithm used yet, could this be the problem?)

Here it is:

; Print_Char Procedure ---------------------------------------------------------

; Parameters: Colour: ASCII Char: ah
; Page : al
; Returns: Nothing

; Descripiton: Prints a char on the screen according to parameters

; Regs used : ax, bx, cx, dx

Print_Char:
push bx
push cx
; First work out the address

mov cl, ah ; Store Char
mov bl, al ; Store Page

mov ax, 0x1000
mul bl ; Multiply Page by 0x1000
mov bx, ax ; Sore for later use
call Get_Cursor_Pos ; Get Row an Col for the cursor
mov ax, 0xa0
mul dl ; Multiply row my 0xa0
add bx, ax ; Add that to the address
movzx ax, dh
shl ax, 1 ; Multiply col by 2
add bx, ax ; Add that to the address



cmp cl, 10 ; Check for Line Feed Char
Je .LF
cmp cl, 13
Je .CR ; Check for Carrage Return

mov byte [es:bx], cl ; Standard char, just Print it
inc dh
call Set_Cursor_Pos ; Move Cursor one Place to the right
Jmp .Done

.LF inc dl
call Set_Cursor_Pos ; Line Feed (Move Cursor one Place down)
Jmp .Done

.CR mov dh, 0 ; Carrage Return (Move Cursor to
call Set_Cursor_Pos ; the start of the line )



.Done pop cx ; restore prior value into cx
pop bx
ret

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 7:41 am
by crazybuddha
I only have a sec, but . . .

Get rid of the page selection. Stick with page 0 for now.
What's in DX??
Recheck the math in your row/col calculation. Remember, there are two bytes for each character spot on the screen ( I know you know this already). If you want to be in the middle of the second row, how would you get there?

doh, gotta go.

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 10:06 am
by srg
Slight update

This routine is ok when run once, but if run in a loop, it goes haywire!!!!

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 11:04 am
by srg
Checked the math of the row/col and it's fine!

chars are on even bytes in display memory
attibs are on the odd bytes in display memory

Now all of a sudden, it seems that my cursor positioning routines have problems, they were fine before! :(

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 12:24 pm
by crazybuddha
I suggest you steal my register printing code from this earlier thread and check your values. I think I even put a macro at the top which makes it a little nicer to use.


http://www.mega-tokyo.com/forum/index.p ... eadid=1089

I briefly tried to get your code to work. I still stand by my original suggestion . . . to start from the beginning and add incrementally.

I have an observation for what it's worth. Use registers to pass in values to routines, but don't expect them to retain their state throughout the code. And if it seems best to do so, be sure to comment it. It makes the code hard to follow and introduces lots of trouble spots. My 'spidey sense' tells me that either this is the problem or you aren't getting values from the ports that you expect. Use the register printing to check.

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 1:09 pm
by srg
BTW How long have you been writing programs in assembly and how long does it , in your oppinion, take to become a competant asm coder (talking about x86 at the moment).

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 2:08 pm
by crazybuddha
Let's just say a long time.

There's actually little practical use for assembly anymore, so the bar has lowered considerably. Nowadays, if you understand most of the instructions, the pecularities of protected mode, and a few of the conventions, then you're competent.

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 3:10 pm
by srg
IC, a shame

Am I wasting my time writing most of my programs purely in assembly language. Eventually, I want to write an os, should I write the bulk of it in C.

The main reason I wanted to write this and other programs in assembly is purely speed and size. Should it be better to write mainly in C and then use inline asm for speeding up the code?

Doing calculations like the ones I'm wrestling with in this program would be made much easier as I could write them as an equasion. Heh Mathematics is one of my weak subjects.

Re:String Writing Troubles

Posted: Mon Aug 12, 2002 8:02 pm
by crazybuddha
The only reason to write everything in assembly is because you want to. Writing the 'bulk' in C is certainly the smarter choice.

As has been often said, "Make it work, then make it work fast."

Naturally, many parts of an OS really do need to be written in assembly, so your time is certainly not wasted. Besides, assembly makes you a better programmer. Or more macho at any rate.

Re:String Writing Troubles

Posted: Tue Aug 13, 2002 2:17 am
by srg
IC

I'm using Windows and according to all the FAQs DJGPP is the best compiler to use. I genrally like to use Borland stuff, couldn't I use Borland's C/C++ compiler to this sort of thing.

Also, when rewriting libc, surely a lot of this stuff should be in the kernal anyways, shouldn't it?