Created
June 19, 2011 08:31
-
-
Save miau/1033982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class String | |
# offset 文字目から val の長さぶん val で置き換える | |
def write(offset, val) | |
self[offset, val.length] = val | |
end | |
end | |
def compile(lines) | |
lines << "exit" unless lines.last == "exit" | |
ret = "" | |
lines.each_with_index do |line, i| | |
line.strip! | |
case line | |
when /^let eax (.+)$/ | |
ret << [0xb8, $1.to_i].pack("CI") | |
when /^exit$/ | |
ret << [0xc3].pack("C") | |
else | |
puts "can't compile(line #{i+1}): #{line}" | |
exit 1 | |
end | |
end | |
ret | |
end | |
source = [ | |
"let eax 123", | |
] | |
base = File.open("base.exe", "rb").read | |
text = compile(source) | |
base.write(0x180, [text.length].pack("I")) | |
base.write(0x200, text) | |
File.open("output.exe", "wb").write(base) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment