Hey, im trying to make an assembly program which encodes a file using base64.

Anyway I have a program that works in converting the word "Man" to TWFu. Problem is it does not convert anything else but a few 3 letter words like Tan and Ban probally because of its similarity with Man. Below is a code snippet I am using to run the encode, taking 3 bytes from the read file and converting it to 4 bytes writing it to the file then grabbing another 3 bytes and doing the same thing. If anyone could help me figure out what I am doing wrong here help would be appreciated. I am completely stumped at this point and spent the weekend stuck on this one issue.

By the way this is x86 Assembly code, 16 bit. I am using Masm as my compiler.

Encode64:
	;Read Input File (3 bytes)
	mov ah, 3fh 
	mov bx, input_handle 
	mov cx, Buff3b;seting the size to read at a time
	mov dx, offset input_pointer ;pointing the buffer where to store
	int 21h 
	jc End_Program

	;Appends a 0 if the number of bytes read is less then 3
	;ax = amount of read bytes
	cmp ax, 2
	jne start0
	mov input_pointer[2], 0
	start0:
		cmp ax, 1
		jne end0
		mov input_pointer[1],0
		mov input_pointer[2],0
	end0:
		or ax, ax
		jz done2 ;jump to done2 if ax=0
	
;;;;;;;;;;Convert the 3byte data into 4 byte data
;;;;;;;;;;Storing the output into a buffer for writing
	
	
	B1:
		mov al, input_pointer[0]
		shr al, 2
		add al,65d
		mov output_pointer[0],al

	B2:
		mov ax, word ptr input_pointer[0]
		xchg al, ah
		shl ax, 6
		shr ax, 10
		add al, 65d
		
		
		
		mov output_pointer[1], al

	B3:
		mov ax, word ptr input_pointer[1]
		xchg al, ah
		shl ax, 4
		shr ax, 10
		add al, 65d
		mov output_pointer[2],al
	B4:
		mov al, input_pointer[2]
		shl al, 4
		shr al,2
		add al, 61d
		mov output_pointer[3], al


;;;;;;;;;;;;;;;;;;;;;;;;; write 4b encoded data to output file 
		mov cx, buff4b;set number of bye to write
		mov ah, 40h 
		mov bx, output_handle 
		mov dx, offset output_pointer ;write from buffer
		int 21h 
		jc end_program
		jmp Encode64

Recommended Answers

All 4 Replies

I'm not sure exactly what problems you are having, but the stuff following B4 has some errors.

First, if you only want to clear the top two bits of AL then shl al, [B]2[/B] shr al, 2 Or, even simpler: and al, 3Fh Also, you only add 61 to it instead of 65 as you did in all the others.

You are aware, I presume, that simply adding 'A' doesn't account for three things: punctuation characters between A..Z and a..z, a control character (ASCII 127), and the fact that you exceed the ASCII character range by one character (your domain is 65..128, while ASCII is only defined on 0..127).

You would do better to use a lookup table for ASCII digits and use the xlat instruction to convert AL.

Hope this helps.

I'm not sure exactly what problems you are having, but the stuff following B4 has some errors.

First, if you only want to clear the top two bits of AL then shl al, [B]2[/B] shr al, 2 Or, even simpler: and al, 3Fh Also, you only add 61 to it instead of 65 as you did in all the others.

You are aware, I presume, that simply adding 'A' doesn't account for three things: punctuation characters between A..Z and a..z, a control character (ASCII 127), and the fact that you exceed the ASCII character range by one character (your domain is 65..128, while ASCII is only defined on 0..127).

You would do better to use a lookup table for ASCII digits and use the xlat instruction to convert AL.

Hope this helps.

xlat is definately a nifty feature, just applied it. That said it is defiantely working better but still working hard on it!. I will look into more in depth how to fix it with the suggestions you have outline. I'll come back if I run into any road blocks but hopefulyl that wont happen.

Great. When you are ready to go the other way, you can use rep scasb to look for an item in the table, and subtract the difference between the location of the item in the table and the beginning of the table to get the index of the item.

Hope this helps.

Thanks~ Definately helps, I was able to make a working program that perfectly encodes into Base64 100% of the time. As for decoding I decided to start working on it soon, thanks for the tip on how to go the other way!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.