coder.as2 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. jumps
  2. .286
  3. cseg segment byte public 'CODE'
  4. assume cs:cseg, ds:cseg, es:cseg
  5. start:
  6. mov ax, seg cseg
  7. mov es, ax
  8. push ax
  9. mov si, 80h
  10. cmp byte ptr ds:[si], 0
  11. je errusg
  12. xor ax, ax
  13. mov al, ds:[si]
  14. dec ax
  15. mov cx, ax
  16. inc si
  17. inc si
  18. mov di, offset _hname
  19. lp: cmp byte ptr ds:[si], 0dh
  20. je wrk
  21. movsb
  22. loop lp
  23. wrk: pop ds
  24. ;------------- Host --------------------
  25. mov ax, 3d02h
  26. mov dx, offset _hname
  27. int 21h
  28. jc IOErr
  29. mov bx, ax
  30. mov handle, ax
  31. mov ah, 3fh
  32. mov dx, offset header
  33. mov cx, 28
  34. int 21h
  35. ;------------- Host --------------------
  36. ;------- Password ----------------------
  37. mov ah, 09h
  38. mov dx, offset enter_pass
  39. int 21h
  40. ; Read password from stdin
  41. ;-------------------------------
  42. lea di, [pass]
  43. mov cx, 20
  44. chrloop:mov ah, 08h
  45. int 21h
  46. cmp al, 0Dh
  47. jz pass_end
  48. stosb
  49. mov ah, 02h
  50. mov dl, '*'
  51. int 21h
  52. dec cx
  53. jnz chrloop
  54. pass_end:
  55. mov ah, 09h
  56. mov dx, offset newline
  57. int 21h
  58. sub cx, 20
  59. neg cx
  60. mov [pass_len], cx
  61. cmp cx, 3
  62. jl pass_error
  63. ;-----------------------------------
  64. ; Done
  65. ;------------- Header ------------------
  66. mov ax, 'CD' ; Set signature
  67. mov [header+12h], ax
  68. ;------------- Header ------------------
  69. ; Write EXE header
  70. mov ax, 4200h
  71. mov bx, handle
  72. xor cx, cx
  73. xor dx, dx
  74. int 21h
  75. mov ah, 40h
  76. mov cx, 28
  77. mov dx, offset header
  78. int 21h
  79. ; Calculate count of encode bytes
  80. mov ax, 4202h
  81. xor cx, cx
  82. xor dx, dx
  83. int 21h
  84. mov cx, dx ; Hi part
  85. mov dx, ax ; Lo part
  86. assume cs:cseg, ds:dseg, es:dseg
  87. mov ax, seg dseg
  88. mov es, ax
  89. mov ds, ax
  90. mov bx, cs:[header+8]
  91. mov ax, bx
  92. shr ax, 12
  93. mov cs:[lastOffsH], ax
  94. shl bx, 4
  95. mov cs:[lastOffsL], bx
  96. sub cx,[lastOffsH]
  97. sub dx,[lastOffsL]
  98. mov bx, [handle]
  99. push dx
  100. seg_loop:
  101. test cx, cx
  102. jz no_seg
  103. begins: push cx
  104. ; push dx
  105. mov ax, 4200h
  106. mov cx, cs:[lastOffsH]
  107. mov dx, cs:[lastOffsL]
  108. int 21h
  109. mov ah, 3Fh ; Read segment of
  110. mov cx, 0ffffh ; input file
  111. mov dx, 0 ;
  112. int 21h ;
  113. mov ah, 3Fh ; Read segment of
  114. mov cx, 1h ; input file
  115. mov dx, 0ffffh ;
  116. int 21h ;
  117. xor di, di
  118. xor cx, cx
  119. not cx
  120. call addmagic
  121. call encode
  122. mov ax, 4200h
  123. mov cx, cs:[lastOffsH]
  124. mov dx, cs:[lastOffsL]
  125. int 21h
  126. mov ah, 40h ; Write segment to
  127. mov cx, 0FFFFh ; the input file
  128. mov dx, 0 ;
  129. int 21h ;
  130. mov ah, 40h ; Write segment to
  131. mov dx, 0FFFFh ; the input file
  132. mov cx, 1 ;
  133. int 21h ;
  134. inc cs:[lastOffsH]
  135. pop cx
  136. dec cx
  137. jmp seg_loop
  138. no_seg:
  139. mov ax, 4200h
  140. mov cx, cs:[lastOffsH]
  141. mov dx, cs:[lastOffsL]
  142. int 21h
  143. mov ah, 3Fh ; Read data of
  144. pop cx ; input file
  145. push cx
  146. mov dx, 0 ;
  147. int 21h ;
  148. xor di, di
  149. pop cx
  150. push cx
  151. dec cx
  152. call addmagic
  153. xor di, di
  154. call encode
  155. mov ax, 4200h
  156. mov cx, cs:[lastOffsH]
  157. mov dx, cs:[lastOffsL]
  158. int 21h
  159. mov ah, 40h ; Write segment to
  160. pop cx
  161. mov dx, 0 ;
  162. int 21h ;
  163. pop dx
  164. ; Close host file
  165. mov ah, 3eh
  166. int 21h
  167. ;- Done
  168. ;--------------------------------------------------
  169. mov ah, 4ch
  170. int 21h
  171. ;--------------------------------------------------
  172. ;==================================================
  173. ;--------------------------------------------------
  174. pass_error:
  175. mov dx, offset bad_pass
  176. mov ah, 09h
  177. int 21h
  178. mov ax, 4cFFh
  179. int 21h
  180. errusg: pop ds
  181. mov ah, 09h
  182. mov dx, offset errstr
  183. int 21h
  184. mov ax, 4cFEh
  185. int 21h
  186. IOErr: mov ah, 09h
  187. mov dx, offset errstr
  188. int 21h
  189. mov ax, 4cFDh
  190. int 21h
  191. ;-----------------------------------------------
  192. encode proc near
  193. push si
  194. push ax
  195. push cx
  196. loops2: mov si, offset pass
  197. cmp cx, cs:[pass_len]
  198. jb lasts2
  199. push cx
  200. mov cx, cs:[pass_len]
  201. addmag2: mov al, cs:[si]
  202. xor es:[di], al
  203. inc si
  204. inc di
  205. loop addmag2
  206. pop cx
  207. sub cx, cs:[pass_len]
  208. jmp loops2
  209. lasts2: inc cx
  210. lasts3: mov al, cs:[si]
  211. xor es:[di], al
  212. inc si
  213. inc di
  214. loop lasts3
  215. pop cx
  216. pop ax
  217. pop si
  218. ret
  219. endp
  220. addmagic proc near
  221. push si
  222. push ax
  223. push cx
  224. loops: mov si, offset test_pass
  225. cmp cx, 10
  226. jb lasts
  227. push cx
  228. mov cx, 10
  229. addmag: mov al, cs:[si]
  230. add es:[di], al
  231. inc si
  232. inc di
  233. loop addmag
  234. pop cx
  235. sub cx, 10
  236. jmp loops
  237. lasts: inc cx
  238. lasts0: mov al, cs:[si]
  239. add es:[di], al
  240. inc si
  241. inc di
  242. loop lasts0
  243. pop cx
  244. pop ax
  245. pop si
  246. ret
  247. endp
  248. test_pass db 10, 12, 15, 17, -20, -13, 05, -30, 55, -51
  249. io_err_str db 'Error in file io', 0dh, 0ah, '$'
  250. enter_pass db 'Please enter pass: $'
  251. errstr db 'usage: "coder.exe source"', 0dh, 0ah,'$'
  252. _hname db 13 dup (0)
  253. bad_pass db 'Bad password',0dh,0ah,'$'
  254. newline db 0dh,0ah,'$'
  255. zeroBuf db 400 dup (0)
  256. pass db 20 dup (0)
  257. pass_len dw ?
  258. handle dw ?
  259. header dw 14 dup (?)
  260. _isize dw ?
  261. lastOffsH dw ?
  262. lastOffsL dw ?
  263. rCS2 dw ?
  264. rIP2 dw ?
  265. reloBuf dw 200 dup (?)
  266. buffer dw 07B00h dup (?)
  267. ends
  268. dseg segment para public 'DATA'
  269. file_buf db 0FFFFh dup (?)
  270. ends
  271. sseg segment stack
  272. db 200 dup (?)
  273. ends
  274. end start