;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
;
; ESCAPE's  Binary to ASM-DB Converter V2.4
;
; Done by Capella/Escape using DOS32 version3.3
;
;
;                                                               escape'96
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±


ideal
p386
model flat
stack 256


;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
;                        ......CODESEGMENT......
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±

codeseg

start:

                mov ax,0ee00h
                int 31h

                mov fs,bx

                mov ax,_data
                mov ds,ax

                mov ah,09h
                mov edx,offset start_mess
                int 21h

                mov ax,0ee02h
                int 31h

                mov [ds:PSP],esi
                mov [ds:Enviroment],edi

                mov esi,[ds:PSP]
                add esi,128
                                        
                xor ecx,ecx              ; L„nge der Befehlszeile holen
                mov cl,[ds:esi]
                cmp ecx,0
                je error_3
                cmp ecx,127
                ja error_3

                inc esi

                mov edi,offset para_line ; Befehlszeile kopieren
para_get:
                mov al,[ds:esi]
                mov [ds:edi],al

                inc esi
                inc edi
                dec ecx
                jnz para_get

                mov esi,offset para_line ; 1. filenamen holen
                inc esi
                mov edi,offset file_1
_file_1_get:
                mov al,[ds:esi]
                cmp al,32
                je _file_1_end

                mov [ds:edi],al
                inc esi
                inc edi
                jmp _file_1_get
_file_1_end:
                inc esi                  ; 2. filenamen holen
                mov edi,offset file_2
_file_2_get:
                mov al,[ds:esi]
                cmp al,32
                je _file_2_end
                cmp al,0
                je _file_2_end
                cmp al,0dh
                je _file_2_end

                mov [ds:edi],al
                inc esi
                inc edi
                jmp _file_2_get
_file_2_end:
                inc esi                  ; label holen
                mov edi,offset file_label
                mov ecx,20
_label_get:
                mov al,[ds:esi]
                cmp al,32
                je _label_end
                cmp al,0
                je _label_end
                cmp al,0dh
                je _label_end

                mov [ds:edi],al
                inc esi
                inc edi
                dec ecx
                jmp _label_get
_label_end:
                or ecx,ecx
                jz _label_end2
_label_end3:
                mov [byte ds:edi],32
                inc edi
                dec ecx
                jnz _label_end3
_label_end2:
                mov ax,3d02h            ; file zum lesen ”ffnen
                mov edx,offset file_1
                int 21h
                jc error_1              ; wenn fehler dann nach error1

                mov [ds:handel_1],eax   ; handel sichern...

                mov ax,3c00h            ; file zum schreiben ”ffnen
                xor ecx,ecx
                mov edx,offset file_2
                int 21h
                jc error_2              ; wenn fehler, dann nach error2

                mov [ds:handel_2],eax   ; handel sichern......

                call set_label

                mov ah,09h
                mov edx,offset conv_mess
                int 21h

                mov dx,03d4h
                mov al,0ah
                out dx,al
                inc dx
                in al,dx
                mov [ds:cursor_height],al
                mov al,32
                out dx,al

                call do_converting

                mov dx,03d4h
                mov al,0ah
                out dx,al
                inc dx
                mov al,[ds:cursor_height]
                out dx,al

                mov ah,09
                mov edx,offset dummy
                int 21h

                mov ax,4000h
                mov ecx,1
                mov ebx,[ds:handel_2]
                mov edx,offset eof_mark
                int 21h

                mov ax,3e00h
                mov ebx,[ds:handel_1]
                int 21h

                mov ax,3e00h
                mov ebx,[ds:handel_2]
                int 21h

end_it:
                mov ax,4c00h
                int 21h

error_1:
                mov edx,offset e1_txt_1
                cmp ax,2
                je _print_error
                mov edx,offset e1_txt_2
                cmp ax,4
                je _print_error
                mov edx,offset e1_txt_3
                cmp ax,5
                je _print_error
                cmp ax,12
                je _print_error 

                mov edx,offset e1_txt_0
_print_error:
                mov ah,09h
                int 21h

                jmp end_it
error_2:
                mov edx,offset e2_txt_1
                cmp ax,4
                je _print_error
                mov edx,offset e2_txt_2
                cmp ax,5
                je _print_error
                mov edx,offset e2_txt_0
                jmp _print_error

error_3:
                mov ah,09h
                mov edx,offset e3_txt
                int 21h

                jmp end_it

;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
; SET_LABEL = sets the choosen label
;             at the beginning of file
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
set_label:
                mov esi,offset file_label
                xor ecx,ecx
set_label_1:
                mov al,[ds:esi]
                cmp al,32
                je label_end

                inc esi
                inc ecx
                jmp set_label_1
label_end:                          ; ECX = l„nge des labels

                inc ecx

                mov ax,4000h              ; write label into file
                mov edx,offset file_label
                mov ebx,[ds:handel_2]
                int 21h

                ret

;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
; DO_CONVERTING = converts the data and save it 
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
do_converting:
                mov ax,3f00h                    ; lese daten aus dem source-
                mov edx,offset load_buffer      ; file
                mov ecx,64000
                mov ebx,[ds:handel_1]
                int 21h
                cmp eax,0
                je end_converting
                cmp eax,64000                   ; 64000 bytes gelesen ????
                jb convert_last_bytes           ; nein, dann zur anderen
                                                ; converting-routine

                mov ecx,6400                    ; ansonsten 64000 bytes kon-
                mov esi,offset load_buffer      ; vertieren....
                mov edi,offset write_buffer
_do_y_line:
                push ecx

                mov [byte ds:edi],"d"
                mov [byte ds:edi+1],"b"
                mov [byte ds:edi+2],32

                add edi,3

                mov ecx,10                      ; 10 bytes pro zeile
_do_x_line:
                xor ebx,ebx                     ; byte holen
                mov bl,[ds:esi]

                inc [dword ds:byte_counter]

                mov eax,ebx                     ; EBX * 3
                add ebx,ebx
                add ebx,eax

                mov al,[ds:_db_values+ebx]      ; DB-values holen und 
                mov [ds:edi],al                 ; schreiben
                mov al,[ds:_db_values+ebx+1]
                mov [ds:edi+1],al
                mov al,[ds:_db_values+ebx+2]
                mov [ds:edi+2],al

                inc esi                         ; n„chstes byte
                add edi,3                       ; n„chstes _db_value

                dec ecx                         ; schon zeile zu ende ???
                jz end_of_line                  ; ja, dann carriage-return und
                                                ; linefeed setzen
                
                mov al,[ds:_db_komma]           ; ansonsten komma zur trennung
                mov [ds:edi],al                 ; setzen
                inc edi

                jmp _do_x_line
end_of_line:            
                mov ax,0a0dh
                mov [ds:edi],ax
                add edi,2

                mov eax,[ds:byte_counter]
                call _calc_dezi

                pop ecx

                dec ecx                         ; gesamten load_buffer schon
                jnz _do_y_line                  ; konvertiert ???
                                                ; nein, dann weiter.....

                mov edx,offset write_buffer
                sub edi,edx

                mov ax,4000h                    ; schreibe konvertierte Daten
                mov ecx,edi                     ; auf disk....
                mov edx,offset write_buffer
                mov ebx,[ds:handel_2]
                int 21h

                jmp do_converting
end_converting:

                ret

convert_last_bytes:

                mov ecx,eax                     ;
                mov esi,offset load_buffer      
                mov edi,offset write_buffer
_do_y_line2:
                mov [byte ds:edi],"d"
                mov [byte ds:edi+1],"b"
                mov [byte ds:edi+2],32

                add edi,3

                mov edx,10
_do_x_line2:
                xor ebx,ebx                     ; byte holen
                mov bl,[ds:esi]

                inc [dword ds:byte_counter]

                mov eax,ebx                     ; EBX * 3
                add ebx,ebx
                add ebx,eax

                mov al,[ds:_db_values+ebx]      ; DB-values holen und 
                mov [ds:edi],al                 ; schreiben
                mov al,[ds:_db_values+ebx+1]
                mov [ds:edi+1],al
                mov al,[ds:_db_values+ebx+2]
                mov [ds:edi+2],al

                inc esi                         ; n„chstes byte
                add edi,3                       ; n„chstes _db_value

                dec ecx                         ; schon zeile zu ende ???
                jz end_of_line3

                dec edx
                jz end_of_line2                 ; ja, dann carriage-return und
                                                ; linefeed setzen
                
                mov al,[ds:_db_komma]           ; ansonsten komma zur trennung
                mov [ds:edi],al                 ; setzen
                inc edi

                jmp _do_x_line2

end_of_line3:
                mov ax,0a0dh
                mov [ds:edi],ax
                add edi,2

                mov eax,[ds:byte_counter]
                call _calc_dezi

                jmp real_end

