lib/ronin/asm/program.rb in ronin-asm-0.1.0 vs lib/ronin/asm/program.rb in ronin-asm-0.2.0

- old
+ new

@@ -1,9 +1,9 @@ # # Ronin ASM - A Ruby DSL for crafting Assembly programs and Shellcode. # -# Copyright (c) 2007-2012 Hal Brodigan (postmodern.mod3 at gmail.com) +# Copyright (c) 2007-2013 Hal Brodigan (postmodern.mod3 at gmail.com) # # This file is part of Ronin ASM. # # Ronin is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -36,18 +36,18 @@ # class Program # Supported Assembly Syntaxs SYNTAX = { - :att => Syntax::ATT, - :intel => Syntax::Intel + att: Syntax::ATT, + intel: Syntax::Intel } # The Assembly Parsers PARSERS = { - :att => :gas, - :intel => :nasm + att: :gas, + intel: :nasm } # The targeted architecture attr_reader :arch @@ -92,11 +92,11 @@ # # @yield [] # The given block will be evaluated within the program. # # @example - # Program.new(:arch => :amd64) do + # Program.new(arch: :amd64) do # push rax # push rbx # # mov rsp, rax # mov rax[8], rbx @@ -191,57 +191,77 @@ end # # Creates an operand of size 1 (byte). # - # @param [Integer] number + # @param [MemoryOperand, Integer] op # The value of the operand. # - # @return [ImmediateOperand] + # @return [MemoryOperand, ImmediateOperand] # The new operand value. # - def byte(number) - ImmediateOperand.new(number,1) + def byte(op) + case op + when MemoryOperand + MemoryOperand.new(op.base,op.offset,op.index,op.scale,1) + else + ImmediateOperand.new(op,1) + end end # # Creates a operand of size 2 (bytes). # - # @param [Integer] number + # @param [MemoryOperand, Integer] op # The value of the operand. # - # @return [ImmediateOperand] + # @return [MemoryOperand, ImmediateOperand] # The new operand value. # - def word(number) - ImmediateOperand.new(number,2) + def word(op) + case op + when MemoryOperand + MemoryOperand.new(op.base,op.offset,op.index,op.scale,2) + else + ImmediateOperand.new(op,2) + end end # # Creates a operand of size 4 (bytes). # - # @param [Integer] number + # @param [MemoryOperand, Integer] op # The value of the operand. # # @return [ImmediateOperand] # The new operand value. # - def dword(number) - ImmediateOperand.new(number,4) + def dword(op) + case op + when MemoryOperand + MemoryOperand.new(op.base,op.offset,op.index,op.scale,4) + else + ImmediateOperand.new(op,4) + end end # # Creates a operand of size 8 (bytes). # - # @param [Integer] number + # @param [MemoryOperand, Integer] op # The value of the operand. # - # @return [ImmediateOperand] + # @return [MemoryOperand, ImmediateOperand] # The new operand. # - def qword(number) - ImmediateOperand.new(number,8) + def qword(op) + case op + when MemoryOperand + MemoryOperand.new(op.base,op.offset,op.index,op.scale,8) + else + ImmediateOperand.new(op,8) + end end # # Adds a label to the program. # @@ -316,19 +336,19 @@ end # # Generic method for setting a register. # - # @param [Register, Immediate, Integer] value - # The new value for the register. - # # @param [Symbol] name # The name of the reigster. # + # @param [Register, Immediate, Integer] value + # The new value for the register. + # # @abstract # - def register_set(value,name) + def register_set(name,value) end # # Generic method for saving a register. # @@ -384,24 +404,31 @@ # Converts the program to Assembly Source Code. # # @param [Symbol] syntax # The syntax to compile the program to. # - def to_asm(syntax=:att) + def to_asm(syntax=:intel) SYNTAX[syntax].emit_program(self) end # + # @see #to_s + # + def to_s + to_asm + end + + # # Assembles the program. # # @param [String] output # The path for the assembled program. # # @param [Hash] options # Additional options. # - # @option options [Symbol, String] :syntax (:att) + # @option options [Symbol, String] :syntax (:intel) # The syntax to compile the program to. # # @option options [Symbol] :format (:bin) # The format of the assembled executable. May be one of: # @@ -422,23 +449,23 @@ # # @return [String] # The path to the assembled program. # def assemble(output,options={}) - syntax = options.fetch(:syntax,:att) + syntax = options.fetch(:syntax,:intel) format = options.fetch(:format,:bin) parser = PARSERS[syntax] source = Tempfile.new(['ronin-asm', '.s']) source.write(to_asm(syntax)) source.close YASM::Program.assemble( - :file => source.path, - :parser => PARSERS[syntax], - :target => @arch, - :output_format => format, - :output => output + file: source.path, + parser: PARSERS[syntax], + target: @arch, + output_format: format, + output: output ) return output end