1、特注:代码可以优化,请读者自己思考,运行结果绝对没问题!实验二数制转换程序代码二进制到十进制转换DATAS SEGMENTS0 DB “please input a binary (length16):“,0AH,0DH,“$“S1 DB 18 S2 DB ? ;总长度S3 DB 16 DUP(0);存放首地址S4 DW ? ;存放输入的二进制数S5 DB “The nuber is not a binary!“,“$“S6 DB “Do you want to continue?(Y or N):“,0AH,0DH ,“$“TEMP DB 0BUF DB 5 DUP (?);此处输入数据段代
2、码 DATAS ENDSSTACKS SEGMENTDB 256 DUP (?) ;初始化堆栈大小为100;此处输入堆栈段代码STACKS ENDSCODES SEGMENTASSUME CS:CODES,DS:DATAS,SS:STACKSSTART:MOV AX,DATASMOV DS,AXMOV AX,STACKSMOV SS,AX;-;输入一个二进制数 LOOPP:CALL CLS CALL HCMOV DX,OFFSET S0 ;显示字符串s0CALL G9 ;“please input a binary (length16):“,0AH,0DH,“$“ MOV DX,OFFSET
3、S1 ;申请16个内存空间MOV AH,0AH ;将数输入到缓冲区INT 21H;-;将输入的数据整合放到AX中 MOV SI,OFFSET S3MOV CL,S2MOV CH,0HMOV AX,0HLOOP1:CMP BYTE PTR SI,30H ;判断是否为二进制数JL EXITCMP BYTE PTR SI,31HJG EXITMOV BL,SISUB BL,30HMOV BH,0H INC SISHL AX,1ADD AX,BXLOOP LOOP1MOV S4,AXCALL HC;-;二进制转换成十进制 MOV SI,5MOV AX,S4MOV CX,5MOV BX,10LP: CW
4、DMOV DX,0DIV BXADD DL,30H MOV BUF SI-1,DLDEC SILOOP LP;-;显示结果CALL COUNT ;找到第一个非零元的位置MOV AL,TEMPMOV AH,0MOV SI,AXMOV CX,5LOOPPP:MOV DL,BUFSIMOV AH,02HINT 21H INC SI LOOP LOOPPP JMP EXIT1 EXIT:CALL HCMOV DX,OFFSET S5 ;显示字符串S5 CALL G9 ;“The nuber is not a binary!“,“$“ EXIT1:CALL HC MOV DX,OFFSET S6 ;显示
5、字符串S6 CALL G9 ;“Do you want to continue?(Y or N):“,0AH,0DH ,“$“ MOV AH,1INT 21HCMP AL,YJE LOOPPCMP AL,y JE LOOPP MOV AH,4CHINT 21HCOUNT: ;判断第一个非零元的位置MOV TEMP,0MOV SI,0MOV CX,4LOO:MOV DL,BUFSICMP DL,30HJNE EXIT3MOV AL,TEMPADD AL,1MOV TEMP,AL INC SILOOP LOO EXIT3: RET G9: MOV AH,9 ;显示功能调用INT 21HCALL H
6、CRETHC: ;回车换行MOV DL,0AHMOV AH,2INT 21HMOV DL,0DHMOV AH,2INT 21HRETCLS:MOV AH,6 ;屏幕初始化MOV AL,0MOV BH,7HMOV CH,0MOV CL,0MOV DH,24MOV DL,79INT 10HMOV BH,0 ;置光标位子MOV DX,0MOV AH,2INT 10HRET CODES ENDSEND START二进制到十六进制转换DATAS SEGMENTS0 DB “please input a binary (length16):“,0AH,0DH,“$“S1 DB 17 S2 DB ? ;总长
7、度S3 DB 16 DUP(0);存放首地址S4 DW ? ;存放输入的二进制数S5 DB “The nuber is not a binary!“,“$“S6 DB “Do you want to continue?(Y or N):“,0AH,0DH ,“$“S7 DB “The length of the numbei is not 16“,“$“DATAS ENDSSTACKS SEGMENTSTACKS ENDSCODES SEGMENTASSUME CS:CODES,DS:DATAS,SS:STACKSSTART:MOV AX,DATASMOV DS,AXMOV AX,STACKS
8、MOV SS,AX;-;输入一个二进制数 LOOPP:CALL CLS CALL HCMOV DX,OFFSET S0 ;显示字符串S0 CALL G9 ;“please input a binary (length16):“,0AH,0DH,“$“MOV DX,OFFSET S1 ;申请16个内存空间MOV AH,0AH ;将数输入到缓冲区INT 21H;-;将输入的数据整合放到AX中 MOV SI,OFFSET S3MOV CL,S2MOV CH,0HMOV AX,0HLOOP1:CMP BYTE PTR SI,30H ;判断是否为2进制JL EXITCMP BYTE PTR SI,31H
9、 JG EXITCMP S2,10H ;判断是否为16为二进制JNE EXIT2MOV BL,SISUB BL,30HMOV BH,0H INC SISHL AX,1ADD AX,BXLOOP LOOP1MOV S4,AXCALL HC;-;二进制变16进制并输出MOV BX,S4CMP BX,0JZ ZEROMOV CH,04HMOV CL,04H NEXT: ROL BX,CLMOV DL,BLAND DL,0FHADD DL,30HCMP DL,3AHJB LADD DL,7H L: CALL XSDEC CHJNZ NEXTJMP WEISHUZERO: MOV DL,0CALL XS
10、WEISHU:MOV DL,HCALL XS JMP EXIT1 ;-;选择用的程序EXIT:CALL HCMOV DX,OFFSET S5 ;显示字符串S5 CALL G9 ; “The nuber is not a binary!“,“$“ JMP EXIT1EXIT2: CALL HCMOV DX,OFFSET S7 ;显示字符串S7CALL G9 ; “The length of the numbei is not 16“,“$“EXIT1:CALL HC MOV DX,OFFSET S6 CALL G9 ;显示字符串S6MOV AH,1 ;“Do you want to contin
11、ue?(Y or N):“,0AH,0DH ,“$“INT 21HCMP AL,YJE LOOPPCMP AL,y JE LOOPP MOV AH,4CHINT 21HG9: MOV AH,9 ;显示功能调用INT 21HCALL HCRETHC: ;回车换行MOV DL,0AHMOV AH,2INT 21HMOV DL,0DHMOV AH,2INT 21HRETCLS:MOV AH,6 ;屏幕初始化MOV AL,0MOV BH,70HMOV CH,0MOV CL,0MOV DH,24MOV DL,79INT 10HMOV BH,0 ;置光标位子MOV DX,0MOV AH,2INT 10HRETXS: ;2号功能显示MOV AH,2INT 21HRETCODES ENDSEND START