Snake Game Code in Assembly Language

October 11, 2022 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download Snake Game Code in Assembly Language...

Description

 

[org 0x0100] jmp start snake: times 240 db 0 lenght: times 240 dw 0 screenpositon: times 240 dw 0 orientation: dw 0 tickcount: dw 0 tickcount2: dw 0 lives: db 3 livesmessage: db !our "ives :  timemessage: db #ime le$t :  minitues: db 3 seconds: dw %0 tickcount3: dw 0 speed: dw 32 score: dw 0 scoremessage: db !our &core :  tickcount4: dw 0 positiono$$ruit: dw 0 generaterandom: pusha mov ax'[cs:tickcount4] checki$itisonoraboveupperboarder: cmp ax'%42 ja checki$itisonorbelowlowerboarder add ax'1%0 cmp ax'%42 jbe checki$itisonoraboveupperboarder

checki$itisonorbelowlowerboarder: cmp ax'3(1% jna checki$boarder sub ax'1%0 cmp ax'3(1% jae checki$itisonorbelowlowerboarder checki$boarder: mov mov mov cmp jne add

ch'0x0) cl'* bx'ax word[es:bx]'cx checki$onsnake ax'%

checki$onsnake: mov bx'0 mov cx'[cs:lenght] loop$orchecki$onsnake: cmp ax'[cs:screenpositon+bx] je add outo$outo$lop bx'2 loop loop$orchecki$onsnake

 

outo$outo$lop: mov bx'[cs:lenght] shl bx'1 sub bx'2 outo$loop: mov cx'[cs:screenpositon+bx] add cx'% mov ax'cx

store$ruitposition: mov word[cs:positiono$$ruit]'ax popa ret

print$ruit: pusha mov ax'0xb,00 mov es'ax mov di'[cs:positiono$$ruit] mov ah'0x04 mov al'- mov word[es:di]'ax popa ret

printscore: pusha

mov ax'0xb,00 mov es'ax mov cx'13 mov di'2., mov ah'0x03 mov bx'0 loop$orscoremessage: mov al'[cs:scoremessage+bx] mov word[es:di]'ax add di'2 inc bx loop loop$orscoremessage

mov ax'[cs:score] push ax

 

call printnum$orscore

popa ret

printnum$orscore: push bp mov bp' sp push es push ax push bx push cx push dx push di

mov mov mov mov mov

ax' es' ax' bx' cx'

0xb,00 ax [bp+4] 10 0

/ / / /

point es to video base load number in ax use base 10 $or division initialie count o$ digits

nextdigit$orscore: mov div add push inc cmp jn mov

dx' bx 0 dl' 0x30 dx cx ax' 0 nextdigit$orscore di' 304

nextpos$orscore: pop dx mov dh' 0x04 mov [es:di]' dx add di' 2 loop nextpos$orscore pop pop pop pop pop pop pop ret

di dx cx bx ax es bp 2

increasespeed: pusha cmp word[cs:speed]'1 je endo$increasespeed

/ / / / / /

ero upper hal$ o$ dividend divide b 10 convert digit into ascii value save ascii value on stack increment count o$ values is the uotient ero / i$ no divide it again

/ / / /

remove a digit $rom the stack use normal attribute print char on screen move to next screen location / repeat $or all digits on stack

 

cmp jne mov jmp

word[cs:speed]'2 cmp$or4 word[cs:speed]'1 endo$increasespeed

cmp$or4: cmp jne mov jmp

word[cs:speed]'4 cmp$or, word[cs:speed]'2 endo$increasespeed

cmp$or,: cmp jne mov jmp

word[cs:speed]', cmp$or32 word[cs:speed]'4 endo$increasespeed

cmp$or32: cmp jne mov jmp

word[cs:speed]'32 cmp$or1% word[cs:speed]'1% endo$increasespeed

cmp$or1%: cmp jne word[cs:speed]'1% endo$increasespeed mov word[cs:speed]', endo$increasespeed: popa ret

printnum: push mov push push push push push push

mov mov mov mov mov

bp bp' sp es ax bx cx dx di

ax' es' ax' bx' cx'

0xb,00 ax [bp+4] 10 0

nextdigit: mov div add push inc cmp jn

/ / / /

point es to video base load number in ax use base 10 $or division initialie count o$ digits

dx' 0 bx dl' 0x30 dx

/ / / /

ero upper hal$ o$ dividend divide b 10 convert digit into ascii value save ascii value on stack

cx ax' 0 nextdigit

/ is increment count o$ values / the uotient ero / i$ no divide it again

 

mov

di' 24,

nextpos: pop dx mov dh' 0x04 mov [es:di]' dx add di' 2 loop nextpos pop pop pop pop pop pop pop ret

/ / / / /

remove a digit $rom the stack use normal attribute print char on screen move to next screen location repeat $or all digits on stack

di dx cx bx ax es bp 2

printtime: pusha mov ax'0xb,00 mov es'ax mov cx'12 mov di'220 mov ah'0x03 mov bx'0 loop$ortimemessage: mov al'[cs:timemessage+bx] mov word[es:di]'ax add di'2 inc bx loop loop$ortimemessage mov mov add mov

ah'0x04 al'[cs:minitues] al'0x30 word[es:di]'ax

mov ah'0x04 mov al': mov word[es:di+2]'ax mov ax'[cs:seconds] cmp word[cs:seconds]') jbe printsimpleseconds push ax call printnum jmp endo$printnum printsimpleseconds: mov bx'[cs:seconds] mov al'bl add al'0x30 mov ah'0x04

 

mov mov mov mov

bh'0x04 bl'0 word[es:di+4]'bx word[es:di+%]'ax

endo$printnum: popa ret printlives: pusha mov ax'0xb,00 mov es'ax mov cx'13 mov di'1%0 mov ah'0x03 mov bx'0 loop$orlivesmessage: mov al'[cs:livesmessage+bx] mov word[es:di]'ax add di'2 inc bx loop loop$orlivesmessage mov mov ah'0x04 al'[cs:lives] add al'0x30 mov word[es:di]'ax popa ret extendlength: pusha mov cx'[cs:lenght] mov bx'[cs:lenght] sub bx'1 loop$orextendlength: mov al'[cs:snake+bx] mov [cs:snake+bx+4]'al dec bx loop loop$orextendlength

mov mov shl sub

cx'[cs:lenght] bx'[cs:lenght] bx'1 bx'2

loop$orextendlengtho$screenpositon: mov ax'[cs:screenpositon+bx] mov word[cs:screenpositon+bx+,]'ax sub bx'2

 

loop loop$orextendlengtho$screenpositon

add word[cs:lenght]'4

popa ret reset: pusha call clrscr call printboarders call initialiesnake call printsnake call generaterandom call print$ruit mov word[cs:speed]'32 mov word[cs:orientation]'1

mov word[cs:score]'0 call printscore mov bte al'[cs:lives] sub al'1 mov bte[cs:lives]'al call printlives cmp bte[cs:lives]'0 jne printingnormaltime mov word[cs:seconds]'0 mov bte[cs:minitues]'0 call printtime jmp endo$reset printingnormaltime: mov word[cs:seconds]'%0 mov bte[cs:minitues]'3 call printtime endo$reset: popa ret clrscr: push es push ax push cx push di mov ax' 0xb,00 mov es' ax xor di' di mov ax' 0x0.20 mov cx' 2000

 

cld rep stosw pop pop pop pop

di cx ax es

ret printboarders: pusha mov ax'0xb,00 mov es'ax mov di'4,0 mov cx'1%0 mov ah'0x0) mov al'* printupperboarder: mov word[es:di]'ax add di'2 dec cx loop printupperboarder mov di'3(20 mov cx'1%0 printlowerboarder: mov word[es:di]'ax add di'2 dec cx loop printlowerboarder mov di'4,0 mov cx'20 printle$tboarder: mov word[es:di]'ax add di'1%0 loop printle$tboarder mov di'%3, mov cx'20 printrightboarder: mov word[es:di]'ax add di'1%0 loop printrightboarder

