DATA SEGMENTMSG1 DB "Hex:", "$"MSG2 DB 0DH, 0AH, "Binary:", "$"COUNT EQU 4BUF DB 4 DUP(?)DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATASTART: MOV AX, DATA MOV DS, AX LEA DX, MSG1 MOV AH, 9 INT 21H MOV CX, COUNT MOV SI, 0INPUT: MOV AH, 1 ;透過四次迴圈從鍵盤讀入四位十六進位制數 INT 21H MOV BUF[SI], AL DEC CX INC SI CMP CX, 0 JNZ INPUTNEXT3: LEA DX, MSG2 MOV AH, 9 INT 21H MOV CX, COUNT MOV SI, 0NEXT2: MOV BL, BUF[SI] CMP BL , "9" JBE OK ;讀入的字元<=9 轉向 OK SUB BL, 7 ;讀入字元>= 9 減7 OK: PUSH CX MOV CL ,4 SHL BL, CL ; 邏輯左移4位,低4位à高4位 MOV CX, 4 ; 4次迴圈將當前處理字元的4位二進位制輸出NEXT1: MOV DL, "0" SHL BL, 1 JNC SKIP INC DLSKIP: MOV AH, 2 INT 21H LOOP NEXT1 POP CX ; INC SI LOOP NEXT2 ; 4次迴圈將4位十六進位制數,每位轉換為對應的4位二進位制EXIT: MOV AH, 4CH INT 21HCODE ENDS END START試試看,我用emu8086
DATA SEGMENTMSG1 DB "Hex:", "$"MSG2 DB 0DH, 0AH, "Binary:", "$"COUNT EQU 4BUF DB 4 DUP(?)DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATASTART: MOV AX, DATA MOV DS, AX LEA DX, MSG1 MOV AH, 9 INT 21H MOV CX, COUNT MOV SI, 0INPUT: MOV AH, 1 ;透過四次迴圈從鍵盤讀入四位十六進位制數 INT 21H MOV BUF[SI], AL DEC CX INC SI CMP CX, 0 JNZ INPUTNEXT3: LEA DX, MSG2 MOV AH, 9 INT 21H MOV CX, COUNT MOV SI, 0NEXT2: MOV BL, BUF[SI] CMP BL , "9" JBE OK ;讀入的字元<=9 轉向 OK SUB BL, 7 ;讀入字元>= 9 減7 OK: PUSH CX MOV CL ,4 SHL BL, CL ; 邏輯左移4位,低4位à高4位 MOV CX, 4 ; 4次迴圈將當前處理字元的4位二進位制輸出NEXT1: MOV DL, "0" SHL BL, 1 JNC SKIP INC DLSKIP: MOV AH, 2 INT 21H LOOP NEXT1 POP CX ; INC SI LOOP NEXT2 ; 4次迴圈將4位十六進位制數,每位轉換為對應的4位二進位制EXIT: MOV AH, 4CH INT 21HCODE ENDS END START試試看,我用emu8086