flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Reverse code macro??? possibilty

Author
Thread Post new topic Reply to topic
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 16 Apr 2006, 05:04
Let's say like having a whole block of code reversed... So I could push the contents on to stack. Does fasm preprocessor have that ability???
Post 16 Apr 2006, 05:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 16 Apr 2006, 06:32
Quote:
Does fasm preprocessor have that ability???
I don't think the preprocessor can do it, but the assembler stage probably can. Use LOAD and STORE, perhaps combined with VIRTUAL.
Post 16 Apr 2006, 06:32
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 17 Apr 2006, 18:22
Code:
load dword CodeAtMinus1234 from $-1234
push CodeAtMinus1234    


only preceding code can be loaded
Post 17 Apr 2006, 18:22
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 18 Apr 2006, 01:33
????? What do you mean???
Post 18 Apr 2006, 01:33
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 18 Apr 2006, 07:05
Code:
db 0,1,2,3,4,5,6,7,8,9,10
;length=11

for x=0 to length/2
  xcgh bytes at x and (length-x)
next

The result would be:
10,9,8,7,6,5,4,3,2,1,0
    

Is this what you are looking for? I think this is what LOAD/STORE can do for you. You can decide yourself if you want byte/word/dword wise Wink
Post 18 Apr 2006, 07:05
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 18 Apr 2006, 10:50
isn't that MASM code?
Code:
dest rb 4
virtual
  src db 1,2,3,4
  repeat 4
    load a from src+%
    store a at dest+3-%
  end repeat
end  virtual    

not tested, i don't have FASM now. i am not sure if repeat goes 0 to 3 or 1 to 4. but the idea is something like this
Post 18 Apr 2006, 10:50
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 18 Apr 2006, 16:58
dest rb 4
virtual
src db 1,2,3,4
repeat 4
load a from src+%
store a at dest+3-%
end repeat
end virtual


store a at dest+3-%
error: value out of range.
Post 18 Apr 2006, 16:58
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 18 Apr 2006, 17:40
then it means repeat starts from 1.. so, replace the % with (%-1) (everywhere you use it here). because you need to start from 0 (when % is 1), and end up with 3 (when % is 4).
Post 18 Apr 2006, 17:40
View user's profile Send private message Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 19 Apr 2006, 01:43
dest rb 4
virtual
src db 1,2,3,4
repeat 4
load a from src+(%-1)
store a at dest+3-(%-1)
end repeat
end virtual

store a at dest+3-(%-1)
error: value out of range.
Post 19 Apr 2006, 01:43
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Apr 2006, 07:25
i forgot "byte" operator with "load" and "store"
Post 19 Apr 2006, 07:25
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 19 Apr 2006, 08:40
"byte" is anyway default for them, even if you omit it.

[a little offtopic] One tricky thing about "load" and "store" is when they operate on code that has relocations. I just realized I forgot to explain it in the manual.
Post 19 Apr 2006, 08:40
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Apr 2006, 12:38
tomasz: so why doesn't that code work?
Post 19 Apr 2006, 12:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 19 Apr 2006, 15:09
Because "virtual" is a separate addressing space.
Post 19 Apr 2006, 15:09
View user's profile Send private message Visit poster's website Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 19 Apr 2006, 15:12
Code:
macro beginreverse name 
{  
  name#.begin:  
}  

macro endreverse name
{  
   name#.end:   
   count = (rva name#.end - rva name#.begin)    
   repeat count     
     load b byte from %+ name#.begin -1
      t = 
     store byte t at %+ name#.begin -1

   end repeat  
}    


I'm using this from a encrypting macro I have.. it works however know how do I reverse it.

So instead of 1234 ...it would be 4321

However this is for each seperate instruction. Such as mov eax,0
xchg eax,eax
add eax,eax
Post 19 Apr 2006, 15:12
View user's profile Send private message Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 19 Apr 2006, 16:08
Thomas does the preprocessor know what is an instruction and just data???

If it does maybe... I can make it so it goes to the end of the instruction and takes that byte and replaces it with the first one. etc etc
Post 19 Apr 2006, 16:08
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 22 Apr 2006, 09:23
How about this macro that reverses strings?

Code:
macro reverse_string string*
{
  local str, a, b
  str db string

  repeat ($-str)/2
   load a from $-%
   load b from str+%-1
   store a at str+%-1
   store b at $-%
  end repeat
}    


and then you use it like:
Code:
reverse_string "string"    
and it outputs
Code:
gnirts    
Wink

if you want instructions, you can replace the "db" there when I used it with the instruction (don't forget to put a label):

example
Code:
local instr, a, b
instr: xor eax, eax

repeat ($-instr)/2
 load a from $-%
 load b from instr+%-1
 store a at instr+%-1
 store b at $-%
end repeat    
xor eax, eax was only an example, you can replace it with a macro parameter, or whatever you wish


PS: This is the assembler part, not preprocessor. Only the macro and local directives are in the preprocessor. The rest is in the assembler. Smile
Post 22 Apr 2006, 09:23
View user's profile Send private message Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 22 Apr 2006, 19:20
let me try it out
Post 22 Apr 2006, 19:20
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Apr 2006, 19:25
grey_beast: good idea :]
Post 22 Apr 2006, 19:25
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
shism2



Joined: 14 Sep 2005
Posts: 248
shism2 22 Apr 2006, 19:46
Code:
macro beginrev name 
{  
  name#.begin:  
}  



macro endrev name
{
name#.end:
count = (rva name#.end - rva name#.begin)


repeat  count
 load a from $-% 
 load b from name#.begin+%-1 
 store a at name#.begin+%-1 
 store b at $-% 
end repeat
}


beginrev revnow
xor ecx,ecx
endrev revnow    


Nothing happens to it at all...

However, If i divide count by 2... Then it does get reversed along with some instructions under it... What am I doing wrong?
Post 22 Apr 2006, 19:46
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 23 Apr 2006, 09:56
shism2 wrote:
Let's say like having a whole block of code reversed... So I could push the contents on to stack. Does fasm preprocessor have that ability???


If you're going to execute code from the stack, realize this will not work on processors with NX bit enabled and an OS that supports it.

_________________
Image - carpe noctem
Post 23 Apr 2006, 09:56
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.