組合語言數值轉換程式
1、將ASCII碼十進位制數轉換為二進位制數
STACK1 SEGMENT STACK
DW 256 DUP(?)
STACK1 ENDS
DDATA SEGMENT
MES1 DB 'The ascii code of decimal code are:$'
BUF DB 30H,30H,30H,31H,32H
DB 10H DUP(0)
DDATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DDATA
START: MOV AX,DDATA
MOV DS,AX
MOV SI,OFFSET BUF
MOV BX,000AH
MOV CX,0004H
MOV AH,00H
MOV AL,[SI]
SUB AL,30H
A1: IMUL BX
ADD AL,[SI+01]
SUB AL,30H
INC SI
LOOP A1
MOV [SI],AX
MOV DX,OFFSET MES1
MOV AH,09H
INT 21H
INC SI ;顯示高位元組
CALL SHOW
DEC SI ;顯示低位元組
CALL SHOW
WAIT1: MOV AH,1 ;判斷是否有按鍵按下
INT 16H
JZ WAIT1 ;無按鍵則跳回繼續等待,有則退出
MOV AX,4C00H
INT 21H
SHOW PROC NEAR
MOV AL,DS:[SI]
AND AL,0F0H ;取高4位
SHR AL,4
CMP AL,0AH ;是否是A以上的數
JB C2
ADD AL,07H
C2: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
MOV AL,DS:[SI]
AND AL,0FH ;取低4位
CMP AL,0AH
JB C3
ADD AL,07H
C3: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
RET
ENDP
CODE ENDS
END START
2、將十進位制數的ASCII碼轉換為BCD碼
STACK1 SEGMENT STACK
DW 256 DUP(?)
STACK1 ENDS
DDATA SEGMENT
MES1 DB 'The BCD code of decimal are:$'
BUF DB 31H,32H,33H,34H,35H
DB 10H DUP(0)
DDATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DDATA
START: MOV AX,DDATA
MOV DS,AX
MOV CX,0005H
MOV DI,OFFSET BUF
A1: MOV BL,0FFH
MOV AL,[DI]
CMP AL,3AH ;比較AL中的數是否是0~9的ASCII碼
JNB A2
SUB AL,30H
JB A2
MOV BL,AL
A2: MOV AL,BL
MOV [DI+05H],AL
INC DI
LOOP A1
MOV SI,DI
MOV CX,05H
MOV DX,OFFSET MES1
MOV AH,09H
INT 21H
A3: CALL SHOW
MOV DL,20H
MOV AH,02H
INT 21H
INC SI
LOOP A3
WAIT1: MOV AH,1 ;判斷是否有按鍵按下
INT 16H
JZ WAIT1 ;無按鍵則跳回繼續等待,有則退出
MOV AX,4C00H
INT 21H
SHOW PROC NEAR
MOV AL,DS:[SI]
AND AL,0F0H ;取高4位
SHR AL,4
CMP AL,0AH ;是否是A以上的數
JB C2
ADD AL,07H
C2: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
MOV AL,DS:[SI]
AND AL,0FH ;取低4位
CMP AL,0AH
JB C3
ADD AL,07H
C3: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
RET
ENDP
CODE ENDS
END START
3、將十六進位制的ASCII碼轉換為十進位制數
STACK1 SEGMENT STACK
DW 256 DUP(?)
STACK1 ENDS
DDATA SEGMENT
MES1 DB 'The ascii code of hex are:$'
BUF DB 0CH,00H
DB 10H DUP(0)
DDATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DDATA
START: MOV AX,DDATA
MOV DS,AX
MOV SI,OFFSET BUF
MOV DX,[SI]
MOV BX,SI
ADD BX,2
ADD SI,7
A1: DEC SI
MOV AX,DX
MOV DX,0000H
MOV CX,000AH
DIV CX
XCHG AX,DX
ADD AL,30H
MOV [SI],AL
CMP DX,0000H
JNE A1
A2: CMP SI,BX
JZ A3
DEC SI
MOV AL,30H
MOV [SI],AL
JMP A2
A3: MOV CX,5
MOV SI,OFFSET BUF
ADD SI,2
MOV DX,OFFSET MES1
MOV AH,09H
INT 21H
A4: CALL SHOW
MOV DL,20H
MOV AH,02H
INT 21H
INC SI
LOOP A4
WAIT1: MOV AH,1 ;判斷是否有按鍵按下
INT 16H
JZ WAIT1 ;無按鍵則跳回繼續等待,有則退出
MOV AX,4C00H
INT 21H
SHOW PROC NEAR
MOV AL,DS:[SI]
AND AL,0F0H ;取高4位
SHR AL,4
CMP AL,0AH ;是否是A以上的數
JB C2
ADD AL,07H
C2: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
MOV AL,DS:[SI]
AND AL,0FH ;取低4位
CMP AL,0AH
JB C3
ADD AL,07H
C3: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
RET
ENDP
CODE ENDS
END START
4、BCD碼轉換為二進位制碼
STACK1 SEGMENT STACK
DW 256 DUP(?)
STACK1 ENDS
DDATA SEGMENT
MES1 DB 'The BCD code of binary are:$'
BUF DB 01H,07H,03H,04H,05H,01H,06H,08H
DB 10H DUP(0)
DDATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DDATA
START: MOV AX,DDATA
MOV DS,AX
MOV CX,0004H
MOV DI,OFFSET BUF
A1: MOV AL,[DI]
ADD AL,AL
MOV BL,AL
ADD AL,AL
ADD AL,AL
ADD AL,BL
INC DI
MOV AH,00H
ADD AL,[DI]
MOV [DI+07H],AX
INC DI
LOOP A1
MOV DX,OFFSET MES1
MOV AH,09H
INT 21H
MOV CX,04H
MOV DI,OFFSET BUF
ADD DI,08H
A2: MOV AX,[DI]
CALL SHWORD
MOV DL,20H
MOV AH,02H
INT 21H
INC DI
INC DI
LOOP A2
WAIT1: MOV AH,1 ;判斷是否有按鍵按下
INT 16H
JZ WAIT1 ;無按鍵則跳回繼續等待,有則退出
MOV AX,4C00H
INT 21H
SHWORD PROC NEAR
MOV BL,AH
CALL SHOW
MOV BL,AL
CALL SHOW
RET
ENDP
SHOW PROC NEAR
PUSH AX
PUSH DX
MOV AL,BL
AND AL,0F0H ;取高4位
SHR AL,4
CMP AL,0AH ;是否是A以上的數
JB C2
ADD AL,07H
C2: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
MOV AL,BL
AND AL,0FH ;取低4位
CMP AL,0AH
JB C3
ADD AL,07H
C3: ADD AL,30H
MOV DL,AL ;show character
MOV AH,02H
INT 21H
POP DX
POP AX
RET
ENDP
CODE ENDS
END START