2020-06-10 23:49:25 +08:00
|
|
|
Perl scripts for assembler sources
|
|
|
|
==================================
|
|
|
|
|
1998-12-21 18:56:39 +08:00
|
|
|
The perl scripts in this directory are my 'hack' to generate
|
2013-06-13 07:22:32 +08:00
|
|
|
multiple different assembler formats via the one original script.
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
The way to use this library is to start with adding the path to this directory
|
|
|
|
and then include it.
|
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
push(@INC,"perlasm","../../perlasm");
|
|
|
|
require "x86asm.pl";
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2017-02-07 07:05:06 +08:00
|
|
|
The first thing we do is setup the file and type of assembler
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&asm_init($ARGV[0]);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
The first argument is the 'type'. Currently
|
2020-06-10 23:49:25 +08:00
|
|
|
`cpp`, `sol`, `a.out`, `elf` or `win32`.
|
|
|
|
The second argument is the file name.
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
The reciprocal function is
|
2020-06-10 23:49:25 +08:00
|
|
|
`&asm_finish()` which should be called at the end.
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
There are two main 'packages'. `x86ms.pl`, which is the Microsoft assembler,
|
|
|
|
and `x86unix.pl` which is the unix (gas) version.
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
Functions of interest are:
|
2020-06-10 23:49:25 +08:00
|
|
|
|
|
|
|
&external_label("des_SPtrans"); declare and external variable
|
|
|
|
&LB(reg); Low byte for a register
|
|
|
|
&HB(reg); High byte for a register
|
|
|
|
&BP(off,base,index,scale) Byte pointer addressing
|
|
|
|
&DWP(off,base,index,scale) Word pointer addressing
|
|
|
|
&stack_push(num) Basically a 'sub esp, num*4' with extra
|
|
|
|
&stack_pop(num) inverse of stack_push
|
|
|
|
&function_begin(name,extra) Start a function with pushing of
|
|
|
|
edi, esi, ebx and ebp. extra is extra win32
|
|
|
|
external info that may be required.
|
|
|
|
&function_begin_B(name,extra) Same as normal function_begin but no
|
|
|
|
pushing.
|
|
|
|
&function_end(name) Call at end of function.
|
|
|
|
&function_end_A(name) Standard pop and ret, for use inside
|
|
|
|
functions.
|
|
|
|
&function_end_B(name) Call at end but with pop or ret.
|
|
|
|
&swtmp(num) Address on stack temp word.
|
|
|
|
&wparam(num) Parameter number num, that was push in
|
|
|
|
C convention. This all works over pushes
|
|
|
|
and pops.
|
|
|
|
&comment("hello there") Put in a comment.
|
|
|
|
&label("loop") Refer to a label, normally a jmp target.
|
|
|
|
&set_label("loop") Set a label at this point.
|
|
|
|
&data_word(word) Put in a word of data.
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
So how does this all hold together? Given
|
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
int calc(int len, int *data)
|
|
|
|
{
|
|
|
|
int i,j=0;
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
for (i=0; i<len; i++)
|
|
|
|
{
|
|
|
|
j+=other(data[i]);
|
|
|
|
}
|
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
So a very simple version of this function could be coded as
|
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
push(@INC,"perlasm","../../perlasm");
|
|
|
|
require "x86asm.pl";
|
|
|
|
|
|
|
|
&asm_init($ARGV[0]);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&external_label("other");
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
$tmp1= "eax";
|
|
|
|
$j= "edi";
|
|
|
|
$data= "esi";
|
|
|
|
$i= "ebp";
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&comment("a simple function");
|
|
|
|
&function_begin("calc");
|
|
|
|
&mov( $data, &wparam(1)); # data
|
|
|
|
&xor( $j, $j);
|
|
|
|
&xor( $i, $i);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&set_label("loop");
|
|
|
|
&cmp( $i, &wparam(0));
|
|
|
|
&jge( &label("end"));
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&mov( $tmp1, &DWP(0,$data,$i,4));
|
|
|
|
&push( $tmp1);
|
|
|
|
&call( "other");
|
|
|
|
&add( $j, "eax");
|
|
|
|
&pop( $tmp1);
|
|
|
|
&inc( $i);
|
|
|
|
&jmp( &label("loop"));
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&set_label("end");
|
|
|
|
&mov( "eax", $j);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&function_end("calc");
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&asm_finish();
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
The above example is very very unoptimised but gives an idea of how
|
|
|
|
things work.
|
|
|
|
|
|
|
|
There is also a cbc mode function generator in cbc.pl
|
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&cbc($name,
|
|
|
|
$encrypt_function_name,
|
|
|
|
$decrypt_function_name,
|
|
|
|
$true_if_byte_swap_needed,
|
|
|
|
$parameter_number_for_iv,
|
|
|
|
$parameter_number_for_encrypt_flag,
|
|
|
|
$first_parameter_to_pass,
|
|
|
|
$second_parameter_to_pass,
|
|
|
|
$third_parameter_to_pass);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
|
|
|
So for example, given
|
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
void BF_encrypt(BF_LONG *data,BF_KEY *key);
|
|
|
|
void BF_decrypt(BF_LONG *data,BF_KEY *key);
|
|
|
|
void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
|
|
|
|
BF_KEY *ks, unsigned char *iv, int enc);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-06-10 23:49:25 +08:00
|
|
|
&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
|
|
|
|
&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
|