dhilst

Perl assembly generator

I wrote a little perl script that receives input and creates an x86 assembly program. This program put all data on stack and then print it using printf c function. Useless but cool
#!/usr/bin/perl

use warnings;
use strict;

undef $/;
my $input = <>

$input = unpack("H*", $input);
my @integers;

while (length($input) > 8) {
my $str = substr($input, 0, 8);
$str =~ s/(.{2})(.{2})(.{2})(.{2})/$4$3$2$1/;
push(@integers, ("0x".$str));
$input = substr($input, 8);
}

my $buf = "";
while (length($input) > 0) {
$buf .= substr($input, -2);
$input = substr($input, 0, -2);
}

push(@integers, sprintf("0x%08x", hex($buf)));
my $stck_deep = ($#integers + 1) * 4;

print <<EOS
.section .text.startup,"ax",\@progbits
.globl main
.type main, \@function
main:
pushl %ebp
movl %esp, %ebp

subl \$$stck_deep, %esp

movl \$$integers[0], (%esp)
EOS
;

for my $i (1 .. $#integers) {
printf("\tmovl \$%s, %d(%%esp)\n", $integers[$i], $i * 4);
}

print <<EOS
movl \$0x0, $stck_deep(%esp)

pushl %esp
call printf
popl %eax

addl \$$stck_deep, %esp


xorl %eax, %eax
leave
ret
EOS

;

Here is a try:
https://ideone.com/cuvXe
https://ideone.com/2oA9q
[]'s