popa ret printsnake: pusha mov ax'0xb,00 mov es'ax

 

mov di'1),0 mov word cx'[cs:lenght] mov bx'0 mov ah'0x0% mov si'0 loop$orprintsnake: mov al'[cs:snake+bx] mov word[es:di]'ax mov word[cs:screenpositon+si]'di inc bx add di'2 add si'2 loop loop$orprintsnake

popa ret initialiesnake: pusha mov word[cs:lenght]'20 mov cx'20 mov bte[cs:snake+1)]'+ dec cx mov bx'0 loop$orinitialiesnake: mov bte[cs:snake+bx]' inc bx loop loop$orinitialiesnake

popa ret movright: pusha mov ax'0xb,00 mov es'ax cmp word[cs:orientation]'3 je endo$movright mov word si'[cs:lenght] shl si'1 sub si'2 colissioncheckright: mov ah'0x0) mov al'* mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di+2]

 

jne colissioncheckrightitsel$ call reset jmp endo$movright colissioncheckrightitsel$: mov ah'0x0% mov al' mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di+2] jne colissioncheckright$ruit call reset jmp endo$movright colissioncheckright$ruit: mov ah'0x04 mov al'- mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di+2] jne continuetomoveright inc word[cs:score] call generaterandom call print$ruit call extendlength mov si'[cs:screenpositon+,] mov di'[cs:screenpositon+10] sub di'si checkup$orright$ruit: cmp di'1%0 jne checkdown$orright$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placeup$orright$ruit: sub si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeup$orright$ruit jmp continuetomoveright checkdown$orright$ruit: cmp di'1%0 jne checkle$t$orright$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placedown$orright$ruit: add si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placedown$orright$ruit

 

jmp continuetomoveright

checkle$t$orright$ruit: cmp di'2 jne checkright$orright$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placele$t$orright$ruit: sub si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placele$t$orright$ruit jmp continuetomoveright checkright$orright$ruit: mov cx'4 mov ah'0x0% mov al' mov bx'% placeright$orright$ruit: add si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeright$orright$ruit

continuetomoveright: mov word cx'[cs:lenght] dec cx mov word bx'[cs:lenght] dec bx mov word si'[cs:lenght] shl si'1 sub si'2 mov ah'0x0% mov di'[cs:screenpositon+si] /osition 56 789 mov al'[cs:snake+bx] / ;et value o$ 789 mov word[es:di+2]'ax add word[cs:screenpositon+si]'2 loop$ormovingright: mov bx'[cs:screenpositon+si2] mov ax'[es:bx] mov word[es:di]'ax mov [cs:screenpositon+si2]'di mov di'bx

 

sub si'2 loop loop$ormovingright mov dx'0x0.20 mov word[es:di]'dx mov word[cs:orientation]'1 endo$movright: popa ret movup: pusha mov ax'0xb,00 mov es'ax cmp word[cs:orientation]'4 je endo$movingup mov word si'[cs:lenght] shl si'1 sub si'2 colissioncheckup: mov ah'0x0) mov al'* mov di'[cs:screenpositon+si] cmp ax'[es:di1%0] jne colissioncheckupitsel$ call reset jmp endo$movingup colissioncheckupitsel$: mov ah'0x0% mov al' mov di'[cs:screenpositon+si] cmp ax'[es:di1%0] jne colissioncheckup$ruit call reset jmp endo$movingup colissioncheckup$ruit: mov ah'0x04 mov al'- mov di'[cs:screenpositon+si] cmp ax'[es:di1%0] jne continuetomoveup inc word[cs:score] call generaterandom call print$ruit call extendlength

mov si'[cs:screenpositon+,] mov di'[cs:screenpositon+10] sub di'si

 

