x86 machine code on DOS (.com
file) - 70 bytes
Dealing with .COM files, creating a palyndrome is easy - since the COM "loader" just puts the content of the file at address 100h
and jumps there, the program must already hardcode its end somehow and ignore everything after it, so we can just append the reverse of the first N-1 bytes (only caveat: if the program somehow tries to do tricks with the length of file everything breaks).
Here is the hex dump of my .COM
-palyndromizing .COM
:
00000000 31 db 8a 1e 80 00 c6 87 81 00 00 ba 82 00 b8 00 |1...............|
00000010 3d cd 21 72 30 89 c6 bf ff ff b9 01 00 ba fe 00 |=.!r0...........|
00000020 89 f3 b4 3f cd 21 3c 01 75 18 b4 40 bb 01 00 cd |...?.!<.u..@....|
00000030 21 85 ff 75 e5 89 f3 f7 d9 88 ee b8 01 42 cd 21 |!..u.........B.!|
00000040 eb d8 47 74 f0 c3 |..Gt..|
It takes the input file on the command line, and writes the output on stdout; the expected usage is something like compalyn source.com > out.com
.
Commented assembly:
org 100h
section .text
start:
; NUL-terminate the command line
xor bx,bx
mov bl, byte[80h]
mov byte[81h+bx],0
; open the input file
mov dx,82h
mov ax,3d00h
int 21h
; in case of error (missing file, etc.) quit
jc end
; si: source file handle
mov si,ax
; di: iteration flag
; -1 => straight pass, 0 reverse pass
mov di,-1
loop:
; we read one byte at time at a bizarre memory
; location (so that dl is already at -2 later - we shave one byte)
mov cx,1
mov dx,0feh
mov bx,si
mov ah,3fh
int 21h
; if we didn't read 1 byte it means we either got to EOF
; or sought before the start of file
cmp al,1
jne out
; write the byte on stdout
mov ah,40h
mov bx,1
int 21h
; if we are at the first pass we go on normally
test di,di
jnz loop
back:
; otherwise, we have to seek back
mov bx,si
; one byte shorter than mov cx,-1
neg cx
; dl is already at -2, fix dh so cx:dx = -2
mov dh,ch
mov ax,4201h
int 21h
jmp loop
out:
; next iteration
inc di
; if it's not zero we already did the reverse pass
jz back
end:
ret
Tested on itself and the solutions to a previous question seems to work fine in DosBox, some more extensive testing on "canonical" DOS executables will follow.
x=input();print(x+'#'+x[::-1])
. The subset is the set of all programs that don't include newlines. \$\endgroup\$