8088组装件,IBM PC DOS, 59 58个字节
未组装的清单:
BE 0082 MOV SI, 82H ; SI to begining of input string
AC LODSB ; load first '#' char into AL
B4 0E MOV AH, 0EH ; BIOS display char function
CD 10 INT 10H ; call BIOS
B3 11 MOV BL, 17 ; set up for divide by 17
B9 0304 MOV CX, 0304H ; hex byte loop counter (CH=3), shift counter (CL=4)
LOOP_BYTE:
AD LODSW ; load next two ASCII hex chars into AX
B7 02 MOV BH, 2 ; hex chars loop counter
LOOP_ALPHA:
2C 30 SUB AL, '0' ; convert from ASCII
3C 0A CMP AL, 10 ; is digit > 10 (A-F)?
7C 02 JL NOT_ALPHA ; if not, jump to next char
2C 07 SUB AL, 7 ; ASCII adjust alpha char to binary
NOT_ALPHA:
86 E0 XCHG AH, AL ; swap first and second chars
FE CF DEC BH ; decrement loop counter
75 F2 JNZ LOOP_ALPHA ; loop to next hex char
D2 E0 SHL AL, CL ; shift low nibble to high nibble
02 C4 ADD AL, AH ; add first and second nibbles
32 E4 XOR AH, AH ; clear AH for add/division
05 0008 ADD AX, 8 ; add 0.5 (8/16) to round (with overflow)
F6 F3 DIV BL ; divide by 17
3C 0A CMP AL, 10 ; is digit > 10?
7C 02 JL DISP_CHAR ; if not, jump to display digit
04 07 ADD AL, 7 ; binary adjust alpha char to ASCII
DISP_CHAR:
04 30 ADD AL, '0' ; convert to ASCII
B4 0E MOV AH, 0EH ; BIOS display char function
CD 10 INT 10H ; call BIOS
FE CD DEC CH ; decrement loop counter
75 D4 JNZ LOOP_BYTE ; loop to next hex byte
C3 RET ; return to DOS
独立PC DOS可执行文件。输入是通过命令行,输出是通过控制台。
大多数代码长度都在处理所需的十六进制字符串I / O转换为字节,因为DOS / x86机器代码没有内置的内置代码。
输入/输出:
下载并测试HEXCLR.COM或xxd
hexdump:
0000000: be82 00ac b40e cd10 b311 b904 03ad b702 ................
0000010: 2c30 3c0a 7c02 2c07 86e0 fecf 75f2 d2e0 ,0<.|.,.....u...
0000020: 02c4 32e4 0508 00f6 f33c 0a7c 0204 0704 ..2......<.|....
0000030: 30b4 0ecd 10fe cd75 d4c3 0......u..