Page 2 of 2

Re:Keyboard ISR design

Posted: Fri Oct 08, 2004 6:56 am
by bubach
So is DIV what i want to do? :)
I can?t see how i could get x and y values by DIV?
If i divide with 80 (to get the row) for example, it would only work with "even" numbers, like 2000, 1920 and so on...

besides, i really suck at math... 8)
basicly i need someone to do it for me.. (have i missunderstood the purpose of this forum?) ;)

Re:Keyboard ISR design

Posted: Fri Oct 08, 2004 7:06 am
by Solar
bubach wrote: If i divide with 80 (to get the row) for example, it would only work with "even" numbers, like 2000, 1920 and so on...
Uh. Ouch. Excuse me for being blunt, but you should spend some more time in user space before daring the kernel space - gathering experience.

Do you know what an integer division is?
basicly i need someone to do it for me.. (have i missunderstood the purpose of this forum?) ;)
A bit, yes. Basically we're here to help when someone gets seriously stuck, or discussing the pro's and con's of a certain design. But in the end, you are expected to write your own OS (just like most of the others here).

:) :) :) :) :) <- no offense intended!

Re:Keyboard ISR design

Posted: Fri Oct 08, 2004 7:27 am
by bubach
Do you know what an integer division is?
lol.... what to say... ...forgot about that ;D

hm.. if the cursor is at pos. 110 and i div it, it would give me 1.. then i need to add 1 in order to get the actuall row....
hmm... then i would have to find a way of getting the col.... value-(result of div*80) = col?.... wich would problably be as bad as my old code?

Re:Keyboard ISR design

Posted: Fri Oct 08, 2004 7:33 am
by distantvoices
Hej, Bubach, what about thinking for yourself first, hm?

sucking at maths is not an excuse for ... such abysmal subterfuges. *mmpf*

either use separate storage for x and y and be content with it or do it this DIV way you want to do so deliberately, but then, don't you dare to come here whining about your sooo bad maths.

@solar: ow ow ow, spreading the whips, are you? hehehe. What about the nine tailed whip out of the deepest realms from devils den? He is soooo longingly asking for them. ];-> I'm not so sure at a quick glance if the div solutions would satisfy him - well, but at least, the div should give him an idea of *how many full rows we have* if i am not completely mistaken:

say screen xres = 80 yres = 25
in total this makes (80*25) 2000 bytes if i am not mistaken.

now, say we are at position 1457.

1457/80=18.2125 ---> we 've got 18 full lines.
80*0.2125=17 --->we 've got 17 chars in the 19'th line.
or: 1457-(80*18)=17

hope that clears at least the principle of what we wanna do - without any loops. And by the way, this is also feasible in asm, but I wont code this.

Re:Keyboard ISR design

Posted: Fri Oct 08, 2004 7:37 am
by bubach
sorry.. have to blame something/someone.. ;)
thanks for all the help..

Re:Keyboard ISR design

Posted: Fri Oct 08, 2004 8:20 am
by Brendan
Hi,
bubach wrote: hm.. if the cursor is at pos. 110 and i div it, it would give me 1.. then i need to add 1 in order to get the actuall row....
hmm... then i would have to find a way of getting the col.... value-(result of div*80) = col?.... wich would problably be as bad as my old code?
Actually no. The 80x86 32 bit integer divide instruction divides edx:eax by something (a value in a register or memory), and returns the result in eax and the remainder in edx. For example, if edx:eax = 12345 and it's divided by 80, then after the division eax would be 154 and edx would be 25. Therefore:

Code: Select all

   section .data
const80:   dd 80
   section .text

convertOffsetToXY:
   push edx
   push eax
   xor edx,edx
   div dword [const80]
   mov [cursorY],eax
   mov [cursorX],edx
   pop eax
   pop edx
   ret
Also X and Y (or column and row) normally start at 0...

While I'm here perhaps I should comment on this:
So how should i do it? I don?t want to "cheat", by having the x and y values in vars..
Basically, if you did keep the X and Y values in variables you wouldn't need to do division to convert the screen offset into the cursor position, and you wouldn't have to do multiplication to convert the X and Y values into the screen offset. Also you wouldn't have to read the cursor's screen offset from the video card using IO ports (you'd only have to update the cursor's X and/or Y position when they need updating).

Now the funny thing is that multiplication, division and reading/writing to IO ports are all slow, and usually it's considered good programming practice to find solutions that avoid things that are slow (it's not "cheating") :).

How about something like this (NASM, assuming 80 characters per row and neglecting scrolling):

Code: Select all

   section .data
cursorX:   dd 0
cursorY:   dd 0
cursorOffset:   dd 0
   section .code

outputString:
   push eax
   push edi
   push esi
   mov ah,attributes??
   mov edi,[cursorOffset]
   cld
.nextChar:
   lodsb
   test al,al
   je .done
   cmp al,0x0a
   je .newLine
   stosw
   inc dword [cursorX]
   cmp dword [cursorX],80
   jb .nextChar
.newLine:
   inc dword [cursorY]
   mov dword [cursorX],0
   call updateOffset
   mov edi,[cursorOffset]
.done:
   call updateCursor
   pop esi
   pop edi
   pop eax
   ret

outputChar:
   push eax
   push edi
   cmp al,0x0a
   je .newLine
   mov ah,attributes??
   cld
   mov edi,[cursorOffset]
   stosw
   inc dword [cursorX]
   mov [cursorOffset],edi
   cmp dword [cursorX],80
   jb .update
.newLine:
   inc dword [cursorY]
   mov dword [cursorX],0
   call updateOffset
.update:
   call updateCursor
   pop edi
   pop eax
   ret

updateOffset:
   push eax
   mov eax,[cursorY]
   lea eax,[eax*4+eax]
   shl eax,4
   add eax,[cursorX]
   add eax,eax
   mov [cursorOffset],eax
   pop eax
   ret

updateCursor:
   ;Update the video cards cursor position with OUT instructions...
   ret


Cheers,

Brendan