程序题目:
编写一个程序。 先从键盘输入一个字符串(有英文字母,可能也有数字),然后显示其中数字符的个数、英文字母的个数和字符串的长度;字符串中不能有空格,若有将其删除,并将改变后的字符串按相反的顺序显示出来;输入第二个字符串,显示的时全都以‘*’显示,然后将输入的字符串与前面处理后的字符串比较是否相同,若相同,输出“Password Right!”,否则输出 “Password Error!”。
样例:
题目分析:这个题目比较直白,有几个要注意的地方
1)个人在处理输入原始的密码并计算其中的字母和数字的长度时,所用的方法是单个字符输入(调用中断),每次输入都检查是否是回车,是否是空格,是否是数字或是字母; 2)在输出长度时,如果长度小于10,可以直接将二进制表示与‘0’相加再用字符的形式输出,但是当长度大于10就行不通了,要把十位和个位分离出来;所用的方法是将长度去除以10,这样div会直接把个位和十位分开;
代码:
DATAS SEGMENTMSG DB "Please input a string:", '$'MSG1 DB "The number of alphabet:", '$'MSG2 DB "The number of digit:", '$'MSG3 DB "The length of string: ",'$'MSG4 DB "The opposite direction string: " ,'$'MSG5 DB "Please input password:",'$'MSG6 DB "The result:", '$'MSG7 DB "Password Right",'$'MSG8 DB "Password Error", '$'DIG_NUM DB 0 ;数字长度ALP_NUM DB 0 ;字母长度BLANK_NUM DB 0 ;空格个数FLAG DB '*', '$'PASSWORD DB 30 DUP ('$') ;密码内容与长度PASSWORD_LEN DB 0TEST_PASS DB 30 DUP ('$') ;输入的测试密码内容与长度TEST_PASS_LEN DB 0CRLF DB 0DH, 0AH, '$' ;回车,换行 结束 DATAS ENDSSTACKS SEGMENTDB 8 DUP (0)DB 8 DUP (0) STACKS ENDSCODES SEGMENTASSUME CS:CODES, DS:DATAS, SS:STACKS START: ;PROC FAR ;主函数 MOV AX, DATASMOV DS, AXMOV AX, STACKSMOV SS,AX;MOV AX, CODES ;怎么回事;MOV CS,AXLEA DX, MSGMOV AH, 9INT 21H;输入并处理密码,处理指去空格并同时计算各种长度LEA DI, PASSWORDCALL SET_AND_DEAL_POSSWORDLEA DX, CRLFMOV AH, 9INT 21H;输出各种长度LEA DX, MSG1MOV AH, 9INT 21HMOV AL, ALP_NUMMOV AH, 0CALL OUTPUT_LENLEA DX, CRLFMOV AH, 9INT 21HLEA DX, MSG2MOV AH, 9INT 21HMOV AL, DIG_NUMMOV AH, 0CALL OUTPUT_LENLEA DX, CRLFMOV AH, 9INT 21HLEA DX, MSG3MOV AH ,9INT 21HMOV AL, PASSWORD_LENADD AL, BLANK_NUMMOV AH, 0CALL OUTPUT_LENLEA DX, CRLFMOV AH, 9INT 21H;输出真正的字符串,有空格就反序输出LEA DX, MSG4MOV AH, 9INT 21H;LEA DX, PASSWORD;MOV AH, 9;INT 21HLEA SI, PASSWORDCALL OUTPUT_PASSWORDLEA DX, CRLFMOV AH, 9INT 21H;输入密码LEA DX, MSG5MOV AH, 9INT 21HLEA DI, TEST_PASSCALL INPUT_PASSWORDLEA DX, CRLFMOV AH, 9INT 21H;测试密码;输入参数LEA SI, TEST_PASSLEA DI, PASSWORDMOV AL, [PASSWORD_LEN]MOV AH, [TEST_PASS_LEN]CALL IS_RIGHTLEA DX,CRLFMOV AH,9INT 21HMOV AH,4CHINT 21H;******************* SET_AND_DEAL_POSSWORD *************** SET_AND_DEAL_POSSWORD PROC NEARSO:MOV AH, 1INT 21HCMP AL, 0DH ;输入回车结束JNZ NOT_ENDMOV AH, '$'MOV [DI], AH ;输入字符串结束符RETNOT_END:CMP AL, ' 'JZ BLANK_CASEMOV [DI], AL ;把数据装入密码中INC DICMP AL, 'A'; 'A' = 65, 'a' = 97, '9' = 57JB DIGIT_CASE ; 数字的情况JMP ALPHABEG_CASE ;字母的情况BLANK_CASE:INC BLANK_NUMJMP SODIGIT_CASE:INC DIG_NUMINC PASSWORD_LENJMP SOALPHABEG_CASE:INC ALP_NUMINC PASSWORD_LENJMP SOSET_AND_DEAL_POSSWORD ENDP;*************** OUTPUT_LEN ************ OUTPUT_LEN PROC NEARMOV BL, 10DIV BLMOV BH, AH ;保存余数MOV DL, ALCMP DL, 0JZ OL_S1 ;如果十位为0不输出ADD DL, '0'MOV AH, 2INT 21HOL_S1:MOV DL, BH ;得到个位ADD DL, '0'MOV AH, 2INT 21HRETOUTPUT_LEN ENDP ;************************** OUTPUT_PASSWORD PROC NEARCMP BLANK_NUM, 0JNZ OP_S1 ;反序;ELSEMOV DX, SIMOV AH, 9INT 21HRETOP_S1:MOV DL, PASSWORD_LENMOV DH, 0ADD SI, DX ;从后面开始DEC SI;按字符输出MOV CL, PASSWORD_LENMOV CH, 0OP_S2:MOV DL, [SI]MOV AH, 2INT 21HDEC SILOOP OP_S2RET OUTPUT_PASSWORD ENDP;********************** INPUT_PASSWORD ************** INPUT_PASSWORD PROC NEARS2:MOV AH, 8INT 21HCMP AL, 0DH ; 判断是不是输入换行符JNZ IP_S3MOV AH, '$'MOV [DI], AHRETIP_S3:MOV [DI], AL;[si]是当前地址,相当于c语言中的*pt = value; INC DIINC [TEST_PASS_LEN]MOV DL, '*'MOV AH, 2INT 21HJMP S2INPUT_PASSWORD ENDP;************************* IS_RIGHT PROC NEARCMP AL, AHJZ S3LEA DX, MSG8 ;长度不相等MOV AH, 9INT 21HRETS3:MOV CL, PASSWORD_LENMOV CH, 0S4: MOV AL, [SI]MOV AH, [DI]INC SIINC DICMP AL, AHJNZ S5LOOP S4;rightLEA DX, MSG7MOV AH,9INT 21HRETS5:;errorLEA DX, MSG8MOV AH,9INT 21HRETIS_RIGHT ENDP ;******** CODES ENDS END START
经验总结:
1)写代码,可读性,可重用性,简洁性要保证, BUT BEAUTY;
2)对于自己的代码,如果自己都不能清晰地表达出思路,那就该反省反省,并采取行动,NOT TIME, BUT COURAGE.