Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 09-13-2005, 16:51
redbull redbull is offline
Friend
 
Join Date: Mar 2004
Posts: 160
Rept. Given: 17
Rept. Rcvd 5 Times in 4 Posts
Thanks Given: 3
Thanks Rcvd at 6 Times in 6 Posts
redbull Reputation: 5
Calculating relative jnz opcode sizes in a custom code generator

Hi Guys,

I have a question and I cant seem to find answers (even in the compiler e-books available on the web). I think it is a ID10T error on my side

My problem is this (Appologies for this being a little badly worded)

I have a code generator written in delphi.

Lets say I generate a simple routine with it

<-- snip -->
Start1:
(..stuff..)
dec eax
jnz Position1

(..stuff)

Position1:
(..stuff..)

dec ecx
jnz Start1
<-- snip -->

Ok now my problem is I want to create a generic routine where I specify just to create a JNZ instruction but I dont care (at first pass) exactly which
JNZRel instruction size to use. (because I dont know how far apart the code is yet).

As you know there are several kinds of JNZ (RelJnzAddr32 and RelJnzAddr8 etc)

RelJnzAddr8 = 0x75 <byte>
RelJnzAddr32 = 0x0f 0x82 <dword>

Now I have code to handle flagging certain parts of the instruction and being able to change them. It is a class of mine called TRelocationHelper. It works fine and no problems. I can work out the distance between the instructions and patch the correct bytes.

The concern is, of course, is how to resolve which instruction to use.

I want to be able to mark that JNZ instruction and then come back to it later in the compile phase and insert the right instruction there (for the correct relative offset size).

I am not even sure where to begin.

At the moment I am using all my relative instructions as ADDR32 size; But this is less than optimal!!

I use structure lists and pointers to keep track of which memory area the instrusctions are targeting and which bytes to change to patch the relative addresses. So inserting larger opcodes etc is not a problem becuase I will still know where they point to.

Please post a few links to help me shed light on this matter




Redbull
Reply With Quote
  #2  
Old 09-13-2005, 23:34
doug
 
Posts: n/a
if the destination is more than 0x7F bytes away from the instruction following your Jcc, then use the long form.

But honestly, do you think that an extra 4-bytes really matter, if you consider the greatly added simplicity of *always* using the 6-byte instruction (0F 8x)?

If you start resizing instructions, then you have to make sure that *all* jumps that go beyond the resize/insertion point are also fixed. It's certainly not impossible, but seems like an overkill for what you want.

Last edited by doug; 09-13-2005 at 23:37.
Reply With Quote
  #3  
Old 09-14-2005, 21:44
redbull redbull is offline
Friend
 
Join Date: Mar 2004
Posts: 160
Rept. Given: 17
Rept. Rcvd 5 Times in 4 Posts
Thanks Given: 3
Thanks Rcvd at 6 Times in 6 Posts
redbull Reputation: 5
Hi Doug,

Agreed but the point is to actually make it right now.. My existing code generator works well and now I want to take it to the next level.


Here are some interesting arictles on this topic:

hxxp://compilers.iecc.com/comparch/article/97-05-281

hxxp://compilers.iecc.com/comparch/article/00-10-073

hxxp://compilers.iecc.com/crenshaw/

hxxp://www.pcengines.ch/tp3.htm

hxxp://www.etek.chalmers.se/~e8johan/compiler/

Quote:
Generating instructions with variable length addresses is a well understood
problem. The PDP-11 Unix assembler did it in about 1973, and there was
a paper by Tom Szymanski in the CACM about that time. I always preferred
the other approach, start with everything long, then shorten until there's
nothing left to shorten, which has the advantage of being fail-safe, all
of the intermediate stages being valid code. -John
Reply With Quote
  #4  
Old 09-15-2005, 23:54
SlashZero SlashZero is offline
Friend
 
Join Date: Aug 2002
Posts: 28
Rept. Given: 4
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 0 Times in 0 Posts
SlashZero Reputation: 0
if your code generator works fine.. why dont you precalc the opcodes between the jump and the "label" and count the byte size to see which case of jump you need?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opcode Table wilson bibe General Discussion 23 04-09-2015 06:00
Calculating the size in bytes of a C++ function yaa General Discussion 8 11-07-2007 09:15
opcode generator johnq General Discussion 4 01-02-2004 07:11


All times are GMT +8. The time now is 16:07.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX
( 1998 - 2021 )