x86ms.pl 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/local/bin/perl

package x86ms;

$label="L000";

%lb=(	'eax',	'al',
	'ebx',	'bl',
	'ecx',	'cl',
	'edx',	'dl',
	'ax',	'al',
	'bx',	'bl',
	'cx',	'cl',
	'dx',	'dl',
	);

%hb=(	'eax',	'ah',
	'ebx',	'bh',
	'ecx',	'ch',
	'edx',	'dh',
	'ax',	'ah',
	'bx',	'bh',
	'cx',	'ch',
	'dx',	'dh',
	);

27 28 29 30 31
sub main'asm_init_output { @out=(); }
sub main'asm_get_output { return(@out); }
sub main'get_labels { return(@labels); }
sub main'external_label { push(@labels,@_); }

32 33 34 35 36 37 38 39 40 41 42 43
sub main'LB
	{
	(defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
	return($lb{$_[0]});
	}

sub main'HB
	{
	(defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
	return($hb{$_[0]});
	}

44 45 46 47 48
sub main'BP
	{
	&get_mem("BYTE",@_);
	}

49 50
sub main'DWP
	{
51 52 53
	&get_mem("DWORD",@_);
	}

54 55 56 57 58 59 60 61 62 63
sub main'BC
	{
	return @_;
	}

sub main'DWC
	{
	return @_;
	}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
sub main'stack_push
	{
	local($num)=@_;
	$stack+=$num*4;
	&main'sub("esp",$num*4);
	}

sub main'stack_pop
	{
	local($num)=@_;
	$stack-=$num*4;
	&main'add("esp",$num*4);
	}

sub get_mem
	{
	local($size,$addr,$reg1,$reg2,$idx)=@_;
	local($t,$post);
	local($ret)="$size PTR ";
83 84 85 86 87 88 89 90 91 92 93 94 95 96

	$addr =~ s/^\s+//;
	if ($addr =~ /^(.+)\+(.+)$/)
		{
		$reg2=&conv($1);
		$addr="_$2";
		}
	elsif ($addr =~ /^[_a-zA-Z]/)
		{
		$addr="_$addr";
		}

	$reg1="$regs{$reg1}" if defined($regs{$reg1});
	$reg2="$regs{$reg2}" if defined($regs{$reg2});
97 98 99 100 101 102
	if (($addr ne "") && ($addr ne 0))
		{
		if ($addr !~ /^-/)
			{ $ret.=$addr; }
		else	{ $post=$addr; }
		}
103 104 105 106
	if ($reg2 ne "")
		{
		$t="";
		$t="*$idx" if ($idx != 0);
107 108
		$reg1="+".$reg1 if ("$reg1$post" ne "");
		$ret.="[$reg2$t$reg1$post]";
109 110 111
		}
	else
		{
112
		$ret.="[$reg1$post]"
113 114 115 116 117 118 119 120 121 122 123
		}
	return($ret);
	}

sub main'mov	{ &out2("mov",@_); }
sub main'movb	{ &out2("mov",@_); }
sub main'and	{ &out2("and",@_); }
sub main'or	{ &out2("or",@_); }
sub main'shl	{ &out2("shl",@_); }
sub main'shr	{ &out2("shr",@_); }
sub main'xor	{ &out2("xor",@_); }
124
sub main'xorb	{ &out2("xor",@_); }
125
sub main'add	{ &out2("add",@_); }
126
sub main'adc	{ &out2("adc",@_); }
127 128 129 130 131
sub main'sub	{ &out2("sub",@_); }
sub main'rotl	{ &out2("rol",@_); }
sub main'rotr	{ &out2("ror",@_); }
sub main'exch	{ &out2("xchg",@_); }
sub main'cmp	{ &out2("cmp",@_); }
132 133 134
sub main'lea	{ &out2("lea",@_); }
sub main'mul	{ &out1("mul",@_); }
sub main'div	{ &out1("div",@_); }
135
sub main'dec	{ &out1("dec",@_); }
136
sub main'inc	{ &out1("inc",@_); }
137
sub main'jmp	{ &out1("jmp",@_); }
138
sub main'jmp_ptr { &out1p("jmp",@_); }
139
sub main'je	{ &out1("je",@_); }
140
sub main'jle	{ &out1("jle",@_); }
141
sub main'jz	{ &out1("jz",@_); }
142 143 144 145 146
sub main'jge	{ &out1("jge",@_); }
sub main'jl	{ &out1("jl",@_); }
sub main'jb	{ &out1("jb",@_); }
sub main'jc	{ &out1("jc",@_); }
sub main'jnc	{ &out1("jnc",@_); }
147
sub main'jnz	{ &out1("jnz",@_); }
148 149 150 151 152 153
sub main'jne	{ &out1("jne",@_); }
sub main'jno	{ &out1("jno",@_); }
sub main'push	{ &out1("push",@_); $stack+=4; }
sub main'pop	{ &out1("pop",@_); $stack-=4; }
sub main'bswap	{ &out1("bswap",@_); &using486(); }
sub main'not	{ &out1("not",@_); }
154
sub main'call	{ &out1("call",'_'.$_[0]); }
155 156
sub main'ret	{ &out0("ret"); }
sub main'nop	{ &out0("nop"); }
157 158 159 160 161 162

sub out2
	{
	local($name,$p1,$p2)=@_;
	local($l,$t);

163
	push(@out,"\t$name\t");
164 165
	$t=&conv($p1).",";
	$l=length($t);
166
	push(@out,$t);
167
	$l=4-($l+9)/8;
168 169 170 171 172 173 174 175 176 177
	push(@out,"\t" x $l);
	push(@out,&conv($p2));
	push(@out,"\n");
	}

sub out0
	{
	local($name)=@_;

	push(@out,"\t$name\n");
178 179 180 181 182 183 184
	}

sub out1
	{
	local($name,$p1)=@_;
	local($l,$t);

185
	push(@out,"\t$name\t".&conv($p1)."\n");
186 187 188 189 190 191 192 193 194 195
	}

sub conv
	{
	local($p)=@_;

	$p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
	return $p;
	}

196 197 198 199 200 201 202
sub using486
	{
	return if $using486;
	$using486++;
	grep(s/\.386/\.486/,@out);
	}

203 204 205 206
sub main'file
	{
	local($file)=@_;

207
	local($tmp)=<<"EOF";
208 209 210 211
	TITLE	$file.asm
        .386
.model FLAT
EOF
212
	push(@out,$tmp);
213 214 215 216
	}

sub main'function_begin
	{
217
	local($func,$extra)=@_;
218

219
	push(@labels,$func);
220

221
	local($tmp)=<<"EOF";
222 223 224 225 226 227 228 229 230
_TEXT	SEGMENT
PUBLIC	_$func
$extra
_$func PROC NEAR
	push	ebp
	push	ebx
	push	esi
	push	edi
EOF
231
	push(@out,$tmp);
232 233 234
	$stack=20;
	}

235 236 237 238 239 240 241 242 243 244 245 246 247 248
sub main'function_begin_B
	{
	local($func,$extra)=@_;

	local($tmp)=<<"EOF";
_TEXT	SEGMENT
PUBLIC	_$func
$extra
_$func PROC NEAR
EOF
	push(@out,$tmp);
	$stack=4;
	}

249 250 251 252
sub main'function_end
	{
	local($func)=@_;

253
	local($tmp)=<<"EOF";
254 255 256 257 258 259 260 261
	pop	edi
	pop	esi
	pop	ebx
	pop	ebp
	ret
_$func ENDP
_TEXT	ENDS
EOF
262
	push(@out,$tmp);
263 264 265 266
	$stack=0;
	%label=();
	}

267
sub main'function_end_B
268 269 270
	{
	local($func)=@_;

271 272 273
	local($tmp)=<<"EOF";
_$func ENDP
_TEXT	ENDS
274
EOF
275 276 277
	push(@out,$tmp);
	$stack=0;
	%label=();
278 279
	}

280
sub main'function_end_A
281 282 283
	{
	local($func)=@_;

284 285 286 287 288 289
	local($tmp)=<<"EOF";
	pop	edi
	pop	esi
	pop	ebx
	pop	ebp
	ret
290
EOF
291
	push(@out,$tmp);
292 293 294 295
	}

sub main'file_end
	{
296
	push(@out,"END\n");
297 298 299 300 301 302 303 304 305
	}

sub main'wparam
	{
	local($num)=@_;

	return(&main'DWP($stack+$num*4,"esp","",0));
	}

306
sub main'swtmp
307
	{
308
	return(&main'DWP($_[0]*4,"esp","",0));
309 310
	}

311 312 313 314 315 316 317 318
# Should use swtmp, which is above esp.  Linix can trash the stack above esp
#sub main'wtmp
#	{
#	local($num)=@_;
#
#	return(&main'DWP(-(($num+1)*4),"esp","",0));
#	}

319 320 321 322
sub main'comment
	{
	foreach (@_)
		{
323
		push(@out,"\t; $_\n");
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		}
	}

sub main'label
	{
	if (!defined($label{$_[0]}))
		{
		$label{$_[0]}="\$${label}${_[0]}";
		$label++;
		}
	return($label{$_[0]});
	}

sub main'set_label
	{
	if (!defined($label{$_[0]}))
		{
		$label{$_[0]}="${label}${_[0]}";
		$label++;
		}
344 345 346 347 348 349 350 351
	if((defined $_[1]) && ($_[1] == 1))
		{
		push(@out,"$label{$_[0]}::\n");
		}
	else
		{
		push(@out,"$label{$_[0]}:\n");
		}
352 353
	}

354 355 356 357 358 359 360 361 362 363 364 365
sub main'data_word
	{
	push(@out,"\tDD\t$_[0]\n");
	}

sub out1p
	{
	local($name,$p1)=@_;
	local($l,$t);

	push(@out,"\t$name\t ".&conv($p1)."\n");
	}