Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. my $debug = 1;
  7. my $bytes = 32;
  8. my $url = 'http://www.edmundgriffiths.com/czero.html?hexdump=';
  9.  
  10. my $codes = {
  11. 'nop' => 0,
  12. 'lda' => 32,
  13. 'sta' => 64,
  14. 'add' => 96,
  15. 'sub' => 128,
  16. 'brz' => 160,
  17. 'jmp' => 192,
  18. 'stp' => 224,
  19. };
  20.  
  21. my $lines = ();
  22. my $variables = ();
  23.  
  24. print "Parsing input...\n";
  25.  
  26. while (<>) {
  27. chomp;
  28. next if /^\s*(#.*)?$/;
  29.  
  30. if (/^\s*(\S+)\s+(\S+)\s*$/) {
  31. my ($code, $data) = (lc $1, lc $2);
  32. print "parsed code=$code, data=$data\n";
  33. push @$lines, {'code' => $code, 'data' => $data};
  34.  
  35. unless (defined $codes->{$code}) {
  36. $variables->{$code} = {'address' => $. - 1, 'value' => $data};
  37. }
  38. }
  39. elsif (/^\s*(\d+)\s*$/) {
  40. my $data = $1;
  41. print "parsed data=$data\n";
  42. }
  43. }
  44.  
  45. print "Generating URL...\n";
  46.  
  47. foreach my $line (@$lines) {
  48. my $value = 0;
  49.  
  50. if (defined $line->{code}) {
  51. if (defined $codes->{$line->{code}}) {
  52. print "processing code=$line->{code}, data=$line->{data}\n"
  53. if $debug;
  54.  
  55. $value = $codes->{$line->{code}};
  56.  
  57. if ($line->{data} =~ /^\d+$/) {
  58. $value += $line->{data};
  59. }
  60. elsif (defined $variables->{$line->{data}}) {
  61. $value += $variables->{$line->{data}}->{address};
  62. }
  63. else {
  64. die "Undefined variable: $line->{data}\n";
  65. }
  66. }
  67. elsif (defined $variables->{$line->{code}}) {
  68. $value = $variables->{$line->{code}}->{value};
  69. }
  70. else {
  71. die "Unknown operation: $line->{code}\n";
  72. }
  73. }
  74. else {
  75. $value = $line->{data};
  76. }
  77.  
  78. print "value=$value\n" if $debug;
  79. $url .= sprintf "%02x", $value;
  80. }
  81.  
  82. $url .= '00' x ($bytes - scalar @$lines);
  83. print $url, "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement