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