Author Topic: Little Endian Code to .dw Code  (Read 5350 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Little Endian Code to .dw Code
« on: April 11, 2011, 10:36:55 am »
I just created a very simple tool that allows you to convert hexadecimal code like FDCB00AE to code like .dw $CBFD, $AE00.

It's a small python program that can either read files or read hexadecimal code by type input.

It converts the code and then creates a .txt file with named 'dwCode.txt' with the final code to be used in an Assembly code.


You need Python 2.x to run it. It's attached, hope you like it. For now, it can only open ASCII files (not 8xp) and the number of bytes has to be divisible by four, so '210100' won't work, but 'FDCB00AE' will. Sorry for this, I'll try to fix it.

Here's the source code:

Code: [Select]
def bibEndianToLittleEndian(hex):
"""Takes a big endian hex string and turns it to little endian"""
splitHex = [hex[x:x+4] for x in xrange(0,len(hex),4)]
for i in range(0,len(splitHex)):
splitHex[i] = splitHex[i][2]+splitHex[i][3]+splitHex[i][0]+splitHex[i][1]
return "".join(splitHex)

def littleEndianToDw(hex):
"""Takes a little endian string as an argument and turns it to .dw blocks"""
#.dw $CBFD,$AE00
finalString = ".dw"
splitHex = [hex[x:x+4] for x in xrange(0,len(hex),4)]
for i in range(0,len(splitHex)):
finalString+= " $"+splitHex[i]+","
return finalString[0:len(finalString)-1]

def main():
print "Choose:\n1. Type hexadecimal code\n2. Open from file"
option = input()
if option==1:
hexCode = raw_input("Type hexadecimal code: ")
g = open('dwCode.txt','w')
g.write(littleEndianToDw(bibEndianToLittleEndian(hexCode)))
g.close()

else:
fileName = raw_input("Enter name of ASCII file: ")
f = open(fileName,'r')
hexCode = f.read()
f.close()
g = open('dwCode.txt','w')
g.write(littleEndianToDw(bibEndianToLittleEndian(hexCode)))
g.close()

if __name__=='__main__':
main()

Enjoy :)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #1 on: April 11, 2011, 10:33:03 pm »
Ah cool, I like this, Scout. However, TASM can only handle so many extra arguments. For example, if this has three arguments:
 .dw $AA00,$BB00,$CC00
Then I cap it at 24 arguments per .dw, but I think 31 is the max. Also, if you could make a code for .db statements (also only handling 31 arguments), that might be easier for the code, too.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #2 on: April 12, 2011, 04:18:52 am »
Ah cool, I like this, Scout. However, TASM can only handle so many extra arguments. For example, if this has three arguments:
 .dw $AA00,$BB00,$CC00
Then I cap it at 24 arguments per .dw, but I think 31 is the max. Also, if you could make a code for .db statements (also only handling 31 arguments), that might be easier for the code, too.

.db's would be much easier:

Code: [Select]
def hexToDb(hex):
"""Takes a big endian hex string and turns it to little endian"""
splitHex = [hex[x:x+2] for x in xrange(0,len(hex),2)]
finalString = ".db"
counter = 0
for i in range(0,len(splitHex)):
if counter==29:
finalString+=" $"+splitHex[i]+"\n.db"
counter = 0
else:
finalString+=" $"+splitHex[i]+","
counter+=1
return finalString[0:len(finalString)-1]

def main():
print "Choose:\n1. Type hexadecimal code\n2. Open from file"
option = input()
if option==1:
hexCode = raw_input("Type hexadecimal code: ")
g = open('dbCode.txt','w')
g.write(hexToDb(hexCode))
g.close()

else:
fileName = raw_input("Enter name of ASCII file: ")
f = open(fileName,'r')
hexCode = f.read().replace(' ','').replace('\n','').replace('\r','')
f.close()
g = open('dbCode.txt','w')
g.write(hexToDb(hexCode))
g.close()

if __name__=='__main__':
main()

It has the 31 arguments limit now, here's a sample return:

Code: [Select]
.db $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77
.db $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9
.db $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D
.db $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00
.db $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40
.db $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F
.db $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48
.db $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF
.db $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00
.db $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45
.db $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72
.db $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65
.db $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A
.db $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD, $CB, $00, $AE, $21, $00, $00, $22
.db $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C, $64, $21, $00, $EF, $40, $45, $FD
.db $CB, $00, $AE, $21, $00, $00, $22, $4B, $84, $21, $AC, $9D, $EF, $0A, $45, $EF, $2E, $45, $C9, $48, $65, $6C, $6C, $6F, $20, $77, $6F, $72, $6C
.db $64, $21, $00

I just tested it and I like the way it works, I have a file with raw hex code (210100EF0745...) and I get what's above just by typing the name of the file. Attached is the .db one.
« Last Edit: April 12, 2011, 04:38:32 am by Scout »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #3 on: April 12, 2011, 11:38:01 am »
This is great, Scout! It works so nicely! Is there a way to change the output file? I can definitely use this for BatLib ♥ The next step is to create a program that converts a file of all hex into a .hex file (in the proper format). If that can be done, I will no longer have to worry about using TASM !

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #4 on: April 12, 2011, 11:40:37 am »
This is great, Scout! It works so nicely! Is there a way to change the output file? I can definitely use this for BatLib ♥ The next step is to create a program that converts a file of all hex into a .hex file (in the proper format). If that can be done, I will no longer have to worry about using TASM !