checkup$orup$ruit: cmp di'1%0 jne checkdown$orup$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placeup$orup$ruit: sub si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeup$orup$ruit jmp continuetomoveup checkdown$orup$ruit: cmp di'1%0 jne checkle$t$orup$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placedown$orup$ruit: add si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placedown$orup$ruit jmp continuetomoveup checkle$t$orup$ruit: cmp di'2 jne checkright$orup$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placele$t$orup$ruit: sub si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placele$t$orup$ruit jmp continuetomoveup checkright$orup$ruit: mov cx'4 mov ah'0x0% mov al' mov bx'% placeright$orup$ruit: add si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeright$orup$ruit

 

continuetomoveup: mov word cx'[cs:lenght] dec cx mov word bx'[cs:lenght] dec bx mov word si'[cs:lenght] shl si'1 sub si'2 mov ah'0x0% mov di'[cs:screenpositon+si] /osition 56 789 mov al'[cs:snake+bx] / ;et value o$ 789 mov word[es:di1%0]'ax sub word[cs:screenpositon+si]'1%0 loop$ormovingup: mov bx'[cs:screenpositon+si2] mov ax'[es:bx] mov word[es:di]'ax mov [cs:screenpositon+si2]'di mov di'bx sub si'2 loop loop$ormovingup mov dx'0x0.20 mov word[es:di]'dx mov word[cs:orientation]'2 endo$movingup: popa ret movdown: pusha mov ax'0xb,00 mov es'ax cmp word[cs:orientation]'2 je endo$movingdown mov word si'[cs:lenght] shl si'1 sub si'2 colissioncheckdown: mov ah'0x0) mov al'* mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di+1%0] jne colissioncheckdownitsel$ call reset jmp endo$movingdown colissioncheckdownitsel$:

 

mov ah'0x0% mov al' mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di+1%0] jne colissioncheckdown$ruit call reset jmp endo$movingdown

colissioncheckdown$ruit: mov ah'0x04 mov al'- mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di+1%0] jne continuetomovedown inc word[cs:score] call generaterandom call print$ruit call extendlength mov si'[cs:screenpositon+,] mov di'[cs:screenpositon+10] sub di'si checkup$ordown$ruit: cmp di'1%0 jne checkdown$ordown$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placeup$ordown$ruit: sub si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeup$ordown$ruit jmp continuetomovedown checkdown$ordown$ruit: cmp di'1%0 jne checkle$t$ordown$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placedown$ordown$ruit: add si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placedown$ordown$ruit jmp continuetomovedown checkle$t$ordown$ruit: cmp di'2

 

jne checkright$ordown$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placele$t$ordown$ruit: sub si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placele$t$ordown$ruit jmp continuetomovedown checkright$ordown$ruit: mov cx'4 mov ah'0x0% mov al' mov bx'% placeright$ordown$ruit: add si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeright$ordown$ruit

continuetomovedown: mov dec mov dec

word cx'[cs:lenght] cx word bx'[cs:lenght] bx

mov word si'[cs:lenght] shl si'1 sub si'2 mov ah'0x0% mov di'[cs:screenpositon+si] /osition 56 789 mov al'[cs:snake+bx] / ;et value o$ 789 mov word[es:di+1%0]'ax add word[cs:screenpositon+si]'1%0 loop$ormovingdown: mov bx'[cs:screenpositon+si2] mov ax'[es:bx] mov word[es:di]'ax mov [cs:screenpositon+si2]'di mov di'bx sub si'2 loop loop$ormovingdown mov dx'0x0.20

 

mov word[es:di]'dx mov word[cs:orientation]'4 endo$movingdown: popa ret movle$t: pusha mov ax'0xb,00 mov es'ax cmp word[cs:orientation]'1 je endo$movingle$t mov word si'[cs:lenght] shl si'1 sub si'2 colissioncheckle$t: mov ah'0x0) mov al'* mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di2] jne colissioncheckle$titsel$ call reset jmp endo$movingle$t colissioncheckle$titsel$: mov ah'0x0% mov al' mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di2] jne colissioncheckle$t$ruit call reset jmp endo$movingle$t colissioncheckle$t$ruit: mov ah'0x04 mov al'- mov di'[cs:screenpositon+si] /osition 56 789 cmp ax'[es:di2] jne continuetomovele$t inc word[cs:score] call generaterandom call print$ruit call extendlength

