Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 11:54

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Hex programming? how?
PostPosted: 20 Nov 2009 04:18 

Joined: 12 Nov 2009 07:06
Posts: 6
Hi all, I'm new here.

I was wanting to know if there are any disassemblers out there that would translate binary equivalents back into assembly. I think the disassemblers that I know change it back into assembly language which is readable, contains pseudo opcodes, opcodes etc..., but I don't think they change it so the assembly instructions are exactly equivalent to the binary instructions in memory. I want to learn how to code simply in hex by using a hex editor which would help me understand how the CPU works. Does anybody know if this is even possible? Actually that's probably a rhetorical question since I'm sure it's possible, but does anybody here know how to do it? I'm not really looking into making a huge program in hex or anything but it would be nice if I could program a small hello world in hex, and while doing so, understand what each hexadecimal values do to change the program.


Thanks


Top
 Profile  
 
 
 Post subject: Re: Hex programming? how?
PostPosted: 20 Nov 2009 18:38 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
the binary compiled code and the assembly translation are exactly the same thing because if you assemble that portion of assembly code you will obtain the same binary data.

probably what you refer to is the "verbosing" of the obtained assembly code but even in that case there are only rare events in which "could" (99% not) happens a difference, for example a "jmp 11223344" from offset 11223300 which in the original code was a far jump and in the assembled code is a (right) near jmp... but this is only an hypothetic example.

or maybe you are referring to the different results obtained by the various assemblers like nasm, masm and so on, in that case is a chaos.

in any case I suggest you to take a look to the full set of instructions provided by intel:
http://www.intel.com/Assets/pdf/manual/253666.pdf
http://www.intel.com/Assets/pdf/manual/253667.pdf


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 20 Nov 2009 20:53 

Joined: 12 Nov 2009 07:06
Posts: 6
Thanks for the manual, I'll have to check it out.

k but lets say this hello world program in hex for example:

Code:
4D 5A 37 00 02 00 01 00 20 00 11 00 FF FF 04 00 00 01 00 00 00 00 00 00 1E 00 00 00 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B8 01 00 8E D8 BA 02 00 B4 09 CD 21 B0 00 B4 4C CD 21 68 65 6C 6C 6F 20 77 6F 72 6C 64 0D 0A 24


What are all the 0's for before and what do they do to the program? Is that all the unused stack?
Then after the 00's we have B8 in hex, is that equivalent to an assembly instruction? If so which instruction is it? I just need to know that, and it would clear much of any of my confusions.

btw as i was writing this, I was looking through the manual you sent me and it seems to be what I'm looking for. thanks for that


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 20 Nov 2009 23:03 

Joined: 24 Sep 2007 02:12
Posts: 1114
Location: http://sethioz.co.uk
not really my field, but indeed 00 in hex is NULL byte. it means it's unused.
in most case, program does not need 00, but some programs will crash if you remove 00 bytes.
and some programs needs 00 at end.


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 20 Nov 2009 23:42 

Joined: 12 Nov 2009 07:06
Posts: 6
After doing some research, I found out what each hexadecimal value does. You are right luigi, they are symbolic representations for assembly code. but when I code in assembly, I need to add segments like this:
.model small
.586
.data
.code
etc...

Those are assembly code, but they don't get converted to hexadecimal values... do they? I mean how would it look in hex, because those(.model,.data,.code etc...) are just pseudo opcodes(not actually instructions). Like say in the sample hex program that I put here... Where is the stack declared or the .model?

I still can't figure out what the 0's are for before the program because like sethioz said if you remove a zero, it will mess up the program.


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 21 Nov 2009 00:48 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
the problem in the example you posted is that it's an executable and so you could get confused by its format.
practically a Windows executable is a PE file (MZ for DOS compatibility + PE, in your case it's a 16bit DOS exe), so just a file format and indeed all the zeroes you see are parts of this format and (to be exact) of its alignment/padding.

so your code starts exactly at offset 0x200 and gets decompiled as:
Code:
:0001.0000 B80100                 mov ax, 0001
:0001.0003 8ED8                   mov ds, ax
:0001.0005 BA0200                 mov dx, 0002
:0001.0008 B409                   mov ah, 09
:0001.000A CD21                   int 21
:0001.000C B000                   mov al, 00
:0001.000E B44C                   mov ah, 4C
:0001.0010 CD21                   int 21
                                  "hello world\r\n"


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 21 Nov 2009 01:24 

Joined: 12 Nov 2009 07:06
Posts: 6
ahhh that clears so much things up. Very interesting how it all works. Do you know any pdf's or websites that have the exact equivalents of binary/hex to assembly instructions? say maybe as a reference table of some sort, explaining what each instruction does. The manual that you gave me is helpful, but I have to skim around, and it takes more time trying to find exactly what I want. A small reference table would be an excellent source of quick help.

Thanks luigi


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 21 Nov 2009 01:37 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
I don't know if exists an updated document easy to follow (so chm or html), in the past there was a chm help file derived from an old version of that intel document and was very easy to follow.
I no longer remember its name, you need to search on some search engines if you can find something similar or better

anyway the menu of those pdf is easy to use and follow so probably the problem doesn't exist


Top
 Profile  
 
 Post subject: Re: Hex programming? how?
PostPosted: 21 Nov 2009 02:06 

Joined: 12 Nov 2009 07:06
Posts: 6
Alrighty man... Sounds good :) Thanks again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: