8086 machine code,
Contents of the
B7 3D 89 DF B1 80 F3 AA 0D 0A 24 B4 01 CD 21 42
38 D8 74 F7 38 17 77 02 88 17 88 C3 31 D2 3C 0D
75 E9 BF 21 3D B1 5E 31 C0 F3 AE E3 EE 4F BB 04
01 8A 05 D4 0A 86 E0 0D 30 30 89 47 02 3C 30 77
04 88 67 03 43 89 3F 89 DA B4 09 CD 21 47 EB D7
It only supports repetitions of up to 99 characters.
Source code (served as input for the
debug.com assembler), with comments!
mov bh, 3d ; storage of 128 bytes at address 3d00
mov di, bx
mov cl, 80
rep stosb ; zero the array
db 0d 0a 24
mov ah, 1
int 21 ; input a char
inc dx ; calculate the run length
cmp al, bl ; is it a repeated character?
cmp [bx], dl ; is the new run length greater than previous?
mov [bx], dl ; store the new run length
mov bl, al ; remember current repeating character
xor dx, dx ; initialize run length to 0
cmp al, d ; end of input?
jne 10b ; no - repeat
mov di, 3d21 ; start printing run lengths with char 21
mov cl, 5e ; num of iterations = num of printable characters
xor ax, ax
repe scasb ; look for a nonzero run length
jcxz 11b ; no nonzero length - exit
mov bx, 104 ; address of output string
mov al, [di] ; read the run length
aam ; convert to decimal
xchg al, ah
or ax, 3030
mov [bx+2], ax
cmp al, 30 ; was it less than 10?
mov [bx+3], ah ; output only one digit
inc bx ; adjust for shorter string
mov [bx], di ; store "x=" into output string
mov dx, bx ; print it
mov ah, 9
jmp 127 ; repeat
Here are some golfing techniques used here that I think were fun:
- array's address is
3d is the ascii-code for
=. This way, the address for array's entry for character
3d78. When interpreted as a 2-character string, it's
- Output buffer is at address
104; it overwrites initialization code that is no longer needed. End-of-line sequence
0D 0A 24 is executed as harmless code.
aam instruction here doesn't provide any golfing, though it could...
- Writing the number twice, first assuming it's greater than 10, and then correcting if it's smaller.
- Exit instruction is at an obscure address
11b, which contains the needed machine code
C3 by luck.