IBM PC DOS,8088组件, 54 35字节
-19字节使用差异法
ac2c 41d0 d8d7 7206 51b1 04d2 e859 240f 2c03 02e0 e2ea 3534 4527 4125 1303 1462 4523 13
未组装:
; compare dashes and dots in a morse code string
; input:
; I: pointer to input string (default SI)
; IL: length of input string (default CX)
; TBL: pointer to data table (default BX)
; output:
; Sign/OF flags: Dot-heavy: SF == OF (JGE), Dash-heavy: SF != OF (JL)
MORSE_DD MACRO I, IL, TBL
LOCAL LOOP_LETTER, ODD
IFDIFI <I>,<SI> ; skip if S is already SI
MOV SI, I ; load string into SI
ENDIF
IFDIFI <IL>,<CX> ; skip if IL is already CX
MOV CX, IL ; set up loop counter
ENDIF
IFDIFI <TBL>,<BX> ; skip if TBL is already BX
MOV BX, OFFSET TBL ; load letter table into BX
ENDIF
LOOP_LETTER:
LODSB ; load next char from DS:SI into AL, advance SI
;AND AL, 0DFH ; uppercase the input letter (+2 bytes)
SUB AL, 'A' ; convert letter to zero-based index
RCR AL, 1 ; divide index by 2, set CF if odd index
XLAT ; lookup letter in table
JC ODD ; if odd index use low nibble; if even use high nibble
PUSH CX ; save loop counter (since SHR can only take CL on 8088)
MOV CL, 4 ; set up right shift for 4 bits
SHR AL, CL ; shift right
POP CX ; restore loop counter
ODD:
AND AL, 0FH ; mask low nibble
SUB AL, 3 ; unbias dash/dot difference +3 positive
ADD AH, AL ; add letter difference to sum (set result flags)
LOOP LOOP_LETTER
ENDM
TBL DB 035H, 034H, 045H, 027H, 041H, 025H, 013H, 003H, 014H, 062H, 045H, 023H, 013H
说明
仅使用8088兼容指令,以Intel / MASM语法作为MACRO(基本上是函数)实现。输入为大写字符串(或+2个字节以允许使用大小写混合),输出Truthy / Falsy结果为SF == OF
(使用JG
或JL
测试)。
字母差异表的值存储为二进制半字节,因此总共只需要13个字节。
原始文件(54个字节):
; compare dashes and dots in a Morse code string
; input:
; I: pointer to input string (default SI)
; IL: length of input string (default CX)
; TBL: pointer to data table
; output:
; Carry Flag: CF=1 (CY) if dot-heavy, CF=0 (NC) if dash-heavy
MORSE_DD MACRO I, IL, TBL
LOCAL LOOP_LETTER
IFDIFI <I>,<SI> ; skip if S is already SI
MOV SI, I ; load string into SI
ENDIF
IFDIFI <IL>,<CX> ; skip if IL is already CX
MOV CX, IL ; set up loop counter
ENDIF
MOV BX, OFFSET TBL ; load score table into BX
XOR DX, DX ; clear DX to hold total score
LOOP_LETTER:
LODSB ; load next char from DS:SI into AL, advance SI
;AND AL, 0DFH ; uppercase the input letter (+2 bytes)
SUB AL, 'A' ; convert letter to zero-based index
XLAT ; lookup letter in table
MOV AH, AL ; examine dot nibble
AND AH, 0FH ; mask off dash nibble
ADD DH, AH ; add letter dot count to total
PUSH CX ; save loop counter (since SHR can only take CL)
MOV CL, 4 ; set up right shift for 4 bits
SHR AL, CL ; shift right
POP CX ; restore loop counter
ADD DL, AL ; add letter dash count to total
LOOP LOOP_LETTER
CMP DL, DH ; if dot-heavy CF=1, if dash-heavy CF=0
ENDM
; data table A-Z: MSN = count of dash, LSN = count of dot
TBL DB 011H, 013H, 022H, 012H, 001H, 013H, 021H, 004H, 002H
DB 031H, 021H, 013H, 020H, 011H, 030H, 022H, 031H, 012H
DB 003H, 010H, 012H, 013H, 021H, 022H, 031H, 022H
说明
仅使用8088兼容指令,以Intel / MASM语法作为MACRO(基本上是函数)实现。作为字符串输入,在进位标志中输出“真/假”结果。分数表包含每个字母的破折号和点数。
输入为大写。添加2个字节以采用小写或混合大小写。
示例测试程序(作为IBM PC DOS独立COM可执行文件)
SHR SI, 1 ; point SI to DOS PSP
LODSW ; load arg length into AL, advance SI to 82H
MOV CL, AL ; set up loop counter in CH
DEC CX ; remove leading space from letter count
MORSE_DD SI, CX, TBL ; execute above function, result is in CF
MOV DX, OFFSET F ; default output to "Falsy" string
JA DISP_OUT ; if CF=0, result is falsy, skip to output
MOV DX, OFFSET T ; otherwise CF=1, set output to "Truthy" string
DISP_OUT:
MOV AH, 09H ; DOS API display string function
INT 21H
RET
T DB "Truthy$"
F DB "Falsy$"
示例输出:
下载测试程序DD.COM
或在线尝试!
我不知道在线TIO可以直接链接到DOS可执行文件,但是您只需几个步骤即可使用它:
- 将DD.COM下载为ZIP文件
- 转到https://virtualconsoles.com/online-emulators/DOS/
- 上载刚刚下载的ZIP文件,然后单击“开始”。
- 输入
DD Hello
或DD code
输入您的内心内容