x86nasm.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #!/usr/local/bin/perl
  2. package x86nasm;
  3. $label="L000";
  4. %lb=( 'eax', 'al',
  5. 'ebx', 'bl',
  6. 'ecx', 'cl',
  7. 'edx', 'dl',
  8. 'ax', 'al',
  9. 'bx', 'bl',
  10. 'cx', 'cl',
  11. 'dx', 'dl',
  12. );
  13. %hb=( 'eax', 'ah',
  14. 'ebx', 'bh',
  15. 'ecx', 'ch',
  16. 'edx', 'dh',
  17. 'ax', 'ah',
  18. 'bx', 'bh',
  19. 'cx', 'ch',
  20. 'dx', 'dh',
  21. );
  22. sub main'asm_init_output { @out=(); }
  23. sub main'asm_get_output { return(@out); }
  24. sub main'get_labels { return(@labels); }
  25. sub main'external_label
  26. {
  27. push(@labels,@_);
  28. foreach (@_) {
  29. push(@out, "extern\t_$_\n");
  30. }
  31. }
  32. sub main'LB
  33. {
  34. (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
  35. return($lb{$_[0]});
  36. }
  37. sub main'HB
  38. {
  39. (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
  40. return($hb{$_[0]});
  41. }
  42. sub main'BP
  43. {
  44. &get_mem("BYTE",@_);
  45. }
  46. sub main'DWP
  47. {
  48. &get_mem("DWORD",@_);
  49. }
  50. sub main'BC
  51. {
  52. return "BYTE @_";
  53. }
  54. sub main'DWC
  55. {
  56. return "DWORD @_";
  57. }
  58. sub main'stack_push
  59. {
  60. my($num)=@_;
  61. $stack+=$num*4;
  62. &main'sub("esp",$num*4);
  63. }
  64. sub main'stack_pop
  65. {
  66. my($num)=@_;
  67. $stack-=$num*4;
  68. &main'add("esp",$num*4);
  69. }
  70. sub get_mem
  71. {
  72. my($size,$addr,$reg1,$reg2,$idx)=@_;
  73. my($t,$post);
  74. my($ret)="[";
  75. $addr =~ s/^\s+//;
  76. if ($addr =~ /^(.+)\+(.+)$/)
  77. {
  78. $reg2=&conv($1);
  79. $addr="_$2";
  80. }
  81. elsif ($addr =~ /^[_a-zA-Z]/)
  82. {
  83. $addr="_$addr";
  84. }
  85. $reg1="$regs{$reg1}" if defined($regs{$reg1});
  86. $reg2="$regs{$reg2}" if defined($regs{$reg2});
  87. if (($addr ne "") && ($addr ne 0))
  88. {
  89. if ($addr !~ /^-/)
  90. { $ret.="${addr}+"; }
  91. else { $post=$addr; }
  92. }
  93. if ($reg2 ne "")
  94. {
  95. $t="";
  96. $t="*$idx" if ($idx != 0);
  97. $reg1="+".$reg1 if ("$reg1$post" ne "");
  98. $ret.="$reg2$t$reg1$post]";
  99. }
  100. else
  101. {
  102. $ret.="$reg1$post]"
  103. }
  104. return($ret);
  105. }
  106. sub main'mov { &out2("mov",@_); }
  107. sub main'movb { &out2("mov",@_); }
  108. sub main'and { &out2("and",@_); }
  109. sub main'or { &out2("or",@_); }
  110. sub main'shl { &out2("shl",@_); }
  111. sub main'shr { &out2("shr",@_); }
  112. sub main'xor { &out2("xor",@_); }
  113. sub main'xorb { &out2("xor",@_); }
  114. sub main'add { &out2("add",@_); }
  115. sub main'adc { &out2("adc",@_); }
  116. sub main'sub { &out2("sub",@_); }
  117. sub main'rotl { &out2("rol",@_); }
  118. sub main'rotr { &out2("ror",@_); }
  119. sub main'exch { &out2("xchg",@_); }
  120. sub main'cmp { &out2("cmp",@_); }
  121. sub main'lea { &out2("lea",@_); }
  122. sub main'mul { &out1("mul",@_); }
  123. sub main'div { &out1("div",@_); }
  124. sub main'dec { &out1("dec",@_); }
  125. sub main'inc { &out1("inc",@_); }
  126. sub main'jmp { &out1("jmp",@_); }
  127. sub main'jmp_ptr { &out1p("jmp",@_); }
  128. # This is a bit of a kludge: declare all branches as NEAR.
  129. sub main'je { &out1("je NEAR",@_); }
  130. sub main'jle { &out1("jle NEAR",@_); }
  131. sub main'jz { &out1("jz NEAR",@_); }
  132. sub main'jge { &out1("jge NEAR",@_); }
  133. sub main'jl { &out1("jl NEAR",@_); }
  134. sub main'ja { &out1("ja NEAR",@_); }
  135. sub main'jae { &out1("jae NEAR",@_); }
  136. sub main'jb { &out1("jb NEAR",@_); }
  137. sub main'jbe { &out1("jbe NEAR",@_); }
  138. sub main'jc { &out1("jc NEAR",@_); }
  139. sub main'jnc { &out1("jnc NEAR",@_); }
  140. sub main'jnz { &out1("jnz NEAR",@_); }
  141. sub main'jne { &out1("jne NEAR",@_); }
  142. sub main'jno { &out1("jno NEAR",@_); }
  143. sub main'push { &out1("push",@_); $stack+=4; }
  144. sub main'pop { &out1("pop",@_); $stack-=4; }
  145. sub main'bswap { &out1("bswap",@_); &using486(); }
  146. sub main'not { &out1("not",@_); }
  147. sub main'call { &out1("call",'_'.$_[0]); }
  148. sub main'ret { &out0("ret"); }
  149. sub main'nop { &out0("nop"); }
  150. sub out2
  151. {
  152. my($name,$p1,$p2)=@_;
  153. my($l,$t);
  154. push(@out,"\t$name\t");
  155. $t=&conv($p1).",";
  156. $l=length($t);
  157. push(@out,$t);
  158. $l=4-($l+9)/8;
  159. push(@out,"\t" x $l);
  160. push(@out,&conv($p2));
  161. push(@out,"\n");
  162. }
  163. sub out0
  164. {
  165. my($name)=@_;
  166. push(@out,"\t$name\n");
  167. }
  168. sub out1
  169. {
  170. my($name,$p1)=@_;
  171. my($l,$t);
  172. push(@out,"\t$name\t".&conv($p1)."\n");
  173. }
  174. sub conv
  175. {
  176. my($p)=@_;
  177. $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
  178. return $p;
  179. }
  180. sub using486
  181. {
  182. return if $using486;
  183. $using486++;
  184. grep(s/\.386/\.486/,@out);
  185. }
  186. sub main'file
  187. {
  188. push(@out, "segment .text use32\n");
  189. }
  190. sub main'function_begin
  191. {
  192. my($func,$extra)=@_;
  193. push(@labels,$func);
  194. my($tmp)=<<"EOF";
  195. global _$func
  196. _$func:
  197. push ebp
  198. push ebx
  199. push esi
  200. push edi
  201. EOF
  202. push(@out,$tmp);
  203. $stack=20;
  204. }
  205. sub main'function_begin_B
  206. {
  207. my($func,$extra)=@_;
  208. my($tmp)=<<"EOF";
  209. global _$func
  210. _$func:
  211. EOF
  212. push(@out,$tmp);
  213. $stack=4;
  214. }
  215. sub main'function_end
  216. {
  217. my($func)=@_;
  218. my($tmp)=<<"EOF";
  219. pop edi
  220. pop esi
  221. pop ebx
  222. pop ebp
  223. ret
  224. EOF
  225. push(@out,$tmp);
  226. $stack=0;
  227. %label=();
  228. }
  229. sub main'function_end_B
  230. {
  231. $stack=0;
  232. %label=();
  233. }
  234. sub main'function_end_A
  235. {
  236. my($func)=@_;
  237. my($tmp)=<<"EOF";
  238. pop edi
  239. pop esi
  240. pop ebx
  241. pop ebp
  242. ret
  243. EOF
  244. push(@out,$tmp);
  245. }
  246. sub main'file_end
  247. {
  248. }
  249. sub main'wparam
  250. {
  251. my($num)=@_;
  252. return(&main'DWP($stack+$num*4,"esp","",0));
  253. }
  254. sub main'swtmp
  255. {
  256. return(&main'DWP($_[0]*4,"esp","",0));
  257. }
  258. # Should use swtmp, which is above esp. Linix can trash the stack above esp
  259. #sub main'wtmp
  260. # {
  261. # my($num)=@_;
  262. #
  263. # return(&main'DWP(-(($num+1)*4),"esp","",0));
  264. # }
  265. sub main'comment
  266. {
  267. foreach (@_)
  268. {
  269. push(@out,"\t; $_\n");
  270. }
  271. }
  272. sub main'label
  273. {
  274. if (!defined($label{$_[0]}))
  275. {
  276. $label{$_[0]}="\$${label}${_[0]}";
  277. $label++;
  278. }
  279. return($label{$_[0]});
  280. }
  281. sub main'set_label
  282. {
  283. if (!defined($label{$_[0]}))
  284. {
  285. $label{$_[0]}="${label}${_[0]}";
  286. $label++;
  287. }
  288. push(@out,"$label{$_[0]}:\n");
  289. }
  290. sub main'data_word
  291. {
  292. push(@out,"\tDD\t$_[0]\n");
  293. }
  294. sub out1p
  295. {
  296. my($name,$p1)=@_;
  297. my($l,$t);
  298. push(@out,"\t$name\t ".&conv($p1)."\n");
  299. }