CSC231 Ansi Sequences

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 13:10, 10 December 2008 (UTC)



Ansi sequences can be used to control the behavior of the program with the text screen. It allows for (crude) animation using text.

The following function illustrates how to clear the screen and position the cursor at position 1,1 (top left):

;;; ------------------------------------------------------
;;; clrScreen:  clears the screen and brings the cursor
;;; to "home" position.  This is done by using ANSI sequences
;;; that, when sent to the screen, do not actually print
;;; anything, but instead modify different things, such as
;;; the position of the cursor, the contents of the screen,
;;; or other screen-related properties.  All ansi sequences
;;; start with Ascii 27, which is the ESCape character.
;;; ------------------------------------------------------
        section         .data
home    db              27,"[2J"        ; clear the screen
        db              27,"[1;1H"      ; ansi sequence to bring
                                        ; cursor at position 1,1
homeLen equ             $-home

        section         .text

clrScreen:
        pushad                          ; save all registers

        mov             eax,WRITE       ; already defined
        mov             ebx,STDOUT      ; already defined
        lea             ecx,[home]
        mov             edx,homeLen
        int             0x80

        popad                           ; restore all registers
        ret

Ansi Sequences

The following list of Ansi sequences was taken from http://isthe.com/chongo/tech/comp/ansi_escapes.html

Wherever you see '#', that should be replaced by the appropriate number.

        ESC code sequence                  Function
       -------------------         ---------------------------
Cursor Controls:
         ESC[#;#H or ESC[#;#f      Moves cusor to line #, column #
         ESC[#A                    Moves cursor up # lines
         ESC[#B                    Moves cursor down # lines
         ESC[#C                    Moves cursor forward # spaces
         ESC[#D                    Moves cursor back # spaces
         ESC[#;#R                  Reports current cursor line & column
         ESC[s                     Saves cursor position for recall later
         ESC[u                     Return to saved cursor position

Erase Functions:
         ESC[2J                    Clear screen and home cursor
         ESC[K                     Clear to end of line

Set Graphics Rendition:
         ESC[#;#;....;#m           Set display attributes where # is
                                       00 for normal display (or just 0)
                                       01 for bold on (or just 1)
				       02 faint (or just 2)
				       03 standout (or just 3)
                                       04 underline (or just 4)
                                       05 blink on (or just 5)
                                       07 reverse video on (or just 7)
                                       08 nondisplayed (invisible) (or just 8)
				       22 normal
				       23 no-standout
				       24 no-underline
				       25 no-blink
				       27 no-reverse
                                       30 black foreground
                                       31 red foreground
                                       32 green foreground
                                       33 yellow foreground
                                       34 blue foreground
                                       35 magenta foreground
                                       36 cyan foreground
                                       37 white foreground
                                       39 default foreground
                                       40 black background
                                       41 red background
                                       42 green background
                                       43 yellow background
                                       44 blue background
                                       45 magenta background
                                       46 cyan background
                                       47 white background
                                       49 default background

         ESC[=#;7h or      Put screen in indicated mode where # is
         ESC[=h or                  0 for 40 x 25 black & white
         ESC[=0h or                 1 for 40 x 25 color
         ESC[?7h                    2 for 80 x 25 b&w
                                    3 for 80 x 25 color
                                    4 for 320 x 200 color graphics
                                    5 for 320 x 200 b & w graphics
                                    6 for 640 x 200 b & w graphics
                                    7 to wrap at end of line

         ESC[=#;7l or ESC[=l or   Resets mode # set with above command
         ESC[=0l or ESC[?7l

Keyboard Reassignments:

         ESC[#;#;...p           Keyboard reassignment. The first ASCII
         or ESC["string"p       code defines which code is to be
         or ESC[#;"string";#;   changed. The remaining codes define
            #;"string";#p       what it is to be changed to.

   E.g. Reassign the Q and q keys to the A and a keys (and vice versa).

         ESC [65;81p                    A becomes Q
         ESC [97;113p                   a becomes q
         ESC [81;65p                    Q becomes A
         ESC [113;97p                   q becomes a

         E.g. Reassign the F10 key to a DIR command.

         ESC [0;68;"dir";13p       The 0;68 is the extended ASCII code
                                   for the F10 key and 13 is the ASCII
                                   code for a carriage return.

         Other function key codes       F1=59,F2=60,F3=61,F4=62,F5=63
                                        F6=64,F7=65,F8=66,F9=67,F10=68