汇编小程序

相关图例:

MY_CAL

  1 TITLE MY_CAL PROGRRAM TO RUN THE CALCULATION
  2 PAGE 60, 132
  3 ;---------------------------------
  4     .MODEL SMALL
  5     .STACK 64
  6      PUBLIC MY_CAL
  7 ;---------------------------------
  8     .DATA
  9     CAL_MSG1 DB 'Please enter the expression(Q for leave):', '$'
 10     CAL_MSG2 DB 'The result is:', '$'
 11     CAL_MSG3 DB 'ERROR: Only for two operators, and the operators is not negtive', '$'
 12     CAL_MSG4 DB 'ERROR: Mul operator error, a size of operator should be 1', '$'
 13     CAL_MSG5 DB 'ERROR: Div operator error, the size of two operator should be 1 or 2 and 1', '$'
 14     CAL_MSG6 DB 'ERROR: Invalid order', '$'
 15     CAL_OP1 DB 40 DUP(0)
 16     CAL_TMP_LEN DW 0 ;用于保存较大的长度值
 17     CAL_LEN1 DW 0
 18     CAL_OP2 DB 40 DUP(0)
 19     CAL_LEN2 DW 0
 20     CAL_CHA DB ? ;用于保存运算符
 21     CAL_CHA_NUM DB ? ;用于判断输入语法的正确性
 22     CAL_ERROR DB ? ;用于判断输入语法的正确性
 23     CAL_RES DB 80 DUP(0)  ;用于保存结果
 24     CAL_LEN DW 0;结果的长度
 25     CAL_FLAG DB 0
 26     CRLF DB 0DH, 0AH, '$'
 27 ;-----------------------------------------
 28     .CODE
 29 MY_CAL PROC FAR
 30   MC_S0:
 31     MOV AX, @DATA
 32     MOV DS, AX
 33     
 34     LEA DX, CAL_MSG1
 35     MOV AH, 9
 36     INT 21H
 37     
 38     MOV CAL_CHA_NUM, 0;用于保存运算符的个数以判断输入是否合法
 39     CALL INPUT_EXP ;输入表达式
 40     ;----------退出判断------
 41     MOV AL, CAL_FLAG
 42     CMP AL, 'Q'
 43     JNZ MC_S00
 44     MOV AL, 0
 45     MOV CAL_FLAG, AL
 46     RET
 47     ;---------错误处理--------------
 48   MC_S00:
 49     MOV CAL_ERROR, 0
 50     CALL DEAL_ERROR
 51     CMP CAL_ERROR, 0
 52     JZ MC_S01
 53     LEA DX, CRLF
 54     MOV AH, 9
 55     INT 21H
 56     MOV CAL_LEN1, 0
 57     MOV CAL_LEN2, 0
 58     JMP MC_S0
 59     
 60   MC_S01:
 61     ;把两个操作数处理成一样等长的数据,高位补0
 62     ;把长的长度保存在AX中
 63     MOV AX, CAL_LEN1
 64     CMP AX, CAL_LEN2
 65     JG MC_S1
 66     MOV AX, CAL_LEN2  
 67   MC_S1:
 68     ;已经有AX里面保存了最长的长度, BX中有自己的长度,去把DI指向的数据高位补O成长度为AX的
 69     MOV CAL_TMP_LEN, AX
 70     
 71     LEA DI, CAL_OP1
 72     MOV BX, CAL_LEN1
 73     CALL DEL_OP
 74     
 75     LEA DI, CAL_OP2
 76     MOV BX, CAL_LEN2
 77     CALL DEL_OP
 78 
 79     ;选择
 80     
 81   CAL_START:
 82     MOV AL, CAL_CHA
 83     CMP AL, '+'
 84     JZ CAL_ADD
 85     CMP AL, '-'
 86     JZ CAL_SUB
 87     CMP AL, '*'
 88     JZ CAL_MUL
 89     CMP AL, '/'
 90     JZ CAL_DIV
 91     
 92     LEA DX, CRLF
 93     MOV AH, 9
 94     INT 21H
 95     
 96     LEA DX, CAL_MSG6
 97     MOV AH, 9
 98     INT 21H
 99     MOV AX, 0
