Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to clear the screen in 32-bit x86 assembly language (Video mode 13h)

So at the moment i am copying a screenbuffer (screenbuffer db 64000 DUP(0)) to the video memory (which starts at 0a0000h) to clear the screen. But i was wondering if it is better to just setup the video mode again like this:

mov ax, 13h
int 10h

which seems to clear the screen as well.

Or is there an even better way to clear the screen?

like image 870
T Snake Avatar asked Dec 24 '16 23:12

T Snake


People also ask

What language does x86 use?

Many x86 assemblers use Intel syntax, including FASM, MASM, NASM, TASM, and YASM. GAS, which originally used AT&T syntax, has supported both syntaxes since version 2.10 via the .

What is JB x86?

2. 1. IIRC, on x86 "JB" means "Jump if Borrow," which would occur if the carry flag is set as pointed out by Simon... – stix.

What is an instruction in assembly language?

An instruction is a statement that is executed at runtime. An x86 instruction statement can consist of four parts: Label (optional) Instruction (required) Operands (instruction specific)


1 Answers

You could use STOSD with a REP prefix to clear video memory for video mode 13 (320x200x256 colors). REP STOSD will repeat STOSD by the count stored in ECX . STOSD will write each DWORD in EAX to ES:[EDI] incrementing EDI by 4 each time.

REP: Repeats a string instruction the number of times specified in the count register.

STOSD: stores a doubleword from the EAX register into the destination operand.

Sample code could look something like:

cld                    ; Set forward direction for STOSD
mov ax, 0x0013
int 0x10               ; Set video mode 0x13 (320x200x256 colors)

push es                ; Save ES if you want to restore it after
mov ax, 0xa000
mov es, ax             ; Beginning of VGA memory in segment 0xA000
mov eax, 0x76767676    ; Set the color to clear with 0x76 (green?) 0x00=black
xor edi, edi           ; Destination address set to 0
mov ecx, (320*200)/4   ; We are doing 4 bytes at a time so count = (320*200)/4 DWORDS
rep stosd              ; Clear video memory
pop es                 ; Restore ES

This code assumes you are on a 32-bit processor, but doesn't assume you are running in unreal mode.

If you are using a 16-bit processor (8086/80186/80286) you would have to use the 16-bit registers, and use REP STOSW . CX would be set to (320*200)/2 instead of (320*200)/4. The 16-bit processors don't allow for 32-bit operands so don't support STOSD.

You could easily convert this code to be an assembly language function.

like image 171
Michael Petch Avatar answered Nov 09 '22 13:11

Michael Petch