Create your own text control codes: Difference between revisions

8086 Assembly solution
(Created page with "{{draft task}} {{task|String manipulation}} A control code is a text character or sequence of characters with a special meaning that, rather than print a text character to th...")
 
(8086 Assembly solution)
Line 1:
{{draft task}}
{{task|StringText manipulationprocessing}}
 
A control code is a text character or sequence of characters with a special meaning that, rather than print a text character to the terminal or screen, instructs the computer to do something text-related. Examples include:
Line 28:
(This is a draft task for now because I don't know if anything other than assembly can do this.)
<br><br>
 
=={{header|8086 Assembly}}==
In languages where you have to write your own print function, this task is relatively straightforward. [[MS-DOS]] has a "PrintString" function built-in, but this example will use a custom one with its own control codes. Currently this print function supports two types of control codes beyond the standard ones: a set of control codes that change the text color, and one that starts a new line with a specified number of spaces.
 
(Generally speaking, the implementation below isn't the best way to do it for compatibility reasons, since you end up sacrificing the ability to print certain characters. It's usually better to have one escape character and then the character after it becomes the control code. That way you only sacrifice one character instead of dozens. And technically you don't even lose the escape character since it can always escape itself by doubling it up.)
 
The routine below is called repeatedly by a routine named <code>PrintString_Color</code> until the null terminator is read.
<lang asm>PrintChar_Color: ;Print AL to screen
push dx
push ax
mov dx,8F80h
call CompareRange8 ;sets carry if 80h <= al <= 8Fh, clears carry otherwise
jc isColorCode
cmp al,90h
jne skipCCR_Color
call CustomCarriageReturn ;prints new line, then moves cursor a variable amount of spaces forward.
;this variable is set prior to printing a string that needs it.
jmp done_PrintChar_Color
skipCCR_Color:
mov ah,0Eh
int 10h ;prints character AL to screen, with the ink color stored in BL.
jmp done_PrintChar_Color
isColorCode:
and al,01111111b ;clear bit 7 to convert to VGA colors.
mov bl,al ;text from this point forward will use this color.
done_printChar_Color:
pop ax
pop dx
ret</lang>
1,489

edits