100     MOV CAL_LEN1, AX
101     MOV CAL_LEN2, AX
102     MOV CAL_FLAG, AL
103     MOV CAL_CHA, AL
104     LEA DX, CRLF
105     MOV AH, 9
106     INT 21H
107     JMP MC_S0
108     
109   CAL_ADD:
110     CALL MY_ADD
111     JMP CAL_OUTPUT_RES
112     
113   CAL_SUB:
114     CALL MY_SUB
115     JMP CAL_OUTPUT_RES
116   
117   CAL_MUL:
118     CALL MY_MUL
119     JMP CAL_OUTPUT_RES
120     
121   CAL_DIV:
122     CALL MY_DIV
123     JMP CAL_OUTPUT_RES
124     
125   CAL_OUTPUT_RES:
126     LEA DX, CRLF
127     MOV AH, 9
128     INT 21H
129     
130     LEA DX, CAL_MSG2
131     MOV AH, 9
132     INT 21H
133 
134     CALL OUTPUT_RES
135   MC_S2:
136     LEA DX, CRLF
137     MOV AH, 9
138     INT 21H
139     
140     CALL CAL_RESET;全部数据归零
141     JMP MC_S0
142     
143 MY_CAL ENDP
144 ;********************DEAL_ERROR*****
145 DEAL_ERROR PROC NEAR
146     ;------------------运算符过多或是无运算符------------------
147     MOV AX, 1
148     CMP CAL_CHA_NUM, 1
149     JZ DE_S0
150     
151     LEA DX, CRLF
152     MOV AH, 9
153     INT 21H
154     
155     LEA DX, CAL_MSG3
156     MOV AH, 9
157     INT 21H
158 
159     INC CAL_ERROR
160     RET
161   DE_S0:
162     CMP CAL_CHA, '*'
163     JZ MUL_CASE
164     CMP CAL_CHA, '/'
165     JZ DIV_CASE
166     RET
167     ;----要有一个长度为1的数
168   MUL_CASE:
169     CMP CAL_LEN1, 1
170     JNZ MUL_CASE_1
171     RET
172     
173   MUL_CASE_1:
174     CMP CAL_LEN2, 1
175     JNZ MUL_CASE_ERROR
176     RET
177   MUL_CASE_ERROR:
178      LEA DX, CRLF
179      MOV AH, 9
180      INT 21H
181      LEA DX, CAL_MSG4
182      MOV AH, 9
183      INT 21H
184      INC CAL_ERROR
185      RET
186     
187   DIV_CASE:
188     CMP CAL_LEN1, 1
189     JZ DIV_CASE_1
190     CMP CAL_LEN1, 2
191     JZ DIV_CASE_1
192     JMP DIV_CASE_ERROR
193     
194   DIV_CASE_1:
195     CMP CAL_LEN2, 1
196     JNZ DIV_CASE_ERROR
197     RET
198   DIV_CASE_ERROR:
199      LEA DX, CRLF
200      MOV AH, 9
201      INT 21H
202      LEA DX, CAL_MSG5
203      MOV AH, 9
204      INT 21H
205      INC CAL_ERROR
206      RET
207 DEAL_ERROR ENDP
208 ;*******************INPUT_EXP**********
209 ;函数说明,把运算式输入并交操作符保存在CHA中,两个操作数保存在 OP1和OP2中,对应的长度保存在LEN1和LEN2中
210 INPUT_EXP PROC NEAR
211     LEA DI, CAL_OP1
212     LEA SI, CAL_LEN1
213   IE_S1:
214     MOV AH, 1
215     INT 21H
216     CMP AL, 'Q'
217     JNZ IE_S11
218     MOV CAL_FLAG, AL
219     RET
220   IE_S11:
221     CMP AL, 0DH ;
222     JNZ NOT_END
223     MOV [DI], '$'
224     RET
225     
226   NOT_END:
227     CMP AL, '0'
228     JB IE_S2;有运算符进来了 ,换第二个数输入
229     MOV [DI], AL
230     INC DI
231     INC BYTE PTR [SI]
232     JMP IE_S1
233   IE_S2:
234     MOV [DI], '$'
235     MOV CAL_CHA, AL
236     INC CAL_CHA_NUM
237     LEA DI, CAL_OP2
238     LEA SI, CAL_LEN2
239     JMP IE_S1
240     
241 INPUT_EXP ENDP
242 
243 ;***************************CAL_INPUT*****************
244 ;已经有AX里面保存了最长的长度, BX中有自己的长度,去把DI指向的数据高位补O成长度为AX的
245 DEL_OP PROC NEAR
246     CMP AX, BX
247     JZ DO_S
248 
249     ;处理OP 定位
250     MOV SI, DI;指向同一个数据数组
251     ADD SI, BX
252     DEC SI
253     ADD DI, AX
254     MOV [DI], '$'
255     DEC DI
256     MOV CX, BX
257   DO_S1:
258     MOV DL, [SI]
259     MOV [DI], DL
260     DEC DI
261     DEC SI
262     LOOP DO_S1
263     
264     ;高位补0
265     MOV CX, AX
266     SUB CX, BX
267     MOV BL, '0'
268   DO_S3:
269     MOV [DI], BL   ;这里绝对不能使用MOV [DI], '0'不然只会把自己弄死
270     DEC DI
271     LOOP DO_S3
272     
273   DO_S:
274     RET
275 DEL_OP ENDP
276 ;**********************RESET********************
277 CAL_RESET PROC NEAR
278     MOV CX, CAL_TMP_LEN
279     LEA DI, CAL_OP1
280     LEA SI, CAL_OP2
281   RS_S0:
282     MOV AL, 0
283     MOV [DI], AL
284     MOV [SI], AL
285     INC SI
286     INC DI
287     LOOP RS_S0
288     
289     MOV CX, CAL_LEN
290     LEA SI, CAL_RES
291     RS_S1:
292     MOV AL, 0
293     MOV [SI], AL
294     INC SI
295     LOOP RS_S1
296     
297     MOV AX, 0
298     MOV CAL_LEN1, AX
299     MOV CAL_LEN2, AX
300     MOV CAL_LEN, AX
301     MOV CAL_TMP_LEN, AX
302     MOV CAL_FLAG, AL
303     MOV CAL_CHA, AL
304     RET
305 CAL_RESET ENDP
306 ;****************************OUTPUT_RES***********
307  OUTPUT_RES PROC NEAR
308     MOV CX, CAL_LEN
309     LEA SI, CAL_RES
310     ADD SI, CAL_LEN
311     DEC SI
312   OR_S:
313     MOV DL, [SI]
314     MOV AH, 2
315     INT 21H
316     DEC SI
317     LOOP OR_S
318     RET
319 OUTPUT_RES ENDP
320 ;********************************MY_ADD**************
321 ;参数说明, OP1,OP2 为操作九,RES为和, LEN1, LEN2为长度 
322 MY_ADD PROC NEAR
323     MOV AX, CAL_TMP_LEN
324     MOV CAL_LEN, AX
325     
326     LEA SI, CAL_OP1
327     ADD SI, CAL_TMP_LEN
328     DEC SI
329     LEA DI, CAL_OP2
330     ADD DI, CAL_TMP_LEN
331     DEC DI
332     LEA DX, CAL_RES
333     
334     MOV CX, CAL_TMP_LEN
335     XOR AX, AX
336     PUSHF
337     
338     MA_S1:
339     MOV AL, [SI]
340     SUB AL, '0'
341     MOV BL, [DI]
342     SUB BL, '0'
343     POPF
344     ADC AL, BL
345     AAA
346     PUSHF ;保存进位信息
347     ADD AL, '0'
348     MOV [SI], AL
349     
350     ;汇编就是个坑
351     PUSH SI
352     MOV SI, DX
353     MOV [SI], AL
354     POP SI
355     
356     INC DX
357     DEC DI
358     DEC SI
359     LOOP MA_S1
360     
361     ;判断最后有没有进位
362     DEC DX;减去前面多减的
363     MOV SI, DX
364     XOR AL, AL
365     POPF
366     ADC AL,0
367     CMP AL,0
368     JZ MA_S2
369     INC CAL_LEN
370     INC SI
371     ADD AL,'0'
372     MOV [SI], AL
373   MA_S2:
374     RET
375     
376 MY_ADD ENDP
377 ;*********************MY_SUB*********************
378 MY_SUB PROC NEAR
379     MOV AX, CAL_TMP_LEN
380     MOV CAL_LEN, AX
381     
382     LEA SI, CAL_OP1
383     ADD SI, CAL_TMP_LEN
384     DEC SI
385     LEA DI, CAL_OP2
386     ADD DI, CAL_TMP_LEN
387     DEC DI
388     LEA DX, CAL_RES
389     
390     MS_S:
391     MOV CX, CAL_TMP_LEN
392     XOR AX, AX
393     PUSHF
394     
395     MS_S1:
396     MOV AL, [SI]
397     SUB AL, '0'
398     MOV BL, [DI]
399     SUB BL, '0'
400     POPF
401     SBB AL, BL
402     AAS
403     PUSHF
404     ADD AL, '0'
405     MOV [SI], AL
406     
407     ;汇编就是个坑
408     PUSH SI
409     MOV SI, DX
410     MOV [SI], AL
411     POP SI
412     
413     INC DX
414     DEC DI
415     DEC SI
416     LOOP MS_S1
417     
418     ;判断最后的借位
419     DEC DX;减去前面多减的
420     MOV SI, DX
421     XOR AL, AL
422     POPF
423     SBB AL,0
424     CMP AL,0
425     JZ MS_S2
426     INC CAL_LEN
427     INC SI
428     ADD AL,'0'
429     MOV [SI], AL
430   MS_S2:
431     RET
432     
433 MY_SUB ENDP
434 ;**************************************MY_MUL************
435 MY_MUL PROC NEAR
436     MOV AX, CAL_TMP_LEN
437     MOV CAL_LEN, AX
438     ;判断哪个是大数,比较长的放在SI中
439     CMP AX, CAL_LEN1
440     JNZ MM_S
441     LEA SI, CAL_OP2
442     ADD SI, CAL_TMP_LEN
443     DEC SI
444     MOV BL, [SI]
445     SUB BL, '0' 
446     LEA SI, CAL_OP1
447     ADD SI, CAL_TMP_LEN
448     DEC SI
449     JMP MM_S00
450   MM_S:
451     LEA SI, CAL_OP1
452     ADD SI, CAL_TMP_LEN
453     DEC SI
454     MOV BL, [SI]
455     SUB BL, '0' 
456     LEA SI, CAL_OP2
457     ADD SI, CAL_TMP_LEN
458     DEC SI
459 
460   MM_S00:
461     LEA DI, CAL_RES
462     XOR AX, AX
463     XOR BH, BH
464     MOV CX, CAL_TMP_LEN
465   MM_S0:
466     MOV AL, [SI]
467     SUB AL, '0'
468     MUL BL
469     ADD AL, BH;加上进位
470     AAM
471     ADD AL, '0'
472     MOV [DI], AL
473     MOV BH, AH;保存进位
474     DEC SI
475     INC DI
476     LOOP MM_S0
477     
478     ;判断有没有再进位
479     MOV AL, 0
480     CMP BH, AL
481     JZ MM_S1
482     INC CAL_LEN
483     ADD BH, '0'
484     MOV [DI], BH
485     INC DI
486     MM_S1:
487     MOV [DI], '$'
488     RET
489 MY_MUL ENDP
490 ;*****************************MY_DIV**************
491 MY_DIV PROC NEAR
492     MOV AX, CAL_TMP_LEN
493     MOV CAL_LEN, AX
494     
495     MOV AX, WORD PTR CAL_OP1
496     SUB AL, '0'
497     SUB AH, '0'
498     XCHG AL, AH
499     
500     AAD
501     LEA SI, CAL_OP2
502     ADD SI, CAL_TMP_LEN
503     DEC SI
504     MOV BL, [SI]
505     SUB BL, '0'
506     DIV BL
507     AAM
508     LEA DI, CAL_RES
509     ADD AL, '0'
510     MOV [DI], AL
511     INC DI
512     ADD AH, '0'
513     MOV [DI], AH
514     RET
515 MY_DIV ENDP
516 ;******************************************
517     END
View Code