You mean 210100 to an Assembly file (.8xp) which only instruction is ld hl,1. BTW, it crashes calc. If that's what you mean, I've been working on it for a few months now, and repeatedly asking for help over at Cemetech. That's also all I need to do to finish Assemblex. And then you can use Assemblex *ONLY* to develop programs.

Also, not sure if I can create Applications, that is very hard.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #5 on: April 12, 2011, 11:43:57 am »
Oh, well that would be great if it could compile hex to a .8xp, but I mean when I use TASM to compile my .dw (now .db) statements, I have to change the .obj file it outputs into a .hex file before I can sign it as an app.

Also, could you upload an example that crashes the calc? Does it crash when you run it or when you send it? If it is when you run it, I will open up the program and take a look at the hex when it gets on calc :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #6 on: April 12, 2011, 11:49:30 am »
Attached, this will crash the calculator. It crashes when you run it.

Send me one of those .hex files please.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #7 on: April 12, 2011, 12:01:58 pm »
All you need is to add an ret to the end of that XD you need to compile 210100C9. However, a piece of advice: Don't make your program automatically add a C9 because in some cases it isn't needed. If it isn't needed, people might get peeved that there is an extra unneeded byte.

I uploaded the .hex file, too :) Taking one line, I think it follows the following format:
:1840900000003C425A5A423C00C30950C30A50C32750C32850C338506F
So the red is a :18
The green is the the cumulative bytes+the offset (4000h for an app)
Then you have 6 zero's (because addresses don't get that large on the calc XD
Then you have the actual line of hex. TASM uses 24 bytes (48 chars)
The last byte is a checksum or something

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #8 on: April 12, 2011, 02:05:56 pm »
I checked it out Zeda, not sure if I can make it.

But it's great you're using it already to turn BatLib to .db's, I personally prefer those because they don't need special endian :S

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #9 on: April 12, 2011, 04:28:01 pm »
Hehe, I am already making a version in Python (or at least attempting it)!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #10 on: April 12, 2011, 04:36:02 pm »
Hehe, I am already making a version in Python (or at least attempting it)!
* Scout deleted this.
« Last Edit: April 12, 2011, 04:55:13 pm by Scout »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #11 on: April 12, 2011, 04:47:35 pm »
Well, I am reading and writing to files and I can input the file to read and write to. Please, I feel insulted that you think I cannot do it. So for now, I am going to leave until I accomplish this.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #12 on: April 12, 2011, 04:49:19 pm »
Well, I am reading and writing to files and I can input the file to read and write to. Please, I feel insulted that you think I cannot do it. So for now, I am going to leave until I accomplish this.

Now that I think about it, .hex files are text files, so it is quite easy (I was thinking about 8xp files, that would be extremely harder). Text files, yeah you can do it :)

Sorry for being rude.
« Last Edit: April 12, 2011, 04:52:59 pm by Scout »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #13 on: April 12, 2011, 05:52:01 pm »
Thanks :) I think I will make one, too. Here is my hex to .db converter, by the way :) You input the file to read, then the file to write to and it will convert it :) Also, the hex doesn't need to be an even number on each line, but if there is an odd number of chars in all, the last one won't be included. It writes 24 arguments to each .db :) I won't guarantee that it is optimised, because as you said, I just started learning today, but it is a good start, right?

If I think I can do something, I usually have good reason to believe so :) I will also try to write a converter to convert to .8xp and possibly .8xk. :) That will be a little later on down the road, though...

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Little Endian Code to .dw Code
« Reply #14 on: April 13, 2011, 03:45:09 am »
Thanks :) I think I will make one, too. Here is my hex to .db converter, by the way :) You input the file to read, then the file to write to and it will convert it :) Also, the hex doesn't need to be an even number on each line, but if there is an odd number of chars in all, the last one won't be included. It writes 24 arguments to each .db :) I won't guarantee that it is optimised, because as you said, I just started learning today, but it is a good start, right?

If I think I can do something, I usually have good reason to believe so :) I will also try to write a converter to convert to .8xp and possibly .8xk. :) That will be a little later on down the road, though...

Nice.

Quote
Also, the hex doesn't need to be an even number on each line, but if there is an odd number of chars in all, the last one won't be included.

I didn't test it, but I think it will accept code in several lines...

Also, the code is way bigger than mine:

Code: [Select]
def hexToDb(hex):
splitHex = [hex[x:x+2] for x in xrange(0,len(hex),2)]
finalString = ".db"
counter = 0
for i in range(0,len(splitHex)):
if counter==29:
finalString+=" $"+splitHex[i]+"\n.db"
counter = 0
else:
finalString+=" $"+splitHex[i]+","
counter+=1
return finalString[0:len(finalString)-1]

But the main problem with the code is its organization. I have to teach you about main functions, functions and code organizing :D

Quote
If I think I can do something, I usually have good reason to believe so  I will also try to write a converter to convert to .8xp and possibly .8xk.  That will be a little later on down the road, though...

A converter to .8XP is really hard man, I'd try a 8xk first, since it's much easier.