end_of_line2:
                mov ax,0a0dh
                mov [ds:edi],ax
                add edi,2

                mov eax,[ds:byte_counter]
                call _calc_dezi

                jmp _do_y_line2                 
real_end:
                mov edx,offset write_buffer
                sub edi,edx

                mov ax,4000h                    ; schreibe konvertierte Daten
                mov ecx,edi                     ; auf disk....
                mov edx,offset write_buffer
                mov ebx,[ds:handel_2]
                int 21h

                ret

;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
; BERECHNE DEZIMALWERT VON REGISTER - EAX -
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
ALIGN 4

_calc_dezi:

        pushad
        xor edi,edi
        
        mov     ESI,4*7         ; 8 dezimalstellen
        mov     ecx,eax

dec83:
        mov     eax,ECX
        xor edx,edx
        idiv     [ds:dec_divider+ESI]
        mov	ECX,EDX		; reuse remainder

                add al,30h      ; use quotient as digit number
                mov [ds:dec_buffer+edi],al

                inc edi
                
        sub ESI,4

        jge dec83

        popad

        mov ah,09h
        mov edx,offset _dec_value
        int 21h

        RET

;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
;                       ......DATENSEGMENT......
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±

dataseg


PSP             dd 0
Enviroment      dd 0
para_amount     dd 0
para_line       db 128 dup (0)

byte_counter    dd 0

dec_divider     dd 1,10,100,1000,10000,100000,1000000,10000000

_dec_value      db 13
dec_buffer      db 8 dup (32)
                db "$"
dummy           db 10,"$"

start_mess      db 13,10," BIN2DB-Converter v2.4 / Ultrafast Pmode-Converting-Routine"
                db 13,10," (c) 1996 Escape Studios",10,"$"

e1_txt_0        db 13,10," Unknown error during opening file...",10,"$"
e1_txt_1        db 13,10," File not found...",10,"$"
e1_txt_2        db 13,10," Too many open files...",10,"$"
e1_txt_3        db 13,10," Access not allowed...",10,"$"

e2_txt_0        db 13,10," Unknown error during writing file...",10,"$"
e2_txt_1        db 13,10," Too many open files...",10,"$"
e2_txt_2        db 13,10," Can't access local drive...",10,"$"

e3_txt          db 13,10," USAGE: BIN2DB(.EXE) -Sourcefile -Targetfile -Label",10,"$"

conv_mess       db 13,10,"         Bytes converted: $"

cursor_height   db 0

handel_1        dd 0
handel_2        dd 0

file_1          db 13 dup (0)
file_2          db 13 dup (0)

file_label      db 20 dup (32)
                

eof_mark        db 0

_db_komma       db ","

_db_values      db "000","001","002","003","004","005","006","007","008","009"
                db "010","011","012","013","014","015","016","017","018","019"
                db "020","021","022","023","024","025","026","027","028","029"
                db "030","031","032","033","034","035","036","037","038","039"
                db "040","041","042","043","044","045","046","047","048","049"
                db "050","051","052","053","054","055","056","057","058","059"
                db "060","061","062","063","064","065","066","067","068","069"
                db "070","071","072","073","074","075","076","077","078","079"
                db "080","081","082","083","084","085","086","087","088","089"
                db "090","091","092","093","094","095","096","097","098","099"

                db "100","101","102","103","104","105","106","107","108","109"
                db "110","111","112","113","114","115","116","117","118","119"
                db "120","121","122","123","124","125","126","127","128","129"
                db "130","131","132","133","134","135","136","137","138","139"
                db "140","141","142","143","144","145","146","147","148","149"
                db "150","151","152","153","154","155","156","157","158","159"
                db "160","161","162","163","164","165","166","167","168","169"
                db "170","171","172","173","174","175","176","177","178","179"
                db "180","181","182","183","184","185","186","187","188","189"
                db "190","191","192","193","194","195","196","197","198","199"

                db "200","201","202","203","204","205","206","207","208","209"
                db "210","211","212","213","214","215","216","217","218","219"
                db "220","221","222","223","224","225","226","227","228","229"
                db "230","231","232","233","234","235","236","237","238","239"
                db "240","241","242","243","244","245","246","247","248","249"
                db "250","251","252","253","254","255"

load_buffer     db 64000 dup (?)

;
; 30 Zahlencodes, 9 Kommas, 1 Carriage-return, 1 Linefeed + 3 bytes fr DB_
;
write_buffer    db 64000*3+6400*9+6400*3+6400*2+1 dup (?)

end start

