<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl

# require 'x86asm.pl';
# &amp;asm_init(&lt;flavor&gt;,"des-586.pl"[,$i386only]);
# &amp;function_begin("foo");
# ...
# &amp;function_end("foo");
# &amp;asm_finish

$out=();
$i386=0;

# AUTOLOAD is this context has quite unpleasant side effect, namely
# that typos in function calls effectively go to assembler output,
# but on the pros side we don't have to implement one subroutine per
# each opcode...
sub ::AUTOLOAD
{ my $opcode = $AUTOLOAD;

    die "more than 4 arguments passed to $opcode" if ($#_&gt;3);

    $opcode =~ s/.*:://;
    if    ($opcode =~ /^push/) { $stack+=4; }
    elsif ($opcode =~ /^pop/)  { $stack-=4; }

    &amp;generic($opcode,@_) or die "undefined subroutine \&amp;$AUTOLOAD";
}

sub ::emit
{ my $opcode=shift;

    if ($#_==-1)    { push(@out,"\t$opcode\n");				}
    else            { push(@out,"\t$opcode\t".join(',',@_)."\n");	}
}

sub ::LB
{   $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
  $1."l";
}
sub ::HB
{   $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
  $1."h";
}
sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &amp;sub("esp",$num);	}
sub ::stack_pop	{ my $num=$_[0]*4; $stack-=$num; &amp;add("esp",$num);	}
sub ::blindpop	{ &amp;pop($_[0]); $stack+=4;				}
sub ::wparam	{ &amp;DWP($stack+4*$_[0],"esp");				}
sub ::swtmp	{ &amp;DWP(4*$_[0],"esp");					}

sub ::bswap
{   if ($i386)	# emulate bswap for i386
    {	&amp;comment("bswap @_");
	&amp;xchg(&amp;HB(@_),&amp;LB(@_));
	&amp;ror (@_,16);
	&amp;xchg(&amp;HB(@_),&amp;LB(@_));
    }
    else
    {	&amp;generic("bswap",@_);	}
}
# These are made-up opcodes introduced over the years essentially
# by ignorance, just alias them to real ones...
sub ::movb	{ &amp;mov(@_);	}
sub ::xorb	{ &amp;xor(@_);	}
sub ::rotl	{ &amp;rol(@_);	}
sub ::rotr	{ &amp;ror(@_);	}
sub ::exch	{ &amp;xchg(@_);	}
sub ::halt	{ &amp;hlt;		}
sub ::movz	{ &amp;movzx(@_);	}
sub ::pushf	{ &amp;pushfd;	}
sub ::popf	{ &amp;popfd;	}

# 3 argument instructions
sub ::movq
{ my($p1,$p2,$optimize)=@_;

    if ($optimize &amp;&amp; $p1=~/^mm[0-7]$/ &amp;&amp; $p2=~/^mm[0-7]$/)
    # movq between mmx registers can sink Intel CPUs
    {	&amp;::pshufw($p1,$p2,0xe4);		}
    else
    {	&amp;::generic("movq",@_);			}
}

# SSE&gt;2 instructions
my %regrm = (	"eax"=&gt;0, "ecx"=&gt;1, "edx"=&gt;2, "ebx"=&gt;3,
		"esp"=&gt;4, "ebp"=&gt;5, "esi"=&gt;6, "edi"=&gt;7	);
sub ::pextrd
{ my($dst,$src,$imm)=@_;
    if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
    {	&amp;::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2&lt;&lt;3)|$regrm{$1},$imm);	}
    else
    {	&amp;::generic("pextrd",@_);		}
}

sub ::pinsrd
{ my($dst,$src,$imm)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
    {	&amp;::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1&lt;&lt;3)|$regrm{$2},$imm);	}
    else
    {	&amp;::generic("pinsrd",@_);		}
}

sub ::pshufb
{ my($dst,$src)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
    {	&amp;data_byte(0x66,0x0f,0x38,0x00,0xc0|($1&lt;&lt;3)|$2);	}
    else
    {	&amp;::generic("pshufb",@_);		}
}

sub ::palignr
{ my($dst,$src,$imm)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
    {	&amp;::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1&lt;&lt;3)|$2,$imm);	}
    else
    {	&amp;::generic("palignr",@_);		}
}

sub ::pclmulqdq
{ my($dst,$src,$imm)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
    {	&amp;::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1&lt;&lt;3)|$2,$imm);	}
    else
    {	&amp;::generic("pclmulqdq",@_);		}
}

sub ::rdrand
{ my ($dst)=@_;
    if ($dst =~ /(e[a-dsd][ixp])/)
    {	&amp;::data_byte(0x0f,0xc7,0xf0|$regrm{$dst});	}
    else
    {	&amp;::generic("rdrand",@_);	}
}

sub ::rdseed
{ my ($dst)=@_;
    if ($dst =~ /(e[a-dsd][ixp])/)
    {	&amp;::data_byte(0x0f,0xc7,0xf8|$regrm{$dst});	}
    else
    {	&amp;::generic("rdrand",@_);	}
}

sub rxb {
 local *opcode=shift;
 my ($dst,$src1,$src2,$rxb)=@_;

   $rxb|=0x7&lt;&lt;5;
   $rxb&amp;=~(0x04&lt;&lt;5) if($dst&gt;=8);
   $rxb&amp;=~(0x01&lt;&lt;5) if($src1&gt;=8);
   $rxb&amp;=~(0x02&lt;&lt;5) if($src2&gt;=8);
   push @opcode,$rxb;
}

sub ::vprotd
{ my $args=join(',',@_);
    if ($args =~ /xmm([0-7]),xmm([0-7]),([x0-9a-f]+)/)
    { my @opcode=(0x8f);
	rxb(\@opcode,$1,$2,-1,0x08);
	push @opcode,0x78,0xc2;
	push @opcode,0xc0|($2&amp;7)|(($1&amp;7)&lt;&lt;3);		# ModR/M
	my $c=$3;
	push @opcode,$c=~/^0/?oct($c):$c;
	&amp;::data_byte(@opcode);
    }
    else
    {	&amp;::generic("vprotd",@_);	}
}

# label management
$lbdecor="L";		# local label decoration, set by package
$label="000";

sub ::islabel		# see is argument is a known label
{ my $i;
    foreach $i (values %label) { return $i if ($i eq $_[0]); }
  $label{$_[0]};	# can be undef
}

sub ::label		# instantiate a function-scope label
{   if (!defined($label{$_[0]}))
    {	$label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++;   }
  $label{$_[0]};
}

sub ::LABEL		# instantiate a file-scope label
{   $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
  $label{$_[0]};
}

sub ::static_label	{ &amp;::LABEL($_[0],$lbdecor.$_[0]); }

sub ::set_label_B	{ push(@out,"@_:\n"); }
sub ::set_label
{ my $label=&amp;::label($_[0]);
    &amp;::align($_[1]) if ($_[1]&gt;1);
    &amp;::set_label_B($label);
  $label;
}

sub ::wipe_labels	# wipes function-scope labels
{   foreach $i (keys %label)
    {	delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/);	}
}

# subroutine management
sub ::function_begin
{   &amp;function_begin_B(@_);
    $stack=4;
    &amp;push("ebp");
    &amp;push("ebx");
    &amp;push("esi");
    &amp;push("edi");
}

sub ::function_end
{   &amp;pop("edi");
    &amp;pop("esi");
    &amp;pop("ebx");
    &amp;pop("ebp");
    &amp;ret();
    &amp;function_end_B(@_);
    $stack=0;
    &amp;wipe_labels();
}

sub ::function_end_A
{   &amp;pop("edi");
    &amp;pop("esi");
    &amp;pop("ebx");
    &amp;pop("ebp");
    &amp;ret();
    $stack+=16;	# readjust esp as if we didn't pop anything
}

sub ::asciz
{ my @str=unpack("C*",shift);
    push @str,0;
    while ($#str&gt;15) {
	&amp;data_byte(@str[0..15]);
	foreach (0..15) { shift @str; }
    }
    &amp;data_byte(@str) if (@str);
}

sub ::asm_finish
{   &amp;file_end();
    print @out;
}

sub ::asm_init
{ my ($type,$fn,$cpu)=@_;

    $filename=$fn;
    $i386=$cpu;

    $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0;
    if    (($type eq "elf"))
    {	$elf=1;			require "x86gas.pl";	}
    elsif (($type eq "elf-1"))
    {	$elf=-1;		require "x86gas.pl";	}
    elsif (($type eq "a\.out"))
    {	$aout=1;		require "x86gas.pl";	}
    elsif (($type eq "coff" or $type eq "gaswin"))
    {	$coff=1;		require "x86gas.pl";	}
    elsif (($type eq "win32n"))
    {	$win32=1;		require "x86nasm.pl";	}
    elsif (($type eq "nw-nasm"))
    {	$netware=1;		require "x86nasm.pl";	}
    #elsif (($type eq "nw-mwasm"))
    #{	$netware=1; $mwerks=1;	require "x86nasm.pl";	}
    elsif (($type eq "win32"))
    {	$win32=1;		require "x86masm.pl";	}
    elsif (($type eq "macosx"))
    {	$aout=1; $macosx=1;	require "x86gas.pl";	}
    elsif (($type eq "android"))
    {	$elf=1; $android=1;	require "x86gas.pl";	}
    else
    {	print STDERR &lt;&lt;"EOF";
Pick one target type from
	elf	- Linux, FreeBSD, Solaris x86, etc.
	a.out	- DJGPP, elder OpenBSD, etc.
	coff	- GAS/COFF such as Win32 targets
	win32n	- Windows 95/Windows NT NASM format
	nw-nasm - NetWare NASM format
	macosx	- Mac OS X
EOF
	exit(1);
    }

    $pic=0;
    for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }

    $filename =~ s/\.pl$//;
    &amp;file($filename);
}

sub ::hidden {}

1;
</pre></body></html>