flat assembler
Message board for the users of flat assembler.

Index > DOS > Write colored strings into console =D

Author
Thread Post new topic Reply to topic
saigon



Joined: 29 May 2006
Posts: 62
saigon 07 Jun 2006, 11:58
Hello!

After some hard work (atleast for me Wink ) I made it. A macro which enables the simplicity to write colored strings with ease.

Here's the source code: (I tried to comment it as good as possible)
Code:
; WriteStringEx macro (made by saigon)
macro WriteStringEx String, Color {
 mov si, String ; Move string to ds:si
 xor bh, bh     ; Reset bh register (page)
 mov bl, Color  ; Set text color
 mov cx, 1      ; Write 1 copy of each character
WriteChar:
 lodsb          ; Load a byte from ds:si to al
 or al, al      ; Or al
 jz WriteDone   ; Jump away if writing is done
 mov ah, 9      ;
 int 10h        ; Write a colored character
 mov ah, 3      ;
 int 10h        ; Get the cursor position
 xor ch, ch     ; Reset starting line
 mov cl, ch     ; Set ending line to starting line
 inc cl         ; Increment ending line
 mov ah, 2      ;
 inc dl         ; Increment cursor's x position
 int 10h        ; Reposition cursor
 jmp WriteChar  ; Repeat to write the next character
WriteDone:
 ret            ; We are done, let's head back
}

; Console header
org 100h

; Colors: 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, 7=White,
;         8=Gray, 9=Light Blue, 10=Light Green, 11=Light Cyan, 12=Light Red (Pink),
;         13=Light Magenta, 14=Light Yellow, 15=Bright White.

; Main code
WriteStringEx Message, 14
Message db "FASM rules, doesn't it?",0

; Please give credit if a portion or the whole source is used.
; I am just a beginner with a little experience in assembly language.
; Thank you! - saigon    


It may have bugs or it may need some optimizing, but atleast it works Very Happy
I hope this makes use for people here in this forum, so please go ahead and post your comments.

Thanks!

EDIT: I just noticed, that this thread is my 30th post and the 2000th of the DOS forum Very Happy
Post 07 Jun 2006, 11:58
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 07 Jun 2006, 12:50
There was also this thread about similar topic: http://board.flatassembler.net/topic.php?t=5075
Post 07 Jun 2006, 12:50
View user's profile Send private message Visit poster's website Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 07 Jun 2006, 14:03
Hi Tomasz!
I think I missed that topic, but as for a beginner like me in assembly, it's quite impressive that I did the code as it's advanced stuff to use DOS interrupts etc. Also, I think I'm the first member in this forum who wrote code from scratch to do this job, but I might be wrong (didn't find anything through forum search).

Anyway, I think this might help new users to understand the DOS interrupts more. I just hope I commented the code so that new users would understand what the program is doing.

Have a nice day!
Post 07 Jun 2006, 14:03
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 07 Jun 2006, 14:11
and as next lesson, you could rewrite it to procedure.

Because with macro, you will have this whole code assembled in source each time you use the macro.
Post 07 Jun 2006, 14:11
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 07 Jun 2006, 17:03
I know for what macros are. For example, this source:
Code:
macro dosomething {
 ; Do something
}

dosomething
dosomething
dosomething    


will be assembled as:
Code:
 ; Do something
 ; Do something
 ; Do something    


Last edited by saigon on 07 Jun 2006, 18:39; edited 1 time in total
Post 07 Jun 2006, 17:03
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 07 Jun 2006, 18:02
Maybe something like this:
Code:
call dosomething    

Code:
dosomething: ; Do somethingret    
Post 07 Jun 2006, 18:02
View user's profile Send private message Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 07 Jun 2006, 18:39
Oh yes, ofcourse. I was not at my main work computer, so I typed from my mind. I perhaps was playing with HLL, that's why I wrote 'proc'. Razz
Post 07 Jun 2006, 18:39
View user's profile Send private message Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie 07 Jun 2006, 22:52
Hi saigon

Just two comments from me:
1) you should make your labels as local so that you can repeat the macro without getting duplication errors. e.g. put "local WriteChar" and "local WriteDone" lines at the start.
2) you might as well preserve all registers since you are not interested in returning any. "pushad" at the start and "popad" at the end is one way to do that.

Regards

Chris
Post 07 Jun 2006, 22:52
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 07 Jun 2006, 23:39
It's always good to have several versions of a routine since none is (usually) best for all circumstances.

Having said that, though, please take a look at this (from Ralf Brown's interrupt list):

Code:
INT 10 - VIDEO - WRITE STRING (AT and later,EGA)
        AH = 13h
        AL = write mode
           bit 0: update cursor after writing
           bit 1: string contains alternating characters and attributes
           bits 2-7: reserved (0)
        BH = page number
        BL = attribute if string contains only characters
        CX = number of characters in string
        DH,DL = row,column at which to start writing
        ES:BP -> string to write
Return: nothing
Notes:  recognizes CR, LF, BS, and bell; for the ET4000 BIOS, scrolling,
          backspace, and CR only take place in the active page
        also available PC or XT with EGA or higher
        HP 95LX only supports write mode 00h
        IBM documents AL=10h,11h,20h,21h as "private" rather than "reserved"
        with PhysTechSoft's PTS ROM-DOS the AL,BH,BL,DH, and DL values are
          ignored on entry.
BUG:    on the IBM VGA Adapter, any scrolling which may occur is performed on
          the active page rather than the requested page
SeeAlso: AH=09h,AH=0Ah,AH=13h"DOS/V"
    
Post 07 Jun 2006, 23:39
View user's profile Send private message Visit poster's website Reply with quote
saigon



Joined: 29 May 2006
Posts: 62
saigon 08 Jun 2006, 08:10
@ChrisLeslie: As I said, my code might need optimizing, and what you're suggesting is exactly that. I am new to assembly, so please give me a lesson and tell me everything I need to have optimized sources.

Thanks!

@RugXulo: I saw the Ralph's list of interrupts, but what I didn't notice was that the interrupt didn't reposition the cursor, so I had to write my own routine.
Post 08 Jun 2006, 08:10
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 08 Jun 2006, 22:05
This ...

Code:
xor ch, ch     ; Reset starting line
 mov cl, ch     ; Set ending line to starting line
 inc cl         ; Increment ending line 
    


should probably be ...

Code:
mov cx,1
    


Razz
Post 08 Jun 2006, 22:05
View user's profile Send private message Visit poster's website Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie 09 Jun 2006, 08:36
This should be little improvement with some of the suggestions and put into a complete little program. Lots more could be done:
Code:
org 100h

macro WriteStringEx String, Color
{
 local WriteChar,newLine,WriteDone
 pushad
 mov si, String ; Move string to ds:si 
 xor bh, bh     ; Reset bh register (page) 
 mov bl, Color  ; Set text color
WriteChar: 
 lodsb          ; Load a byte from ds:si to al 
 or al, al      ; Or al
 jz WriteDone   ; Jump away if writing is done
 mov ah, 9
 mov cx,1
 int 10h        ; Write a colored character 
 mov ah, 3      ; 
 int 10h        ; Get the cursor position
 cmp dl,79
 je newLine
 mov ah, 2
 inc dl         ; Increment cursor's x position
 int 10h        ; Reposition cursor
 jmp WriteChar  ; Repeat to write the next character
newLine:
 mov ah,2
 mov dl,0
 inc dh
 int 21h
 jmp WriteChar
WriteDone:      ; We are done, let's head back
 popad
}

WriteStringEx str1,1
WriteStringEx str2,2

mov ax,4C00h
int 21h

str1 db "Hellooooooooooooooooooooooooooooooooooooooooo",0
str2 db " Wooooooooooooooooooooooooooooooooooooooooooooooooooorld",0          


Regards
Post 09 Jun 2006, 08:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.