《汇编语言写的贪吃蛇小游戏源代码(20页).doc》由会员分享,可在线阅读,更多相关《汇编语言写的贪吃蛇小游戏源代码(20页).doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-DATA SEGMENTdw 0,0snk db 1blk db 32food db 3tal1 db 4tal2 db 2adrs db 5len db ?pst db ?addrs dw ?frow db ?fcol db ?hwrt db ?gmov db game over press r to restart press q to quit $score1 db score :$score2 db ?score0 db 1zero db 48writer db Developer: Geniusdot $email db e-mail: geniusdot$msg1 db The
2、way to play the game:$way db press w to up ,press s to down,press a to left,press d to right$msg db Press any key(except a,s,d,w) to start$DATA ENDSSTACK SEGMENT stack db 200 dup(0)STACK ENDSCODE SEGMENTASSUME CS:CODE,DS:DATA,SS:STACKstart:mov ax,datamov ds,axmov ax,0mov es,axmov frow,10mov fcol,6mo
3、v dh,10 mov dl,26mov ah,2mov bh,0int 10hmov ah,9lea dx,msg1int 21hmov dh,11mov dl,7mov ah,2mov bh,0int 10hmov ah,9lea dx,wayint 21hmov dh,12mov dl,20mov ah,2mov bh,0int 10hmov ah,9lea dx,msgint 21hmov ah,0int 16hmov ah,6mov al,0mov ch,0mov cl,0mov dh,24mov dl,79mov bh,10int 10hmov dh,0mov dl,0mov ah
4、,2mov bh,0int 10hmov ah,9lea dx,score1int 21hmov dl,15mov ah,2mov bh,0int 10hmov ah,9lea dx,writerint 21hmov ah,9lea dx,emailint 21hmov score2,48push es:9*4 ;将原int9入口地址保存pop ds:0push es:9*4+2pop ds:2mov word ptr es:9*4,offset int9 ;更改中断向量表mov es:9*4+2,csjmp aawrite macro row,col,cnt ;宏定义用于向当前光标处输出字符
5、 push bx push cx push dx mov dh,row mov dl,col mov ah,2 mov bh,0 int 10h mov ah,9 mov bl,11 mov cx,1 lea di,cnt ;50 mov al,di int 10h pop dx pop cx pop bxendmreadh macro row,col ;宏定义用于读出当前光标处字符 push dxpush axpush bxmov dh,rowmov dl,colmov ah,2mov bh,0int 10hmov ah,08hint 10hmov pst,alpop bxpop axpop
6、 dxendmwnear macro ;宏定义只用在readcg宏中当readcg的所有判断都不成立调用此宏local wnext1local wnext2local wnext3local wnext4push dxdec dhreadh dh,dlcmp pst,1jne wnext1write dh,dl,tal2jmp wnext4wnext1:inc dhdec dlreadh dh,dlcmp pst,1jne wnext2write dh,dl,tal2jmp wnext4wnext2:inc dhinc dlreadh dh,dlcmp pst,1jne wnext3write
7、 dh,dl,tal2jmp wnext4wnext3:dec dhinc dlreadh dh,dlcmp pst,1jne wnext4write dh,dl,tal2wnext4:pop dxendmreadcg macro row,col ;宏定义用于改变判断出来的字符local tnup,tnup1,tnup2,tnlf,tnlf1,tnlf2,tndn,tndn1,tndn2,tnrt,tnrt1,tnrt2,gooutpush bxpush axpush dxwrite dh,dl,tal1dec rowreadh dh,dlcmp pst,4jne tnup1jmp tnup2
8、tnup1:jmp near ptr tnuptnup2:write dh,dl,blkinc dhinc dhreadh dh,dlcmp pst,1jne tnupwrite dh,dl,tal2jmp near ptr goouttnup:pop dxpush dxdec colreadh dh,dlcmp pst,4jne tnlf1jmp tnlf2tnlf1:jmp near ptr tnlftnlf2:write dh,dl,blkinc dlinc dlreadh dh,dlcmp pst,1jne tnlfwrite dh,dl,tal2jmp near ptr gooutt
9、nlf:pop dxpush dxinc rowreadh dh,dlcmp pst,4jne tndn1jmp tndn2tndn1:jmp near ptr tndntndn2:write dh,dl,blkdec dhdec dhreadh dh,dlcmp pst,1jne tndnwrite dh,dl,tal2jmp near ptr goouttndn:pop dxpush dxinc colreadh dh,dlcmp pst,4jne tnrt1jmp tnrt2tnrt1:jmp near ptr tnrttnrt2:write dh,dl,blkdec dldec dlr
10、eadh dh,dlcmp pst,1jne tnrtwrite dh,dl,tal2jmp near ptr goouttnrt:pop dxpush dxwneargoout:pop dxpop axpop bxendmaddone: ;此标号功能是将蛇身增加一push dxinc score2mov dh,1mov dl,0mov cx,23cmpad1:push cxmov cx,79cmpad2:readh dh,dlcmp pst,2jne nextad3jmp nextad4nextad3:jmp near ptr nextadnextad4:write dh,dl,snkdec
11、 dhreadh dh,dlcmp pst,4jne natupwrite dh,dl,tal2dec dhwrite dh,dl,tal1jmp outonatup:inc dhdec dlreadh dh,dlcmp pst,4jne natlfwrite dh,dl,tal2dec dlwrite dh,dl,tal1jmp outonatlf:inc dhinc dlreadh dh,dlcmp pst,4jne natdnwrite dh,dl,tal2inc dhwrite dh,dl,tal1jmp outonatdn:dec dhinc dlreadh dh,dlcmp pst
12、,4jne natrtwrite dh,dl,tal2inc dlwrite dh,dl,tal1natrt:outo:pop cxjmp near ptr endadnextad:inc dljmp nextad2chgad2:jmp near ptr cmpad2nextad2:loop chgad2sub dl,79inc dhpop cxjmp nextad1chgad1:jmp near ptr cmpad1nextad1:loop chgad1endad:pop dxjmp near ptr crtfaa: ;从这开始产生最原始的蛇mov addrs,offset turnrigh
13、tmov dh,10mov dl,1mov cx,3write dh,dl,tal1inc dlwrite dh,dl,tal2wrt: inc dlwrite dh,dl,snkloop wrtmov len,6mov ax,0jmp wrt1ovflw: ;当蛇碰壁或自身转到此游戏结束mov ah,6mov al,0mov ch,0mov cl,0mov dh,24mov dl,79mov bh,7int 10hmov dh,17mov dl,17mov ah,2mov bh,0int 10hmov ah,9lea dx,gmovint 21hmov ax,0 ;恢复int9中断mov e
14、s,axpush ds:0pop es:9*4push ds:2pop es:9*4+2stop:mov ah,0int 16hcmp al,rje aa1jmp aa2aa1:jmp near ptr startaa2:cmp al,qjne stopjmp near ptr exitwrt1: ;此处蛇行走过程的无限循环call dlypush dxinc dhcmp dh,25je ovflwinc dlcmp dl,80je ovflwpop dxpush dxdec dhcmp dh,0je ovflwdec dlcmp dl,-1je ovflwpop dxpush dxlea a
15、x,turnrightcmp addrs,axjne tonxt2inc dlreadh dh,dlcmp pst,1je tonxt1cmp pst,2je tonxt1cmp pst,4je tonxt1jmp tonxt2tonxt1:jmp ovflwtonxt2:pop dxpush dxlea ax,turnupcmp addrs,axjne tonxt4dec dhreadh dh,dlcmp pst,1je tonxt3cmp pst,2je tonxt3cmp pst,4je tonxt3jmp tonxt4tonxt3:jmp ovflwtonxt4:pop dxpush
16、dxlea ax,turndowncmp addrs,axjne tonxt6inc dhreadh dh,dlcmp pst,1je tonxt5cmp pst,2je tonxt5cmp pst,4je tonxt5jmp tonxt6tonxt5:jmp ovflwtonxt6:pop dxpush dxlea ax,turnbackcmp addrs,axjne tonxt8dec dlreadh dh,dlcmp pst,1je tonxt7cmp pst,2je tonxt7cmp pst,4je tonxt7jmp tonxt8tonxt7:jmp ovflwtonxt8:pop
17、 dxjmp nextacrtf1:jmp near ptr addonecrtf:call rand1call rand2inc frowmov ah,frowmov al,fcolpush dxmov dh,1mov dl,0push cxmov cx,23check1:push cxmov cx,79check2:readh dh,dlcmp pst,1je nextncmp pst,2je nextncmp pst,4je nextnjmp nextnnnextn:cmp ax,dxje crtfnextnn:inc dlloop check2inc dhsub dl,79pop cx
18、loop check1pop cxpop dxwrite frow,fcol,foodnexta:mov ah,frowmov al,fcolcmp ax,dxje crtf12jmp crtf13crtf12:jmp near ptr crtf1crtf13:push dxcmp score2,58jl normalmov score2,49inc score0normal:mov dh,0mov dl,8write dh,dl,score2add dl,score0write dh,dl,zeropop dxcmp adrs,17je jmp1cmp adrs,145je jmp1cmp
19、adrs,31je jmp2cmp adrs,159je jmp2cmp adrs,32je jmp3cmp adrs,160je jmp3cmp adrs,30je jmp4cmp adrs,158je jmp4jmp addrsjmp1:lea ax, turndowncmp ax,addrsje jmp2mov addrs,offset turnupjmp near ptr turnupjmp2:lea ax,turnupcmp ax,addrsje jmp1mov addrs,offset turndownjmp near ptr turndownjmp3:lea ax,turnbac
20、kcmp ax,addrsje jmp4mov addrs,offset turnrightjmp near ptr turnrightjmp4:lea ax,turnrightcmp ax,addrsje jmp3mov addrs,offset turnbackjmp near ptr turnbackturnright: ;此处实现蛇向左走push dxmov dh,1mov dl,0mov cx,23cmpr1:push cxmov cx,79cmpr2:readh dh,dlcmp pst,2je nextr4jmp near ptr nextrnextr4:readcg dh,dl
21、pop cxjmp near ptr endrnextr:inc dljmp nextr2chgr2:jmp near ptr cmpr2nextr2:loop chgr2sub dl,79inc dhpop cxjmp nextr1chgr1:jmp near ptr cmpr1nextr1:loop chgr1endr:pop dxinc dlwrite dh,dl,snkjmp near ptr wrt1turnup: ;此处实现蛇向上走push dxmov dh,1mov dl,0mov cx,23cmpu1:push cxmov cx,79cmpu2:readh dh,dlcmp p
22、st,2jne nextu3jmp nextu4nextu3:jmp near ptr nextunextu4:readcg dh,dlpop cxjmp near ptr endunextu:inc dljmp nextu2chgu2:jmp near ptr cmpu2nextu2:loop chgu2sub dl,79inc dhpop cxjmp nextu1chgu1:jmp near ptr cmpu1nextu1:loop chgu1endu:pop dxdec dhwrite dh,dl,snkjmp near ptr wrt1turndown: ;此处实现蛇向下走push d
23、xmov dh,1mov dl,0mov cx,23cmpd1:push cxmov cx,79cmpd2:readh dh,dlcmp pst,2jne nextd3jmp nextd4nextd3:jmp near ptr nextdnextd4:readcg dh,dlpop cxjmp near ptr enddnextd:inc dljmp nextd2chgd2:jmp near ptr cmpd2nextd2:loop chgd2sub dl,79inc dhpop cxjmp nextd1chgd1:jmp near ptr cmpd1nextd1:loop chgd1endd
24、:pop dxinc dhwrite dh,dl,snkjmp near ptr wrt1turnback: ;此处实现蛇向右走push dxmov dh,1mov dl,0mov cx,23cmpb1:push cxmov cx,79cmpb2:readh dh,dlcmp pst,2jne nextb3jmp nextb4nextb3:jmp near ptr nextbnextb4:readcg dh,dlpop cxjmp near ptr endbnextb:inc dljmp nextb2chgb2:jmp near ptr cmpb2nextb2:loop chgb2sub dl
25、,79inc dhpop cxjmp nextb1chgb1:jmp near ptr cmpb1nextb1:loop chgb1endb:pop dxdec dlwrite dh,dl,snkjmp near ptr wrt1exit:mov ax,0 ;恢复int9中断mov es,axpush ds:0pop es:9*4push ds:2pop es:9*4+2mov ah,4chint 21hint9: ;更改后的中断服务程序push axin al,60hmov adrs,almov al,20hout 20h,alpop axiretDLY PROC near ;延时子程序 P
26、USH CX PUSH DX MOV DX,10000DL1: MOV CX,9801DL2: LOOP DL2 DEC DX JNZ DL1 POP DX POP CX RETDLY ENDPRAND1 PROC PUSH CX PUSH DX PUSH AX STI MOV AH,0 ;读时钟计数器值 INT 1AH MOV AX,DX ;清高6位 AND AH,3 MOV DL,23 ;除23,产生023余数 DIV DL MOV frow,AH ;余数存frow,作随机行数 POP AX POP DX POP CX RETRAND1 ENDPRAND2 PROC PUSH CX PUSH DX PUSH AX STI MOV AH,0 ;读时钟计数器值 INT 1AH MOV AX,DX ;清高6位 AND AH,3 MOV DL,79 ;除79,产生079余数 DIV DL MOV fcol,AH ;余数存fcol,作随机列数 POP AX POP DX POP CX RETRAND2 ENDPCODE ENDS END start-第 20 页-