MY_GAME

  1 TITLE MY_GAME PROGRRAM TO RUN THE LITTLE GAME
  2 PAGE 60, 132
  3 ;---------------------------------
  4     EXTRN CLS:FAR
  5     .MODEL SMALL
  6     .STACK 64
  7      PUBLIC MY_GAME
  8 ;---------------------------------
  9     .DATA
 10     GAME_COUNT DW 0
 11     ROWS DW 4
 12     COLS DW 4
 13     GAME_START DB 'The init game is follows:', '$'
 14     GAME_MSG DB 'A,W,S,D for direction and  Q to leave: ', '$'
 15     GAME_MSG1 DB 'The result is follows:', '$'
 16     GAME_MSG2 DB 'The next order: ' , '$'
 17     GAME_MSG3 DB 'Wrong order, please check you order!!', '$'
 18     GAME_MSG4 DB 'COUNT: ', '$'
 19     MSG_END_GAME DB 'THE GAME IS ENDING!!!!!!', '$'
 20     CRLF DB 0DH, 0AH, '$'
 21     GAME_DIRECTION DB 0
 22     GAME_BOUND1 DB '---------------------------------', '$'
 23     GAME_BOUND2 DB '|'
 24     GAME_ARRAY DB  16 DUP ('$')  ; STORE the station of the chess
 25     GAME_BLANK DB '     ', '$'; for the output
 26     GAME_QUEUE DB 4 DUP (0)
 27     COUNT_QUEUE DW 0
 28     GAME_TMEP DB 4 DUP ('$') ;用于暂时保存数据 
 29     COUNT_NEW DW 0 ;记录GAME_TEMP的长度
 30 ;----------------------------------------
 31     .CODE
 32     MY_GAME PROC NEAR
 33     MOV AX, @DATA
 34     MOV DS, AX
 35     
 36     LEA DX, GAME_START
 37     MOV AH, 9
 38     INT 21H
 39     
 40     LEA DX, CRLF
 41     MOV AH, 9
 42     INT 21H
 43     
 44     CALL INIT_ARRAY ;初始化2048
 45     
 46     CALL GAME_OUTPUT
 47     
 48     LEA DX, GAME_MSG
 49     MOV AH, 9
 50     INT 21H
 51     
 52   S0:
 53     MOV AH, 1
 54     INT 21H
 55     MOV GAME_DIRECTION, AL ;用于保存输入的方向
 56     MOV BL, GAME_DIRECTION
 57     
 58     CMP BL, 'A'
 59     JNZ M1
 60     CALL MOVELEFT
 61     JMP S
 62     
 63   M1:
 64     
 65     CMP BL, 'S'
 66     JNZ M2
 67     CALL MOVEDOWN
 68     JMP S
 69     
 70   M2:
 71     CMP BL, 'D'
 72     JNZ M3 
 73     CALL MOVERIGHT
 74     JMP S
 75     
 76   M3:
 77     
 78     CMP BL, 'W'
 79     JNZ M4
 80     CALL MOVEUP
 81     JMP S
 82   M4:
 83     
 84     CMP BL, 'Q' ;手动结束游戏
 85     JNZ M5
 86     JZ S1
 87     
 88   M5: ;错误命令
 89     LEA DX, CRLF
 90     MOV AH, 9
 91     INT 21H
 92     LEA DX, CRLF
 93     MOV AH, 9
 94     INT 21H
 95     LEA DX, GAME_MSG3
 96     MOV AH, 9
 97     INT 21H
 98     
 99     LEA DX, CRLF
100     MOV AH, 9
101     INT 21H
102     
103     JMP S
104   S:
105     CALL CLS
106     CALL GAME_OUTPUT ;显示游戏界面
107     LEA DX, GAME_MSG2
108     MOV AH, 9
109     INT 21H
110     JMP S0
111  S1: 
112     LEA DX, CRLF
113     MOV AH, 9
114     INT 21H
115     
116     LEA DX, MSG_END_GAME
117     MOV AH,9
118     INT 21H
119     
120     MOV AX, 0
121     MOV GAME_COUNT, AX
122     
123     MOV AH, 1
124     INT 21H
125     
126     RET
127 MY_GAME ENDP
128 
129 ;**********************INIT_ARRA*****************************
130 INIT_ARRAY PROC NEAR
131     MOV CX, 16
132     LEA SI, GAME_ARRAY
133     MOV AL, 0
134   IA_SO:
135     MOV [SI], AL
136     INC SI
137     LOOP IA_SO
138     
139     LEA SI, GAME_ARRAY
140     MOV [SI], 1
141     MOV [SI+1], 2
142     MOV [SI+2], 4
143     MOV [SI+7], 3
144     MOV [SI+8], 1
145     ;MOV [SI+10], 1
146     MOV [SI+13], 2
147     MOV [SI+5], 3
148     RET
149 INIT_ARRAY ENDP
150 
151 ;**********************MOVELEFT*****************************
152 MOVELEFT PROC NEAR
153     MOV CX, ROWS
154   ML_ROW:
155   ; 确定要送入队列的数据的首地址
156     LEA SI, GAME_ARRAY
157     MOV AX, ROWS
158     SUB AX, CX
159     MUL COLS
160     ADD SI, AX   ;当前确定行循环首地址
161     
162     PUSH CX
163     MOV CX, COLS
164     AND COUNT_QUEUE, 0
165     LEA DI, GAME_QUEUE ;队列
166   ML_COL: ;进队列
167     MOV AL, [SI]  ;好坑啊,为什么 用MOV AL, O; CMP [SI], AL就错了
168     CMP AL, 0
169     JZ ML_G0
170     
171     ;;将[SI]中的数据放入到队列中
172     MOV AL, [SI]
173     MOV [DI], AL
174     
175     INC DI
176     INC COUNT_QUEUE
177     MOV AL, 0
178     AND [SI], AL ;清零
179   ML_G0: 
180     INC SI
181     LOOP ML_COL
182     
183     ;把队列的数字进行运算并暂时放入到GAME_TEMP
184     AND COUNT_NEW, 0
185     CALL CAL_NEW_DATA
186     
187     
188     ;把新数据写回QUEUE_ARRAY
189     MOV AX, COUNT_NEW
190     CMP AX, 0
191     JZ ML_G3
192     ;计算要把数据从哪里开始
193     POP BX ;得到外循环的CX
194     PUSH BX 
195     MOV AX, ROWS 
196     SUB AX, BX ;因为数据不会太大,AH, BH都为0 
197     MUL COLS
198     LEA DI, GAME_ARRAY;
199     ADD DI, AX ;AL有偏移量,加上就是要存放的起始地址
200     
201     MOV CX, COUNT_NEW
202     LEA BX, GAME_TMEP
203   ML_G5:
204     MOV AL, [BX]
205     MOV [DI], AL
206     INC DI
207     INC BX
208     LOOP ML_G5
209   ML_G3:
210     POP CX
211     LOOP ML_ROW
212     
213     INC GAME_COUNT
214     RET
215 MOVELEFT ENDP
216 ;**********************MOVERIGHTP*****************************
217 MOVERIGHT PROC NEAR
218     MOV CX, ROWS
219   MR_ROW:
220     LEA SI, GAME_ARRAY
221     MOV AX, ROWS
222     SUB AX, CX
223     MUL COLS
224     ADD SI, AX
225     MOV AX, COLS
226     ADD SI, AX
227     DEC SI
228     
229     LEA DI, GAME_QUEUE ;队列
230     AND COUNT_QUEUE, 0
231     PUSH CX
232     MOV CX, COLS
233   MR_COL: ;进队列
234     MOV AL, [SI]
235     CMP AL, 0 
236     JZ MR_G0
237     
238     MOV AL, [SI] 
239     MOV [DI], AL    
240     INC DI
241     INC COUNT_QUEUE ;用于记录队列的长度
242     ;AND [SI], 0
243     MOV AL, 0
244     AND [SI], AL 
245   MR_G0:
246     DEC SI
247     LOOP MR_COL
248     
249     ;问题怎么决断边界地址啊,怎么判断队列是空的情况,用纪录的方式记下个数再后期写回
250     ;遍历队列,并把新数据放回GAME_TEMP对应的行中()
251     AND COUNT_NEW, 0
252     CALL CAL_NEW_DATA
253     
254     MOV AX, COUNT_NEW
255     CMP AX, 0
256     JZ MR_G3
257     
258     ;把新数据写回QUEUE_ARRAY
259     ;计算从哪里开始放数据,怎么放
260     POP BX ;得到外循环已经多少了
261     PUSH BX 
262     LEA DI, GAME_ARRAY
263     MOV AX, ROWS
264     SUB AX, BX
265     MUL COLS
266     ADD DI, AX
267     MOV AX, COLS
268     ADD DI, AX
269     DEC DI
270     
271     MOV CX, COUNT_NEW
272     LEA BX, GAME_TMEP
273   MR_G5:
274     MOV AL, [BX]
275     MOV [DI], AL
276     DEC DI
277     INC BX
278     LOOP MR_G5
279   
280   MR_G3:
281     POP CX
282     LOOP MR_ROW
283     
284     INC GAME_COUNT
285     RET
286 MOVERIGHT ENDP
287 ;**********************MOVEUP*****************************
288 MOVEUP PROC NEAR
289     MOV CX, COLS
290   MU_COL:
291     LEA DI, GAME_QUEUE ;队列
292     LEA SI, GAME_ARRAY  ;当前循环首地址定位 SI = GAME_ARRAY + 4 - ROW(CX) ,后面列循环的元素每个加4
293     ADD SI, COLS
294     SUB SI, CX
295     
296     AND COUNT_QUEUE, 0
297     PUSH CX
298     MOV CX, ROWS
299   MU_ROW: ;进队列
300     MOV AL, [SI]
301     CMP AL, 0
302     JZ MU_G0
303     MOV AL, [SI] 
304     MOV [DI], AL
305     MOV AL, 0
306     MOV [SI], AL
307     INC DI
308     INC COUNT_QUEUE ;用于记录队列的长度
309   MU_G0:
310     ADD SI, 4
311     LOOP MU_ROW
312     
313     ;问题怎么决断边界地址啊,怎么判断队列是空的情况,用纪录的方式记下个数再后期写回
314     ;遍历队列,并把新数据放回GAME_TEMP对应的行中()
315     AND COUNT_NEW, 0
316     CALL CAL_NEW_DATA
317     
318     MOV AX, COUNT_NEW
319     CMP AX, 0
320     JZ MU_G3
321     ;把新数据写回QUEUE_ARRAY
322     
323     ;计算要把数据从哪里开始放 SI+4-row(cx),每次要加上4
324     POP BX ;得到外循环已经多少了
325     PUSH BX 
326     MOV AX, COLS
327     SUB AX, BX
328     LEA DI, GAME_ARRAY
329     ADD DI , AX
330     MOV CX, COUNT_NEW
331     LEA BX, GAME_TMEP
332   MU_G5:
333     MOV AL, [BX]
334     MOV [DI], AL
335     ADD DI, 4
336     INC BX
337     LOOP MU_G5
338   
339   MU_G3:
340     POP CX
341     LOOP MU_COL
342     
343     INC GAME_COUNT
344     RET
345 MOVEUP ENDP
346 ;**********************MOVEDOWN*****************************
347 MOVEDOWN PROC NEAR
348     MOV CX, COLS
349   MD_ROW:
350   
351     LEA DI, GAME_QUEUE ;队列
352     LEA SI, GAME_ARRAY  ;当前循环首地址定位 SI = AGME_ARRAY + (ROWS - 1) * COLS,+ (ROWS - ROW(CX)),后面列循环的元素每个-4
353     ADD SI, 12 ;(ROWS-1) * COLS  = 12 
354     ADD SI, COLS
355     SUB SI, CX
356     PUSH CX
357     MOV CX, ROWS
358     AND COUNT_QUEUE, 0
359     
360   MD_COL: ;进队列
361     MOV AL, 0
362     CMP [SI], AL
363     JZ MD_G0
364     MOV AL, [SI]
365     MOV [DI], AL
366     ;AND [SI], 0 清零
367     MOV AL, 0
368     MOV [SI], AL
369     INC DI
370     INC COUNT_QUEUE ;用于记录队列的长度
371   MD_G0:
372     SUB SI, 4
373     LOOP MD_COL
374     ;运用队列运算并把运算结果放到GAME_TEMP中
375     AND COUNT_NEW, 0
376     CALL CAL_NEW_DATA
377     
378     MOV AX, COUNT_NEW
379     CMP AX, 0
380     JZ MD_G3
381     
382     ;把新数据写回QUEUE_ARRAY
383     ;计算要把数据从哪里开始放 SI + (ROWS-1)*COLS+COLS-COL(CX),后面每次要-4
384     POP BX ;得到外循环已经多少了
385     PUSH BX 
386     MOV AX, COLS
387     SUB AX, BX
388     LEA DI, GAME_ARRAY
389     ADD DI, 12
390     ADD DI, AX
391     MOV CX, COUNT_NEW
392     LEA BX, GAME_TMEP
393   MD_G5:
394     MOV AL, [BX]
395     MOV [DI], AL
396     SUB DI, 4
397     INC BX
398     LOOP MD_G5
399   
400   MD_G3:
401     POP CX
402     LOOP MD_ROW
403     
404     INC GAME_COUNT
405     RET
406 MOVEDOWN ENDP
407 ;==========
408 GAME_UPDATE PROC NEAR
409     MOV AX, ROWS
410     MOV BX, COLS
411     MUL BL ;得到ROWS * COLS
412     MOV CX, AX
413   
414     LEA SI, GAME_ARRAY
415   GU_S0:
416     CMP SI, 0
417     JZ GU_S1
418     JNZ GU_S2
419   GU_S1:
420     MOV AL, 2
421     MOV [SI], AL
422     RET
423   GU_S2:
424     INC SI
425     LOOP GU_S0
426     ;没有办法更新数据
427     RET
428 GAME_UPDATE ENDP
429 ;**********************CAL_NEW_DATA*****************************
430 GAME_OUTPUT PROC NEAR
431     LEA DX, CRLF
432     MOV AH, 9
433     INT 21H    
434     
435     LEA DX, GAME_MSG4
436     MOV AH, 9
437     INT 21H
438     
439     MOV AX, GAME_COUNT
440     CALL OUTPUT_VALUE_G
441     
442     LEA DX, CRLF
443     MOV AH, 9
444     INT 21H
445     
446     LEA DX, GAME_MSG1
447     MOV AH, 9
448     INT 21H
449     
450     LEA DX, CRLF
451     MOV AH, 9
452     INT 21H
453     
454     LEA DX, GAME_BOUND1
455     MOV AH, 9
456     INT 21H
457     
458     LEA DX, CRLF
459     MOV AH, 9
460     INT 21H
461     
462     MOV CX,ROWS
463     LEA SI, GAME_ARRAY
464     
465    GO_S0:
466     MOV DL, GAME_BOUND2
467     MOV AH,2
468     INT 21H
469     
470     PUSH CX
471     MOV CX, COLS
472   GO_S1:
473     LEA DX, GAME_BLANK
474     MOV AH, 9
475     INT 21H
476     
477     MOV AL, [SI]
478     INC SI
479     CALL OUTPUT_VALUE_S
480     
481     MOV DL, GAME_BOUND2
482     MOV AH, 2
483     INT 21H
484     
485     LOOP GO_S1
486     POP CX
487     
488     LEA DX, CRLF
489     MOV AH, 9
490     INT 21H
491     
492     LOOP GO_S0
493     
494     LEA DX, GAME_BOUND1
495     MOV AH, 9
496     INT 21H
497     
498     LEA DX, CRLF
499     MOV AH, 9
500     INT 21H
501     RET
502 GAME_OUTPUT ENDP
503 ;**********************CAL_NEW_DATA*****************************
504 CAL_NEW_DATA PROC NEAR
505 ; DEBUG
506     ;MOV DX, COUNT_QUEUE
507     ;ADD DL, '0'
508     ;MOV AH, 2
509     ;INT 21H
510     
511     MOV CX, COUNT_QUEUE
512     CMP CX, 0 ;队列为空
513     JZ G5
514     LEA DI, GAME_QUEUE
515     LEA BX, GAME_TMEP
516   G1:
517     MOV AL, [DI]  
518     MOV [BX], AL ;PUSH
519     INC COUNT_NEW ;用于纪录有多少个新的数据
520     INC DI
521     DEC CX   ;先行减1,用于提前判断是否为空
522     CMP CX,0 ;本次迭代结束后,队列为空
523     JZ G5
524     MOV AL, [BX]
525     CMP [DI], AL ;判断可不可以进行叠加
526     JNZ G4
527     MOV AL, [BX]
528     ADD [BX], AL
529     INC DI
530     DEC CX  ;POP
531   G4: 
532     INC CX ;补加之前减去的1
533     INC BX
534     LOOP G1 ;POP + FOR
535   G5:
536     RET
537 CAL_NEW_DATA ENDP
538 ;**********************OUTPUT_VALUE_S*****************************
539 ;输出保存在AL中的数字,v如果是0则输出两个空格,如果是一数字则输出一个空格
540 OUTPUT_VALUE_S PROC NEAR
541     MOV DL, AL
542     CMP DL, 0
543     JNZ OVS_S1
544     MOV DL, ' '
545     MOV AH, 2
546     INT 21H
547     MOV AH, 2
548     INT 21H
549     RET
550   OVS_S1:
551     MOV AL, DL
552     MOV AH, 0
553     MOV BL, 10
554     DIV BL
555     MOV BL, AH
556     CMP AL, 0
557     JZ OVS_S2
558     MOV DL, AL
559     ADD DL, '0'
560     MOV AH, 2
561     INT 21H
562     JMP OVS_S21
563   OVS_S2:
564     MOV DL, ' '
565     MOV AH, 2
566     INT 21H
567   OVS_S21:
568     CMP BL, 0
569     JZ OVS_S3
570     MOV DL, BL
571     ADD DL, '0'
572     MOV AH,2
573     INT 21H
574     RET
575   OVS_S3:
576     MOV DL, ' '
577     MOV AH, 2
578     INT 21H
579     RET
580 OUTPUT_VALUE_S ENDP
581 ;**********************OUTPUT_VALUE*****************************
582 ;输出保存在AX中的数字
583 OUTPUT_VALUE PROC NEAR
584     MOV BL, 10
585     DIV BL
586     
587     MOV BH, AH ;保存余数
588     
589     MOV DL, AL
590     CMP DL, 0
591     JZ OV_S1  ;如果十位为0不输出
592     ADD DL, '0'
593     MOV AH, 2
594     INT 21H
595     
596   OV_S1:
597     MOV DL, BH ;得到个位
598     ADD DL, '0'
599     MOV AH, 2
600     INT 21H
601     RET
602 OUTPUT_VALUE ENDP
603 ;**********************OUTPUT_VALUE_G*****************************
604 OUTPUT_VALUE_G PROC NEAR
605 ; 输出 AL中的数字,可能有两位; 
606     MOV BL, 10
607     DIV BL
608     
609     MOV BH, AH ;保存余数
610     
611     MOV DL, AL
612     CMP DL, 0
613     JZ OL_S1  ;如果十位为0不输出
614     ADD DL, '0'
615     MOV AH, 2
616     INT 21H
617     
618   OL_S1:
619     MOV DL, BH ;得到个位
620     ADD DL, '0'
621     MOV AH, 2
622     INT 21H
623     RET
624 OUTPUT_VALUE_G ENDP
625 ;***************************************************
626     END
627     
View Code

