Page 1 of 1
Need some help with ASM text driver
Posted: Mon Feb 07, 2011 7:07 pm
by phillid
Hello, again. I know this is an OS dev forum, and what I'm asking isn't entirely OS - related, but I need some help writing a text driver in ASM. I know about the memory addresses and all of the rest, but I need a simple, example driver for any type of monitor - color or B&W. It doesn't have to scroll the text or anything, I just need an example loop that I can adapt to my requirements.
Thanks a lot for any guidance.
phillid
Re: Need some help with ASM text driver
Posted: Mon Feb 07, 2011 7:17 pm
by Tosi
Do you know a high level language, such as C(++)?
Try writing text mode print routines in C or some other high level language first, and converting that to assembly. Then optimize what you came up with.
But if you don't know how to write something like that in assembly, I would recommend just sticking to a HLL and using assembly only when necessary.
Re: Need some help with ASM text driver
Posted: Mon Feb 07, 2011 8:11 pm
by NickJohnson
Are you using VGA text mode (which is the default when the computer boots), or are you talking about a full-fledged graphics driver with custom fonts? Either way, you can probably find a lot of information on the wiki, either under VGA Hardware or Text mode. Writing the code for the former should be trivial even in assembly.
Re: Need some help with ASM text driver
Posted: Mon Feb 07, 2011 8:19 pm
by Brendan
Hi,
phillid wrote:Hello, again. I know this is an OS dev forum, and what I'm asking isn't entirely OS - related, but I need some help writing a text driver in ASM. I know about the memory addresses and all of the rest, but I need a simple, example driver for any type of monitor - color or B&W. It doesn't have to scroll the text or anything, I just need an example loop that I can adapt to my requirements.
You mean, something like this:
Code: Select all
;Input
; esi = address of zero terminated UTF32 string describing what should be displayed
displayLog:
pushad
xor edi,edi ;edi = screen address = "unknown"
cld
mov ebx,[cursorX] ;ebx = cursorX
mov ecx,[cursorY] ;ecx = cursorY
.nextChar:
lodsd ;eax = next unicode character
test eax,eax ;Has the end of the string been reached?
je .done ; yes
cmp eax,NEWLINE_CHAR ;Is it newline?
je .handleNewLineChar ; yes
cmp eax,TAB_CHAR ;Is it tab?
je .handleTabChar ; yes
cmp eax,SPACE_CHAR ;Is it space?
je .handleSpaceChar ; yes
cmp ebx,[screenWidth] ;Is the cursor past the right edge of the screen?
jb .doneLineWrap ; no
xor edi,edi ; yes, edi = screen address = "unknown"
inc ecx ; Increase cursorY
xor ebx,ebx ; cursorX = 0
.doneLineWrap:
cmp ecx,[screenHeight] ;Is the cursor past the bottom edge of the screen?
jb .doneScroll ; no
;**** Add scrolling code here ****
mov ecx,[screenHeight] ;ecx = screenHeight
dec ecx ;ecx = new cursorY = last line of screen
jmp .calcScreenAddress
.doneScroll:
test edi,edi ;Is the current screen address known?
jne .gotScreenAddress ; yes
; no, need to calculate new screen address
.calcScreenAddress:
push eax
mov eax,[screenWidth] ;eax = screenWidth
mul ecx ;edx:eax = cursorY * screenWidth
mov edi,[screenAddress] ;edi = address of display memory
add eax,ebx ;eax = cursorY * screenWidth + cursorX
lea edi,[edi+eax*2] ;edi = address of display memory + 2 * (cursorY * screenWidth + cursorX)
pop eax
.gotScreenAddress:
call convertUnicodeToExtASCII ;Convert the unicode character to an "extended ASCII" character
mov ah,[currentAttribute] ;ah = current attribute
stosw ;Put the character on the screen, and increase the screen address
inc ebx ;Increase cursorX
jmp .nextChar
.handleNewLineChar:
xor edi,edi ;edi = screen address = "unknown"
inc ecx ;Increase cursorY
xor ebx,ebx ;cursorX = 0
jmp .nextChar
.handleSpaceChar:
inc ebx ;Increase cursorX
test edi,edi ;Is screen address known?
je .nextChar ; no, leave it as unknown
add edi,2 ; yes, update it
jmp .nextChar
.handleTabChar:
xor edi,edi ;edi = screen address = "unknown"
add ebx,4 ;ebx = cursorX + 4
and bl,0xFC ;ebx = (cursorX + 4) % 4 = next tab position
jmp .nextChar
.done:
mov [cursorX],ebx ;Set new cursorX
mov [cursorY],ecx ;Set new cursorY
popad
ret
Cheers,
Brendan
Re: Need some help with ASM text driver
Posted: Mon Feb 07, 2011 10:05 pm
by phillid
Yes! Thank you, brendan! I will look through the code and learn exactly what each instruction does tonight.
Thanks again
phillid