Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encode and Decode problems.

Status
Not open for further replies.

dxcc

Programmer
Dec 1, 2007
1
0
0
US
Hello.

I am having a very hard time trying to encode a file and then decode it again so i can end up with the original file.

I want to transform the content in the original file into base 64. I've tried and succeeded with decimal and hexadecimal bases.
Ex. When i have 'abcde' in a text file and i encode it into 97 98 99 100 101, and then decode it back in a differnet file i have "abcde"

When i use hex base .. i have 'abcde' and then enconde it as 61 62 63 64 65.. when i decode it it gives me back abcde..

THe table looks liie this.


Base is the encoding mode.
Alphabet is the letters it has. Example: the decimal alphabet has 10 "letters"( 0 1 2 3 4 5 6 7 8 9) and a space === 11.

PROPORTION MEANS HOW FAST THE ENCODING IS DONE.



-----------------------------------
| BASE | ALPHABET | PROPORTION |
|---------|----------|------------|
| Dec | 11 | 3.5 |
|---------|----------|------------|
| Hex | 16 | 2.5 |
|---------|----------|------------|
| Base 64 | 64 | 1.33 |
|---------|----------|------------|
| Base 85 | 85 | 1.25 |
|---------|----------|------------|
| Base 85 | 200 |close to one|
|---------|----------|------------|

Here is my code for base 64. However, as mentioned before is not working. Could anyone please help me.
;---------------------------------------------------------
.386
assume cs:main_seg, ds:main_seg
main_seg segment
org 100h
;-----------------------------variables-------------------
start:
jmp real_start
help_content db "Syntex is : code [inputfile] [outputfile]",'$'
tail_length byte 0
length1 byte 0
file_in db 30 dup(0),'$'
file_out_en db 30 dup(0),'$'

inp db 3 dup(?),'$'
oute db 4 dup (?),'$'
buffsize1 equ 3
buffsize2 equ 4
inh1 dw ?
outh1 dw ?

;----------------------------program starts here------------------------------
real_start:



mov bx, 80h ;points bx to the arguments to get the lenght
mov al, [bx]
mov tail_length, al ;store total length of the argument to tail_lenght
dec tail_length ;decrease lenght by one since first character is space and we are not going to count space
mov bx, 81h ;//points bx to get the argument
mov al, [bx] ;//move first argument to al
cmp al, 13 ;//compare if there was no argument
jne next1 ;//if there are arguments, go to analize them
syntax_error:
mov ah, 09h ;//if no argument, show help and stop application
lea dx, help_content
int 21h
call exit

next1:
inc bx ;eleminate the space as i said before
mov dx, offset file_in ;point argument1 to dx

mov cl, tail_length ;Max loop counter set to total lenght of the argument

part1: ;get first part of the agrument
mov al, byte ptr [bx]
inc bx
xchg dx, bx
mov byte ptr [bx], al
inc bx
xchg dx, bx
cmp al, 20h
je getpart2
inc length1
loop part1

getpart2:
mov bx, 82h
sub cx, cx
mov cl, length1
add bx, cx
mov al, [bx]

space_remover2:
cmp al, 13
je syntax_error
inc bx
inc length1
mov al, [bx]
cmp al, 13
je syntax_error
cmp al, 20h
je space_remover2
mov dx, offset file_out_en
mov cl, tail_length
sub cl, length1

part2:
mov al, byte ptr [bx]
inc bx
xchg dx, bx
mov byte ptr [bx], al
inc bx
xchg dx, bx

loop part2



;------------opening input file
mov ah, 3dh ;to open a existing file
mov al,0 ;mode = read only
mov dx, offset file_in ;point the input file to dx
int 21h ;call Ms-dos
jc exit ;quit if error
mov inh1, ax ;move handlar to inh.


;-------------creating output file

mov ah, 3ch ;creat output file
sub cx, cx ; attributes clear
mov dx, offset file_out_en ;point the output file to dx
int 21h ;call Ms-dos
jc exit ;quit if error
mov outh1, ax ;mode handlare to outh

e:

;-----------reading from the input file
mov ah, 3fh ;read the file
mov bx, inh1 ;point input file handler
mov cx, buffsize1 ;seting the size to read at a time
mov dx, offset inp ;pointing the buffer where to store
int 21h ;call ms-dos
jc exit ;quit if error

;--------------add 0 at the end of the file if total caracter is not divided by zero
cmp ax, 2
jne n1
mov inp[2], 0
n1:
cmp ax, 1
jne n2
mov inp[1],0
mov inp[2],0
n2:


or ax, ax
jz done ;jump to done if ax=0


;---------take 3 character (8bit) input form inp vaiable and store 4 character (6bit) to outp vairable
L1:
mov al, inp[0]

shr al, 2
add al, 49d
mov oute[0], al

L2:
mov ax, word ptr inp[0]
xchg al, ah
shl ax, 6
shr ax, 10
add al, 49d
mov oute[1], al

L3:
mov ax, word ptr inp[1]
xchg al, ah
shl ax, 4
shr ax, 10
add al, 49d
mov oute[2], al
L4:
mov al, inp[2]
shl al, 2
shr al,2
add al, 49d
mov oute[3], al
;--------------------------write it to the file


;-----------writing buffer to the file


mov cx, buffsize2 ;set number of bye to write
mov ah, 40h ;write the file
mov bx, outh1 ;point to the output file
mov dx, offset oute ;write from buffer
int 21h ;call dos
jc exit ;quit if error


;----------------------------------------------------

jmp e
done:


;---------close files
mov ah, 3eh
mov bx, inh1
int 21h

mov ah, 3eh
mov bx, outh1
int 21h

exit:
int 20h
main_seg ends
end start
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top