MY_PASSWORD_TEST

  1 TITLE PASSWORD_TEST PROGRRAM TO CHECK THE PASSWORD
  2 PAGE 60, 132
  3 ;---------------------------------
  4     .MODEL SMALL
  5     .STACK 64
  6      PUBLIC PASSWORD_TEST
  7 ;---------------------------------
  8     .DATA
  9     PT_MSG1 DB "Please input your password:", '$'
 10     PT_MSG2 DB "Password wrong, try agagin", '$'
 11     PASSWORD DB "QINKAI", '$'
 12     PASSWORD_LEN DB 6
 13     TEST_PASS DB 30 DUP ('$')
 14     TEST_PASS_LEN DB 0
 15     PT_FLAG DB 0
 16     CRLF DB 0DH, 0AH,'$'
 17 ;----------------------------------------
 18     .CODE
 19 PASSWORD_TEST PROC FAR
 20 PT_SO:
 21     MOV AX, @DATA
 22     MOV DS, AX
 23     MOV ES, AX
 24     
 25     LEA DX, PT_MSG1
 26     MOV AH, 9
 27     INT 21H
 28     
 29     ; 输入密码
 30     LEA DI, TEST_PASS
 31     CALL INPUT_PASSWORD
 32     
 33     LEA DX,CRLF
 34     MOV AH, 9
 35     INT 21H
 36     
 37     ;测试密码 否正确
 38     LEA DI, PASSWORD
 39     LEA SI, TEST_PASS
 40     MOV AL, PASSWORD_LEN
 41     MOV AH, TEST_PASS_LEN
 42     MOV TEST_PASS_LEN, 0
 43     MOV PT_FLAG, 0 ;为0代表 正确的
 44     CALL IS_RIGHT
 45     CMP PT_FLAG, 0
 46     
 47     LEA DX,CRLF
 48     MOV AH, 9
 49     INT 21H
 50     
 51     JNZ PT_SO
 52     
 53     RET
 54 PASSWORD_TEST ENDP
 55 
 56 ;*************************************INPUT_PASSWORD*********************************************
 57 INPUT_PASSWORD PROC NEAR
 58     S2:
 59     MOV AH, 8
 60     INT 21H
 61     CMP AL, 0DH ; 判断是不是输入换行符
 62     JNZ IP_S3
 63     MOV AH, '$'
 64     MOV [DI], AH
 65     RET
 66     
 67   IP_S3:
 68     MOV [DI], AL;[si]是当前地址,相当于c语言中的*pt = value; 
 69     INC DI
 70     INC [TEST_PASS_LEN]
 71     MOV DL, '*'
 72     MOV AH, 2
 73     INT 21H
 74     JMP S2
 75     
 76 INPUT_PASSWORD ENDP
 77 
 78 ;*********************************IS_RIGHT*********************************
 79 IS_RIGHT PROC NEAR
 80     CMP AL, AH
 81     JZ S3
 82     LEA DX, PT_MSG2 ;长度不相等
 83     MOV AH, 9
 84     INT 21H
 85     MOV PT_FLAG, 1
 86     RET
 87     
 88      S3:
 89     MOV CL, PASSWORD_LEN
 90     MOV CH, 0
 91     
 92     REPE CMPSB
 93     JNZ S5
 94     RET
 95   S5:
 96     ;error
 97     LEA DX, PT_MSG2
 98     MOV AH,9
 99     INT 21H
