Need some help with ASM text driver

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
phillid
Member
Member
Posts: 58
Joined: Mon Jan 31, 2011 6:07 pm

Need some help with ASM text driver

Post 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
Last edited by phillid on Mon Aug 25, 2014 2:59 am, edited 1 time in total.
phillid - Newbie-ish operating system developer with a toy OS on the main burner
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Need some help with ASM text driver

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Need some help with ASM text driver

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Need some help with ASM text driver

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
phillid
Member
Member
Posts: 58
Joined: Mon Jan 31, 2011 6:07 pm

Re: Need some help with ASM text driver

Post by phillid »

Yes! Thank you, brendan! I will look through the code and learn exactly what each instruction does tonight.

Thanks again

phillid
phillid - Newbie-ish operating system developer with a toy OS on the main burner
Post Reply