chiark / gitweb /
base91-0.6.0.tar.gz downloaed just now
[base91.git] / DOS-asm / b91enc.asm
1 ; basE91 encoder for DOS\r
2 ;\r
3 ; Copyright (c) 2005-2006 Joachim Henke\r
4 ; All rights reserved.\r
5 ;\r
6 ; Redistribution and use in source and binary forms, with or without\r
7 ; modification, are permitted provided that the following conditions are met:\r
8 ;\r
9 ;  - Redistributions of source code must retain the above copyright notice,\r
10 ;    this list of conditions and the following disclaimer.\r
11 ;  - Redistributions in binary form must reproduce the above copyright notice,\r
12 ;    this list of conditions and the following disclaimer in the documentation\r
13 ;    and/or other materials provided with the distribution.\r
14 ;  - Neither the name of Joachim Henke nor the names of his contributors may\r
15 ;    be used to endorse or promote products derived from this software without\r
16 ;    specific prior written permission.\r
17 ;\r
18 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
19 ; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 ; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21 ; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
22 ; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
23 ; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
24 ; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
25 ; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
26 ; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
27 ; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28 ; POSSIBILITY OF SUCH DAMAGE.\r
29 \r
30 bits 16\r
31 cpu 8086\r
32 org 256\r
33 \r
34         xor sp, sp\r
35         mov si, ld_0    ; create lookup table\r
36         mov bp, 90\r
37 lc_0:\r
38         mov bx, 90\r
39         mov ah, [bp + si]\r
40 lc_1:\r
41         mov al, [bx + si]\r
42         push ax\r
43         dec bx\r
44         jns lc_1\r
45         dec bp\r
46         jns lc_0\r
47 \r
48         inc bx\r
49         mov sp, a_stck\r
50 lc_2:\r
51         push bx\r
52         push bx\r
53         jmp short lc_5\r
54 lc_3:\r
55         mov ax, [si]\r
56         cmp cl, 6       ; bits in queue + 8 < 14?\r
57         sbb dx, dx\r
58         inc si\r
59         mov ch, ah\r
60         add bp, dx\r
61         sbb dx, dx\r
62         xor ch, al\r
63         and ah, dl\r
64         and ch, dl\r
65         sub si, dx\r
66         xor ch, al\r
67         shl ax, cl\r
68         add cl, 8\r
69         or bx, ax\r
70         test bp, bp\r
71         js lc_4\r
72 \r
73         and bh, 0x1F    ; keep 13 bits\r
74         and dl, 8\r
75         and ah, 0x3F\r
76         cmp bx, byte 89 ; value in bit queue < 89?\r
77         sbb al, al\r
78         add dl, cl\r
79         and ah, al\r
80         mov cl, 13\r
81         or bh, ah       ; take 13 or 14 bits\r
82         sub cl, al\r
83         add bx, bx\r
84         mov ax, [bx + a_ltab]\r
85         mov bx, cx\r
86         add cl, 16\r
87         sub cl, dl\r
88         sub dl, bl\r
89         shr bx, cl      ; restore bit queue\r
90         mov cl, dl\r
91         stosw\r
92         dec bp\r
93         jns lc_3\r
94 lc_4:\r
95         push bx\r
96         mov ah, 0x40\r
97         push cx\r
98         mov bx, 1\r
99         lea cx, [di - a_obuf]\r
100         mov dx, a_obuf\r
101         int 0x21        ; write to standard output\r
102 \r
103         dec bx\r
104 lc_5:\r
105         mov ah, 0x3F\r
106         mov cx, s_ibuf\r
107         mov dx, a_ibuf\r
108         int 0x21        ; read from standard input\r
109 \r
110         cld\r
111         pop cx\r
112         mov si, dx\r
113         mov di, a_obuf\r
114         pop bx\r
115         add bp, ax      ; ax = 0 -> EOF\r
116         jc lc_3\r
117 \r
118         push ax\r
119         test cl, cl\r
120         jz lc_6\r
121 \r
122         cmp bx, byte 91 ; value in bit queue < 91?\r
123         sbb dx, dx\r
124         cmp cl, 8       ; less than 8 bits in queue?\r
125         sbb cx, cx\r
126         add bx, bx\r
127         and cx, dx\r
128         mov dx, a_obuf\r
129         mov ax, [bx + a_ltab]\r
130         inc cx\r
131         mov bx, 1\r
132         inc cx\r
133         stosw\r
134         mov ah, 0x40\r
135         int 0x21        ; write out 1 or 2 bytes\r
136 lc_6:\r
137         retn    ; exit program\r
138 ld_0:\r
139         db 'ABCDEFGHIJKLM'\r
140         db 'NOPQRSTUVWXYZ'\r
141         db 'abcdefghijklm'\r
142         db 'nopqrstuvwxyz'\r
143         db '0123456789!#$'\r
144         db '%&()*+,./:;<='\r
145         db '>?@[]^_`{|}~"'\r
146 \r
147 \r
148 a_stck equ ((lc_2 - $$) + 256) & 510\r
149 a_ltab equ 48974\r
150 a_obuf equ ((ld_0 - $$) + 257) & 510\r
151 s_ibuf equ ((a_ltab - a_obuf - 2) << 4) / 29\r
152 a_ibuf equ a_ltab - s_ibuf\r