README 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. The perl scripts in this directory are my 'hack' to generate
  2. multiple different assembler formats via the one original script.
  3. The way to use this library is to start with adding the path to this directory
  4. and then include it.
  5. push(@INC,"perlasm","../../perlasm");
  6. require "x86asm.pl";
  7. The first thing we do is setup the file and type of assembler
  8. &asm_init($ARGV[0]);
  9. The first argument is the 'type'. Currently
  10. 'cpp', 'sol', 'a.out', 'elf' or 'win32'.
  11. Argument 2 is the file name.
  12. The reciprocal function is
  13. &asm_finish() which should be called at the end.
  14. There are 2 main 'packages'. x86ms.pl, which is the Microsoft assembler,
  15. and x86unix.pl which is the unix (gas) version.
  16. Functions of interest are:
  17. &external_label("des_SPtrans"); declare and external variable
  18. &LB(reg); Low byte for a register
  19. &HB(reg); High byte for a register
  20. &BP(off,base,index,scale) Byte pointer addressing
  21. &DWP(off,base,index,scale) Word pointer addressing
  22. &stack_push(num) Basically a 'sub esp, num*4' with extra
  23. &stack_pop(num) inverse of stack_push
  24. &function_begin(name,extra) Start a function with pushing of
  25. edi, esi, ebx and ebp. extra is extra win32
  26. external info that may be required.
  27. &function_begin_B(name,extra) Same as normal function_begin but no pushing.
  28. &function_end(name) Call at end of function.
  29. &function_end_A(name) Standard pop and ret, for use inside functions
  30. &function_end_B(name) Call at end but with poping or 'ret'.
  31. &swtmp(num) Address on stack temp word.
  32. &wparam(num) Parameter number num, that was push
  33. in C convention. This all works over pushes
  34. and pops.
  35. &comment("hello there") Put in a comment.
  36. &label("loop") Refer to a label, normally a jmp target.
  37. &set_label("loop") Set a label at this point.
  38. &data_word(word) Put in a word of data.
  39. So how does this all hold together? Given
  40. int calc(int len, int *data)
  41. {
  42. int i,j=0;
  43. for (i=0; i<len; i++)
  44. {
  45. j+=other(data[i]);
  46. }
  47. }
  48. So a very simple version of this function could be coded as
  49. push(@INC,"perlasm","../../perlasm");
  50. require "x86asm.pl";
  51. &asm_init($ARGV[0]);
  52. &external_label("other");
  53. $tmp1= "eax";
  54. $j= "edi";
  55. $data= "esi";
  56. $i= "ebp";
  57. &comment("a simple function");
  58. &function_begin("calc");
  59. &mov( $data, &wparam(1)); # data
  60. &xor( $j, $j);
  61. &xor( $i, $i);
  62. &set_label("loop");
  63. &cmp( $i, &wparam(0));
  64. &jge( &label("end"));
  65. &mov( $tmp1, &DWP(0,$data,$i,4));
  66. &push( $tmp1);
  67. &call( "other");
  68. &add( $j, "eax");
  69. &pop( $tmp1);
  70. &inc( $i);
  71. &jmp( &label("loop"));
  72. &set_label("end");
  73. &mov( "eax", $j);
  74. &function_end("calc");
  75. &asm_finish();
  76. The above example is very very unoptimised but gives an idea of how
  77. things work.
  78. There is also a cbc mode function generator in cbc.pl
  79. &cbc( $name,
  80. $encrypt_function_name,
  81. $decrypt_function_name,
  82. $true_if_byte_swap_needed,
  83. $parameter_number_for_iv,
  84. $parameter_number_for_encrypt_flag,
  85. $first_parameter_to_pass,
  86. $second_parameter_to_pass,
  87. $third_parameter_to_pass);
  88. So for example, given
  89. void BF_encrypt(BF_LONG *data,BF_KEY *key);
  90. void BF_decrypt(BF_LONG *data,BF_KEY *key);
  91. void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
  92. BF_KEY *ks, unsigned char *iv, int enc);
  93. &cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1);
  94. &cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
  95. &cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);