📜 ⬆️ ⬇️

Python on Assembler (Tasm)

Today we will write in text mode using the BIOS and DOS interrupts snake on Assembler. To do this, you need to know the basics, to be able to assemble (Tasm) and link (Tlink) code.

To begin with, we will write a base - a snake, which moves in one direction along the playing field. The snake will consist of the symbol " * ", the coordinates of each symbol are stored in memory.



View code
model small .data ; .     snake dw 0000h dw 0001h dw 0002h dw 0003h dw 0004h dw 7CCh dup('?') .stack 100h .code ;       delay proc push cx mov ah,0 int 1Ah add dx,3 mov bx,dx repeat: int 1Ah cmp dx,bx jl repeat pop cx ret delay endp start: mov ax,@data mov ds,ax mov es,ax mov ax,0003h int 10h ;   mov cx,5 mov ax,0A2Ah int 10h ;   5  "*" mov si,8 ;    xor di,di ;    mov cx,0001h ; cx    .     cx    x  y main: ;  call delay xor bh,bh mov ax,[snake+si] ;     add ax,cx ;  x inc si inc si mov [snake+si],ax ;       mov dx,ax mov ax,0200h int 10h ; .   mov ah,02h mov dl,002Ah int 21h ;   '*' mov ax,0200h mov dx,[snake+di] int 10h mov ax,0200h mov dl,0020h int 21h ; ,     inc di inc di jmp main end start 


Add the “key_press” procedure for handling keystrokes and assigning a value to the CX register, which is responsible for head direction.
')
Arrow control.


key_press
 key_press proc mov ax, 0100h int 16h jz en ;   xor ah, ah int 16h cmp ah, 50h jne up cmp cx,0FF00h ;      je en mov cx,0100h jmp en up: cmp ah,48h jne left cmp cx,0100h je en mov cx,0FF00h jmp en left: cmp ah,4Bh jne right cmp cx,0001h je en mov cx,0FFFFh jmp en right: cmp cx,0FFFFh je en mov cx,0001h en: ret key_press endp 


Call it immediately after calling the delay procedure:

 main: call delay call key_press 


Feed the snake, create the add_food procedure. This procedure will place food, "$" symbols on the game board. As random numbers we will take time.
add_food
 add_food proc sc: inc bl ;  BL   cmp bx,50h ;   jng ex shr bl,1 ; ,   2   jmp sc ex: mov dl,bl ;  sc2: cmp bx,19h jng ex2 shr bl,2 jmp sc2 ex2: mov dh,bl ;  mov ax,0200h int 10h mov ax,0800h int 10h cmp al,2Ah ;    je sc cmp al,40h je sc ;   mov ax,0200h mov dl,0024h int 21h ret add_food endp 



Call 1 at the beginning.

  mov bl,51h call add_food main: 


We make a check whether the snake ate food or not. If eaten, call the procedure "add_food" and do not remove the tail.

The check is added to the code before displaying the head symbol:

  mov ah,02h int 10h ; .   mov ax,0800h int 10h ;  mov dh,al mov ah,02h mov dl,002Ah int 21h ;   '*' cmp dh,24h jne next call add_food jmp main next: 


Let's complicate the game. After the python has eaten 5 characters, the "@" symbol will appear in the tail. We write the counter and the output symbol:


shit
 ;     .data tick dw 0 ; -------------------------------------------------------------------- cmp dh,24h jne next push cx ;   mov cx,[tick] inc cx cmp cx,5 jne exl xor cx,cx mov ax,0200h mov dx,[snake+di-2] int 10h mov ax,0200h mov dl,0040h int 21h exl:mov [tick],cx pop cx call add_food jmp main next: 


What a game without a Game Over. We write the procedure for checking the field boundary, as well as plunging into itself and the "@" symbol.
game_over
 game_over proc ;  cmp dl,50h je exit cmp dl,0 jl exit cmp dh,0 jl exit cmp dh,19h je exit ;  cmp al,2Ah je exit cmp al,40h je exit jmp good exit: mov ax,4c00h int 21h good: ret game_over endp 


Call it after reading the symbol:

  mov ax,0800h int 10h ;  call game_over mov dh,al 


We add a bit of magic after incrementing indexes.
magic
  inc si inc si cmp si,7CAh jne nex xor si,si nex: --------------------------------------------------------------------- inc di inc di cmp di,7CCh jne main xor di,di 



Well, that's it, you can also add a menu with a choice of level, pause, Game Over screensaver, scorecard.

Under the link the archive with the source code, exe's and DosBox for those who do not start.

Since the last day of the programmer!

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


All Articles