📜 ⬆️ ⬇️

Old, kind “The Matrix” or visualizer for matrix version one

On Habré, articles with the description of the application from the famous film “The Matrix” have repeatedly appeared. As you know, they all belong to the so-called class View Matrix, allow you to visualize the state of the world in which the main characters of the film live. But unlike previous versions of these computer programs, I want to talk about the visualizer of the matrix of the first version (which was mentioned in the movie by the hero named Morpheus in the first part of the trilogy).



Dear friends, this article is dedicated to everyone who remembers what Dos is, 80x25 video mode and other “wonderful little things” of an old school application developer. And also who are interested in meeting this.

The following is the background of this story. At the beginning of the two thousandths, when I was a student, I had a wonderful device (pocket personal computer): Compaq iPAQ H3800 with the installed Microsoft Windows Pocket PC 2002 operating system. Also, there were long, dark evenings of winter holidays in the village, not having with you neither the internet nor the personal computer in this place. But only this device and the student’s great desire to stretch brain cells are not trivial cross / scanwords, but something more interesting. I confess that before going to the village on this device, I installed the Dos operating system emulator called PocketDOS, and also took with me at that time my “reference book”, this remarkable work of the author Zubkov SV, titled: “ Assembler for Dos, Windows and UNIX. ” Plus, I came in handy: the stylus and the standard notebook of the Windwos Pocket PC operating system.
(At the moment, of course, it would be more appropriate to use TASM in a desktop PC emulator DosBox).
')
However, the results of the recent survey “In what language would you write the Matrix?” Confirm my conjectures that it is better to visualize the matrix in the same programming language as writing it.

So, let's begin to solve the “The Matrix” crossword in Assembler language, i80186 processor, in the Dos operating system using the compiler and linker Turbo Assembler (hereinafter TASM) from Borland, on the iPAQ H3800.

As is known, the i80186 processor is 16-bit, respectively, the registers of this processor are 16 bits (for example, the AX battery register) and are divided into two parts of 8 bits each (AH is the highest part and AL is the youngest). We also need BIOS interrupts: 10h and 16h, which will control the video mode and keyboard, respectively. And the Dos interrupt number 21h, which we need in this case to display the string on the display and correctly end the program.

To display the matrix, we need only a small amount of RAM, so we will use the format of the executable file - .com and the memory model tiny. As written by the assembler classics for Dos, we retreat in memory of 100h.

.model tiny ;     .186 ;  i80186 .code ;   ,   org 100h ;    


Next comes the memory in which we declare variables and constants for our code, and immediately jump over this part of the memory in order not to execute it:

 main: jmp start ;          W equ 1Fh H equ 18h OFFS equ 04h cells db W + OFFS dup(0) c_n db W + OFFS dup(0) bsymb db 0 _sleep dw 1fffh ;        the_matrix db "THEMATRI X" ;   ,   the_matrix_len equ $ - the_matrix ;   ,  $       msg_end db 0ah, 0dh, "Follow the white rabbit.", 0ah, 0dh, "$" timer dw 04ffh ;     


So, we got to the point where you need to specify the video display mode of our computer in which we will display the matrix characters. But first, save the current mode to the stack in order to return to it at the end.

 mov ax, 0f00h ;    int 10h push ax ;     

We set the video mode: text CGA, EGA 80x25:

 mov ax, 0003h int 10h 


Next is just a huge piece of code in which incredible magical transformations occur:
  1. Determine the position of the character on the screen that you want to wipe
  2. Procedure for moving the cursor and rubbing the symbol
  3. Determining the position of the character display on the screen
  4. Generating a character from an ASCII table
  5. Output it with a specific color
  6. Delay cycle between next character output
  7. Determining the expired timer output value of the inscription "THEMATRIX"
  8. The output of the inscription "THEMATRIX"

Hidden text
 main_loop: imul dx, 4E35h inc dx push dx and dh, W add dh, OFFS shr dx, 08h mov bx, dx lea di, cells add byte ptr [di+ bx], 1 mov dh, byte ptr [di+ bx] cmp dh, H jne next1 mov byte ptr [di+ bx], 0 next1: cmp dh, 0 je draw1 dec dh ;   mov bh, 00h mov ah, 02h ;    int 10h mov ah, 09h ;  /     mov al, bsymb ;   mov bx, 0002h ;   ,   mov cx, 01h ; (   ) int 10h draw1: inc dh mov bh, 00h mov ah, 02h int 10h mov ah, 09h ;  /     pop dx imul dx, 4E35h inc dx push dx mov bsymb, dh ;   mov al, bsymb mov bx, 000Ah mov cx, 01h int 10h ;... pop dx imul dx, 4E35h inc dx push dx and dh, W add dh, OFFS shr dx, 08h mov bx, dx lea di, c_n add byte ptr [di+ bx], 01h mov dh, byte ptr [di+ bx] cmp dh, H + 01h jne next2 mov byte ptr [di+ bx], 00h next2: dec dh mov bh, 00h mov ah, 2 int 10h mov ah, 09h ;  /     mov al, 08h ;   mov bh, 00h ;   mov bl, 00h ;   ()   () mov cx, 0001h ;   int 10h ; sleep cycle mov cx, _sleep loop2: nop loop loop2 ;draw message cmp timer, 0 je no_msg dec timer mov ax, 1300h ;   mov ch, 00h mov cl, the_matrix_len mov bh, 00h ;   mov bl, 20h ;  mov dl, 0Bh ;  () mov dh, 0Bh ;  () lea bp, the_matrix ;   int 10h inc dh 





And finally, we need to get a signal that we no longer want to look at the matrix and can already follow the white rabbit. To do this, we use the keyboard key test and if the character was not pressed, then continue to display the matrix characters:
 mov ah, 01h int 16h pop dx jz loop1 jmp end_prog loop1: jmp main_loop 

The symbol is pressed and we close the program, but first restore the previous video mode:
 end_prog: pop ax ;     mov ah, 00h mov bh, 00h int 10h 

Follow the white rabbit:
 mov ah, 09h ;     lea dx, msg_end ;  ,   '$' (ASCII 24H) int 21h 



We finish the program correctly:
 mov ah, 4ch mov al, 00h ;   int 21h end main 


If you blind all these pieces of code together, then you can compile and link with the help of the commands:
 tasm /t /z matrix tlink /t matrix 


As a result, this is what we have:




Update: at the request of Habr users, the installation of an “honest” matrix font was added to the code, with the following result:

Source: https://habr.com/ru/post/198106/


All Articles