Encoding help, run into block.

Thread Solved
Reply

Join Date: Dec 2007
Posts: 10
Reputation: Ninjikiran is an unknown quantity at this point 
Solved Threads: 0
Ninjikiran Ninjikiran is offline Offline
Newbie Poster

Encoding help, run into block.

 
0
  #1
Dec 11th, 2007
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.

  1. Encode64:
  2. ;Read Input File (3 bytes)
  3. mov ah, 3fh
  4. mov bx, input_handle
  5. mov cx, Buff3b;seting the size to read at a time
  6. mov dx, offset input_pointer ;pointing the buffer where to store
  7. int 21h
  8. jc End_Program
  9.  
  10. ;Appends a 0 if the number of bytes read is less then 3
  11. ;ax = amount of read bytes
  12. cmp ax, 2
  13. jne start0
  14. mov input_pointer[2], 0
  15. start0:
  16. cmp ax, 1
  17. jne end0
  18. mov input_pointer[1],0
  19. mov input_pointer[2],0
  20. end0:
  21. or ax, ax
  22. jz done2 ;jump to done2 if ax=0
  23.  
  24. ;;;;;;;;;;Convert the 3byte data into 4 byte data
  25. ;;;;;;;;;;Storing the output into a buffer for writing
  26.  
  27.  
  28. B1:
  29. mov al, input_pointer[0]
  30. shr al, 2
  31. add al,65d
  32. mov output_pointer[0],al
  33.  
  34. B2:
  35. mov ax, word ptr input_pointer[0]
  36. xchg al, ah
  37. shl ax, 6
  38. shr ax, 10
  39. add al, 65d
  40.  
  41.  
  42.  
  43. mov output_pointer[1], al
  44.  
  45. B3:
  46. mov ax, word ptr input_pointer[1]
  47. xchg al, ah
  48. shl ax, 4
  49. shr ax, 10
  50. add al, 65d
  51. mov output_pointer[2],al
  52. B4:
  53. mov al, input_pointer[2]
  54. shl al, 4
  55. shr al,2
  56. add al, 61d
  57. mov output_pointer[3], al
  58.  
  59.  
  60. ;;;;;;;;;;;;;;;;;;;;;;;;; write 4b encoded data to output file
  61. mov cx, buff4b;set number of bye to write
  62. mov ah, 40h
  63. mov bx, output_handle
  64. mov dx, offset output_pointer ;write from buffer
  65. int 21h
  66. jc end_program
  67. jmp Encode64
Last edited by Ninjikiran; Dec 11th, 2007 at 1:15 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Encoding help, run into block.

 
0
  #2
Dec 11th, 2007
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, 2
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: Ninjikiran is an unknown quantity at this point 
Solved Threads: 0
Ninjikiran Ninjikiran is offline Offline
Newbie Poster

Re: Encoding help, run into block.

 
0
  #3
Dec 11th, 2007
Originally Posted by Duoas View Post
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, 2
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Encoding help, run into block.

 
0
  #4
Dec 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: Ninjikiran is an unknown quantity at this point 
Solved Threads: 0
Ninjikiran Ninjikiran is offline Offline
Newbie Poster

Re: Encoding help, run into block.

 
0
  #5
Dec 11th, 2007
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC