flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Calculate RVA for JMP

Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate 05 May 2021, 19:46
In HLL, I have these to be parsed (front-end):
Code:
IF a = 3 THEN
    MSGBOX win
ELSE
    IF b = 1 THEN
         MSGBOX you lose
    ELSE
        MSGBOX You won
    ENDIF
ENDIF    


Then my back-end would generate:

Code:
CMP a, 3
JNE cond2
INVOKE MSGBOX, 'win'........
JMP nocond
cond2:  CMP b, 1
JNE cond2b
INVOKE MSGBOX ' You lose'
JMP nocond
cond2b:  INVOKE MSBOX 'You won'
nocond:    


If I use FASM as back-end compiler, then it is end of story. But mine is not, it will generate the final EXE by itself, by computing RVA, e.g. distance of nocond: from different JMP location, depending on how many bytes in between.

E.g.
Code:
JMP 103
RET
103: NOP    


That's JMP 103 because of adjacent memory bytes.

JMP 102 would actually points to RET itself. Sometimes different CPU instructions may have bigger size.

E.g
Code:
JMP 104
MOV EBP,ESP
104:NOP
    


See, GOTO(JMP) need to skip 4 bytes for the label (104).
----
So how do I calculate the length of JMP (in bytes) for each of these conditional instruction? Do I need 2 pass, 3 pass,etc to first find out the size of code block in between as illustrated below?

e,g,
Code:
JMP target
xxxxx 
xxxxx  ;large codeblock
xxxxx
xxxxxx
target:  <----
xxxxxx    


Good news is I planning to revive my Sambal Compiler to support more features....
Post 05 May 2021, 19:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 05 May 2021, 19:55
FlierMate wrote:
So how do I calculate the length of JMP (in bytes) for each of these conditional instruction? Do I need 2 pass, 3 pass,etc to first find out the size of code block in between as illustrated below? <snip>
You don't "need" to do multiple passes, but it often generates good solutions.

Otherwise, for single pass, you can keep a list of unknown targets and fill in the distance when you encounter the future address. As long as you always leave enough space in the originating jmp then it works. But if you try to reserve only a 2-byte jmp and the forward distance is more than 127 bytes then it fails, so you have to reserve a 5-byte jmp instead and sometimes have 3 extra bytes doing nothing of value.
Post 05 May 2021, 19:55
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 05 May 2021, 20:54
For a multi-pass approach, I have written an article that explains what fasm does to generate correct relative values.
Post 05 May 2021, 20:54
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.