“This is the 10th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

Manipulate video memory output string

In the past we have introduced the way to print a string using interrupts. Today we will learn a way to print a string without interrupts

In the memory address structure of 8086, the memory area of B8000H~BFFFFH is the display memory area. Once the data is written to this address space, the CPU will read the data from the offset address 0 and then display the output, (each time the data is read from 0).

Code attempts:

start:
	   mov ax,0B800H
	   mov ds,ax
  
	 
	   mov dl,'a'           
	   mov ds:[0],dl
end start
Copy the code

In this region, each character occupies a fixed space of two bytes, that is, ds:[0] and DS :[1] store information about a character. The former stores the specific content of the character, and the latter stores the corresponding color of the character

Such as:

start: mov ax,0B800H mov ds,ax mov dl,'a' mov ds:[0],dl mov dl,00000100B ; Let the characters appear in red mov DS :[1],dl end startCopy the code

Character color setting rules:

0 0 0 0 0 0 0 0 0; Character attributes are represented by eight binary bitsCopy the code

Counting from high to low, the first binary indicates whether a flicker trace is displayed

start: mov ax,0B800H mov ds,ax mov dl,'a' mov ds:[0],dl mov dl,10000000B ; Preserve character scintillation trace mov DS :[1],dl end startCopy the code

The 234th bit indicates that the character background color represents: RGB, that is, red, green, blue

start: mov ax,0B800H mov ds,ax mov dl,'a' mov ds:[0],dl mov dl,01000000B ; The background color is set to red mov DS :[1],dl end startCopy the code

The fifth bit indicates whether the character is highlighted

start: mov ax,0B800H mov ds,ax mov dl,'a' mov ds:[0],dl mov dl,00001100B ; The character color is set to red and mov DS :[1], DL end start is highlightedCopy the code

The 678th bit represents the color of the character itself: RGB, namely red, green, and blue

start: mov ax,0B800H mov ds,ax mov dl,'a' mov ds:[0],dl mov dl,00000111B ; The background color is set to white. The default color is white mov DS :[1],dl end startCopy the code

Since the CPU reads data from offset 0 and displays the output, if you write character data directly to offset 6, the first three will be placeholders

start: mov ax,0B800H mov ds,ax mov dl,'a' mov ds:[0],dl mov dl,00000111B mov ds:[6],dl ; The output is "a" end startCopy the code

String printing

data segment str db 'hello pangshu' endstr db '' data ends code segment start: mov ax,data mov ds,ax mov ax,0B800H mov es,ax mov cx ,offset endstr-str mov bx,0 mov si,0 print: mov dl,ds:[si] mov es:[bx],dl mov dl,00000111B ; Mov ES :[bx+1],dl Inc si add bx,2 loop print code ends end startCopy the code

Characters can be animated by the character refresh feature

; Code segment start: mov ax,0B800H mov es,ax mov bx,0 mov cx,30 print: mov es:[bx],' ' mov dl,'a' mov es:[bx+2],dl add bx,2 loop print code ends end startCopy the code

By default, the screen displays 80×25 characters and 106×38 characters in full screen, so you can use this feature to move characters up and down

; Code segment start: mov ax,0B800H mov es,ax mov bx,0 mov cx,25 print: mov es:[bx],'a' mov dl,' ' mov es:[bx-160],dl add bx,160 ; Loop print code ends end start loop print code ends start Code segment start: mov ax,0B800H mov es,ax mov bx,160*24 mov cx,25 print: mov es:[bx],'a' mov dl,' ' mov es:[bx+160],dl sub bx,160 ; Loop print code ends end startCopy the code

Also, move the characters diagonally

; Code segment start: mov ax,0B800H mov es,ax mov bx,0 mov cx,25 print: mov es:[bx],'a' mov dl,' ' mov es:[bx-161],dl add bx,161 ; Loop print code ends end startCopy the code

Note: in 8086, the system provides a Video Service interrupt for our use. 10H interrupt code can also be used to print strings with color attributes

; Example 1: mov ah,2; Place cursor mov bh,0; Page 0 mov dh,5; Line number mod dl,12; Column number int 10H; Example 2: mov ah,9; Display the character mov al,'a' at cursor position; Mov bl,11001010B; Mov bh,0; 第0页 mov cx,3; Repeat display 3 times int 10HCopy the code

Use keyboard input to control character movement

Use interrupt code 16

; Code segment start: mov ax,0B800H mov es,ax mov bx,0 mov cx,30 Scan: mov ah, 00H int 16H CMP al,61H; Jne scan2; JMP not equal If the two are not equal, skip to scan2. Otherwise, run call left JMP scan scan2: CMP al,64H jne scan3 call right JMP scan scan3: cmp al,77H jne scan4 call top jmp scan scan4: cmp al,73H jne scan call down jmp scan right: mov es:[bx],' ' mov dl,'a' mov es:[bx+2],dl add bx,2 ret left: mov es:[bx],' ' mov dl,'a' mov es:[bx-2],dl sub bx,2 ret top: mov es:[bx],' ' mov dl,'a' mov es:[bx-160],dl sub bx,160 ret down: mov es:[bx],' ' mov dl,'a' mov es:[bx+160],dl add bx,160 ret code ends end startCopy the code