mov si'[cs:screenpositon+,] mov di'[cs:screenpositon+10] sub di'si checkup$orle$t$ruit: cmp di'1%0 jne checkdown$orle$t$ruit mov cx'4 mov ah'0x0% mov al' mov bx'%

 

placeup$orle$t$ruit: sub si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeup$orle$t$ruit jmp continuetomovele$t checkdown$orle$t$ruit: cmp di'1%0 jne checkle$t$orle$t$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placedown$orle$t$ruit: add si'1%0 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placedown$orle$t$ruit jmp continuetomovele$t checkle$t$orle$t$ruit: cmp di'2 jne checkright$orle$t$ruit mov cx'4 mov ah'0x0% mov al' mov bx'% placele$t$orle$t$ruit: sub si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placele$t$orle$t$ruit jmp continuetomovele$t checkright$orle$t$ruit: mov cx'4 mov ah'0x0% mov al' mov bx'% placeright$orle$t$ruit: add si'2 mov word[es:si]'ax mov [cs:screenpositon+bx]'si sub bx'2 loop placeright$orle$t$ruit

continuetomovele$t: mov word cx'[cs:lenght] dec cx mov word bx'[cs:lenght]

 

dec bx mov word si'[cs:lenght] shl si'1 sub si'2 mov ah'0x0% mov di'[cs:screenpositon+si] /osition 56 789 mov al'[cs:snake+bx] / ;et value o$ 789 mov word[es:di2]'ax sub word[cs:screenpositon+si]'2 loop$ormovingle$t: mov bx'[cs:screenpositon+si2] mov ax'[es:bx] mov word[es:di]'ax mov [cs:screenpositon+si2]'di mov di'bx sub si'2 loop loop$ormovingle$t mov dx'0x0.20 mov word[es:di]'dx mov word[cs:orientation]'3 endo$movingle$t: popa ret timer: pusha inc word[cs:tickcount] inc word[cs:tickcount2] inc word[cs:tickcount3] inc word[cs:tickcount4] inc word[cs:tickcount4] checkrandom: cmp word[cs:tickcount4]'4000 jne check20seconds mov word[cs:tickcount4]'0 check20seconds: cmp word[cs:tickcount3]'3%4 jne checklive call increasespeed mov word[cs:tickcount3]'0

checklive: cmp bte[cs:lives]'0 je endo$timer checkminitues: cmp word[cs:seconds]'0 jne checkseconds

 

cmp bte[cs:minitues]'0 jne checkremaining call reset jmp endo$timer checkremaining: dec bte[cs:minitues] mov bte[cs:seconds]'%0 jmp checktickcount checkseconds: cmp word[cs:tickcount2]'1, jne checktickcount dec word[cs:seconds] mov bte[cs:tickcount2]'0 call printtime call printscore checktickcount: mov ax'[cs:speed] cmp word[cs:tickcount]'ax jng endo$timer i$right: cmp word[cs:orientation]'1 jne i$up call movright mov word[cs:tickcount]'0 i$up: cmp word[cs:orientation]'2 jne i$le$t call movup mov word[cs:tickcount]'0 i$le$t: cmp word[cs:orientation]'3 jne i$down call movle$t mov word[cs:tickcount]'0

i$down: cmp word[cs:orientation]'4 jne endo$timer call movdown mov word[cs:tickcount]'0

endo$timer: mov al' 0x20 out 0x20' al / end o$ interrupt popa iret / return $rom interrupt kbisr: pusha cmp bte[cs:lives]'0 je endo$kbisr

 

in al'0x%0 cmp al'0x4 al'0x4 /compare /compare wi with th
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF