The content of the article
Want to practice assembly language coding? Let's create a game step by step and launch it directly from the boot sector of your computer. If you think that 512 bytes is not enough for a full game, do not rush to conclusions. By the end of the article, you can do it yourself!
Yes, pushing something intelligible into 512 bytes of the boot sector is another challenge. The boot sector program is executed before the operating system is launched, which means that the OS functions are not available to it.
Even for such a seemingly simple action, how to display a number on the screen, you will have to write your own subroutine. In addition, forget about the high resolution of the screen, simple and convenient access to the GPU and sound card. And keep in mind that the BIOS, executing the boot-sector program, looks at your Ryzen or Core i9 processor as a primitive 16-bit 8088.
Introductory is not very encouraging! But nobody took away the basic BIOS functions from us. We can display text, draw large-sized pixels, read the system timer, monitor the keyboard. This is quite enough for a simple toy!
In case you missed the hype in 2013, I’ll explain the meaning of the game. The bird flying from left to right on the screen should fly around the pipes protruding from the bottom and top of the screen. If the player does nothing, she spontaneously decreases. Pressing the button makes it flap its wings and rise a little up. The secret is to maintain the right rhythm and not crash. That's the whole game.
And now let's get down to implementation!
We tell the compiler that our program is 16-bit, reserve several memory cells for variables, jump to the entry point. Please note that we are not creating variables here, we are not allocating memory for them, but simply setting mnemonics for memory cells that already exist.
We do some more preparatory gestures:
- go to text mode 25 × 80 and clear the screen;
- reset the “direction flag” so that the lines are processed from left to right, and not vice versa (when we turn to instructions like stosw);
- segment registers aim at the area of RAM, which is displayed on the video memory. So it will be more convenient for us to draw the playing field.
We initialize the variables. At the same time, we try to save the number of bytes used. When we first put the value we need in AX, and then through
stows assign it to another variable, it turns out less bytes than when we independently initialize each variable. Especially when we assign the same values - like two zeros at the beginning.
It is necessary to give the player time to adapt to the management, so the first pipe is drawn only after the 160th frame. IN
next write the number 160 (
0xA0) and do not write
mov ax, 0x00A0, a
mov al, 0xA0. Of course, it’s important for us to
AH it was zero, but we know for sure that it is already there, so we will not spend a whole byte on re-zeroing.
Continuation is available only to participants
Materials from the latest issues become available separately only two months after publication. To continue reading, you must become a member of the Xakep.ru community.
Join the Xakep.ru Community!
Membership in the community during the specified period will open you access to ALL Hacker materials, increase your personal cumulative discount and allow you to accumulate a professional Xakep Score!
I am already a member of Xakep.ru