flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > (for Tomasz) amazing!

Author
Thread Post new topic Reply to topic
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 23 Jun 2010, 17:50
Code:
ASSEMBLE.INC
------------
.....
call   error_handler
error_handler:
 mov     eax,[error]
 sub     eax,error_handler
   add     [esp],eax
   ret

this above == jmp dword[error]
    


Is there a particular reason for this Tomasz ?
or this is just your programming style ?
(I can understand that ... we all have "our" style)
thank you.

_________________
I am not young enough to know everything (Oscar Wilde)- Image


Last edited by ouadji on 23 Jun 2010, 18:30; edited 2 times in total
Post 23 Jun 2010, 17:50
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20289
Location: In your JS exploiting you and your system
revolution 23 Jun 2010, 18:10
ouadji: fasm is split into two main parts to allow for more than 64kB code (to support the DOS version). That is why this manipulation is necessary.
Post 23 Jun 2010, 18:10
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 23 Jun 2010, 18:25

ok! I had thought it.
it's the only reason requiring this approach !
thank you revolution

having said that, this below works too :
Code:
push dword[error]
ret
    


_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 23 Jun 2010, 18:25
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 23 Jun 2010, 18:56
ouadji, it is incorrect to assume that [esp]==error_handler is always true. The final address for error_handler at run-time may differ from that of the compile-time, but since labels will preserve their distance from each other that trick allows for the calculation of the correct target address.

Looks that you actually understood what I've said above but I'm repeating it because I don't know why you said your second code also works. Confused
Post 23 Jun 2010, 18:56
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 23 Jun 2010, 19:18

Quote:
it is incorrect to assume that [esp]==error_handler is always true.

[esp] == error_handler ??? where ???
(sorry) i don't understand why you said "[esp] == error_handler"
where ???

sorry LocoDelAssembly,
but I don't see any differences between these two pieces of code :
Code:
1)
call        A
A:
mov      eax,[B]
sub  eax,A
add    [esp],eax ;[esp] = A + (B - A) => [esp] = B
ret

;and

2)
push dword[B] ; [esp] = B
ret

1) == 2)
    


_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 23 Jun 2010, 19:18
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 23 Jun 2010, 20:10
You are assuming that A and B are either in the compile-time addresses or the references to them fixed by some loader by applying relocations.

In this scenario only 1) will jump to B correctly:

Code:
mov esi, the_code
mov edi, [unknown_pointer]
mov ecx, the_code.end - the_code
rep movsb

mov edi, the_code
mov ecx, the_code.end - the_code
rep stosb

jmp [unknown_pointer]

the_code:
;1)
call    A
A:
mov     eax,[B]
sub     eax,A
add     [esp],eax ;[esp] = A + (B - A) => [esp] = B
ret

;and

;2)
push dword[B] ; [esp] = B
ret  ; Will jump to garbage created by the rep stosb

some_proc:
xor eax, eax
ret
the_code.end:
B dd some_proc    
Post 23 Jun 2010, 20:10
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 23 Jun 2010, 20:36
Quote:

You are assuming that A and B are either in the compile-time addresses or the references to them fixed
by some loader by applying relocations.

yes, indeed.

Code:
...
mov esi, the_code                        | ; Smile ok, i understood !
mov edi, [unknown_pointer]                | ;in this case, yes, of course, indeed!!!
mov ecx, the_code.end - the_code  |
rep movsb                          |
...
mov edi, the_code                   |
mov ecx, the_code.end - the_code   | aahhhhh !!!! bye bye  Sad
rep stosb                                |

In this scenario only 1) will jump to B correctly:
    
The Tomasz'code dynamically recalculate the displacement
and the correct address of [error], ok !
thank you LocoDelAssembly !

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 23 Jun 2010, 20:36
View user's profile Send private message Send e-mail 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.