Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5::X64::Assembler - add push/pop
  • Loading branch information
fglock committed Mar 24, 2013
1 parent 61a5f3c commit 0e13f14
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
47 changes: 44 additions & 3 deletions src5/lib/Perlito5/X64/Assembler.pm
Expand Up @@ -9,6 +9,25 @@ sub new {
}


# Return the high bit of the register code as a 0 or 1. Used often when constructing the REX prefix byte.
sub high_bit {
my ($reg) = @_;
return $reg->{code} >> 3;
}

# Return the 3 low bits of the register code. Used when encoding registers in modR/M, SIB, and opcode bytes.
sub low_bits {
my ($reg) = @_;
return $reg->{code} & 0x7;
}

# emit REX if needed
sub emit_optional_rex_32 {
my ($reg) = @_;
Perlito5::X64::Assembler::emit(0x41) if $reg->high_bit();
}


package Perlito5::X64::Assembler;

my @buffer;
Expand Down Expand Up @@ -94,11 +113,33 @@ sub is_uint16 {

#--- instructions

sub nop {
sub _nop {
emit(0x90);
}

sub ret {
sub _pop {
my ( $dst ) = @_;
if ( is_register($dst) ) {
$dst->emit_optional_rex_32();
emit(0x58 | $dst->low_bits());
}
else {
die "push: don't know what to do with $dst";
}
}

sub _push {
my ( $src ) = @_;
if ( is_register($src) ) {
$src->emit_optional_rex_32();
emit(0x50 | $src->low_bits());
}
else {
die "push: don't know what to do with $src";
}
}

sub _ret {
my ( $imm16 ) = @_;
if ( !$imm16 ) {
emit(0xC3);
Expand All @@ -125,7 +166,7 @@ The Perlito5 x64 backend
use Perlito5::X64::Assembler;
package Perlito5::X64::Assembler;
ret();
_ret();
say to_hex(); # C3
=head1 References
Expand Down
8 changes: 6 additions & 2 deletions t5-x64/01_sanity.t
Expand Up @@ -25,13 +25,17 @@ say "1..4";
print "not " if $out;
say "ok # !is_register";

ret();
_ret();
$out = to_hex();
print "not " if $out ne 'C3';
say "ok # $out";
ret(10);
_ret(10);
$out = to_hex();
print "not " if $out ne 'C3 C2 0A 00';
say "ok # $out";

_push( rax );
_push( r14 );
say "# " . to_hex();
}

0 comments on commit 0e13f14

Please sign in to comment.