100     MOV PT_FLAG, 1
101     RET
102 
103 IS_RIGHT ENDP
104 ;***********************************
105     END
View Code

MY_MAIN

  1 TITLE MAIN PROGRRAM TO CONTROL THE SYSTEM
  2 PAGE 60, 132
  3     EXTRN PASSWORD_TEST:FAR
  4     EXTRN MY_GAME:FAR
  5     EXTRN MY_CAL:FAR
  6     PUBLIC CLS
  7 ;---------------------------------------------
  8     .MODEL SMALL
  9     .STACK 64
 10 ;------------------------------------------------
 11     .DATA
 12     MAIN_MSG1 DB 'Welcome to System!', '$'
 13     MAIN_MSG3 DB 'Password wrong try again', '$'
 14     MAIN_MSG4 DB 'Please choose your item to continue', '$'
 15     MAIN_MSG5 DB '1: Little game', '$'
 16     MAIN_MSG6 DB '2: Calculation', '$'
 17     MAIN_MSG7 DB 'YOUR CHOISE:', '$'
 18     MAIN_FLAG DB 0
 19     CRLF DB 0DH, 0AH,'$'
 20 ;----------------------------------
 21     .CODE
 22 START:
 23     MOV AX, @DATA
 24     MOV DS, AX
 25     CALL CLS ;清屏
 26     
 27     MOV CX, 2
 28     CALL CRLF_FUNC
 29     LEA DX, MAIN_MSG1
 30     MOV AH, 9
 31     INT 21H
 32     
 33     LEA DX, CRLF
 34     MOV AH, 9
 35     INT 21H
 36     
 37     CALL PASSWORD_TEST ;调用密码判断功能可以跳出这个函数就可以往下运行
 38     
 39     MAIN_S:
 40     CALL MAIN_OUTPUT ;调用系统界面
 41     
 42     ;输入选择
 43     MOV AH, 1
 44     INT 21H
 45     
 46     PUSH AX;保存命令,防止调用函数时改变了AL
 47     CMP AL, '1' ;游戏
 48     JNZ MAIN_S0
 49     CALL CLS
 50     CALL MY_GAME
 51 
 52     
 53   MAIN_S0:
 54     CMP AL, '2' ;计算器
 55     JNZ MAIN_S1
 56     CALL CLS
 57     CALL MY_CAL
 58     
 59   MAIN_S1:
 60     POP AX
 61     CMP AL, 'Q'
 62     JNZ MAIN_S
 63     
 64     MOV AH,4CH
 65     INT 21H
 66     RET
 67 ;***************************CLS*************************
 68 CLS PROC FAR
 69     MOV AH, 0FH
 70     INT 10H
 71     MOV AH, 0
 72     INT 10H
 73     RET
 74 CLS ENDP
 75 ;***************************CRLF_FUNC***************************
 76 ;按CX来进行输空行
 77 CRLF_FUNC PROC NEAR
 78   SO:
 79     LEA DX, CRLF
 80     MOV AH, 9
 81     INT 21H
 82     LOOP SO
 83     RET
 84 CRLF_FUNC ENDP
 85 ;***************************MAIN_OUTPUT***************************
 86 
 87 MAIN_OUTPUT PROC NEAR
 88     CALL CLS
 89     MOV CX, 2
 90     CALL CRLF_FUNC
 91     
 92     LEA DX, MAIN_MSG4
 93     MOV AH,9
 94     INT 21H
 95     
 96     LEA DX, CRLF
 97     MOV AH, 9
 98     INT 21H
 99     
100     LEA DX, MAIN_MSG5
101     MOV AH, 9
102     INT 21H
103     
104     LEA DX, CRLF
105     MOV AH, 9
106     INT 21H
107     
108     LEA DX, MAIN_MSG6
109     MOV AH, 9
110     INT 21H
111     
112     LEA DX, CRLF
113     MOV AH, 9
114     INT 21H
115     
116     LEA DX, MAIN_MSG7
117     MOV AH, 9
118     INT 21H
119     
120     RET
121 MAIN_OUTPUT ENDP
122 ;***************************
123     END START
View Code

 

posted @ 2015-08-15 21:23  Kinthon  阅读(372)  评论(0编辑  收藏  举报