lib/ronin/asm/memory_operand.rb in ronin-asm-0.1.0 vs lib/ronin/asm/memory_operand.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
@@ -26,11 +26,11 @@
#
# Represents a Memory Operand.
#
# @see http://asm.sourceforge.net/articles/linasm.html#Memory
#
- class MemoryOperand < Struct.new(:base, :offset, :index, :scale)
+ class MemoryOperand < Struct.new(:base, :offset, :index, :scale, :width)
#
# Creates a new Memory Operand.
#
# @param [Register, nil] base
@@ -46,11 +46,11 @@
# The scale to multiple `index` by.
#
# @raise [TypeError]
# `base` or `index` was not a {Register} or `nil`.
#
- def initialize(base=nil,offset=0,index=nil,scale=1)
+ def initialize(base=nil,offset=0,index=nil,scale=1,width=nil)
unless (base.nil? || base.kind_of?(Register))
raise(TypeError,"base must be a Register or nil")
end
unless offset.kind_of?(Integer)
@@ -63,11 +63,15 @@
unless scale.kind_of?(Integer)
raise(TypeError,"scale must be an Integer")
end
- super(base,offset,index,scale)
+ if base
+ width ||= base.width
+ end
+
+ super(base,offset,index,scale,width)
end
#
# Adds to the offset of the Memory Operand.
#
@@ -76,11 +80,17 @@
#
# @return [MemoryOperand]
# The new Memory Operand.
#
def +(offset)
- MemoryOperand.new(self.base,self.offset+offset,self.index,self.scale)
+ MemoryOperand.new(
+ self.base,
+ self.offset + offset,
+ self.index,
+ self.scale,
+ self.width
+ )
end
#
# Subtracts from the offset of the Memory Operand.
#
@@ -89,20 +99,16 @@
#
# @return [Memoryoperand]
# The new Memory Operand.
#
def -(offset)
- MemoryOperand.new(self.base,self.offset-offset,self.index,self.scale)
- end
-
- #
- # The width of the Memory Operand.
- #
- # @return [Integer]
- # The width taken from the base {Register}.
- #
- def width
- base.width
+ MemoryOperand.new(
+ self.base,
+ self.offset - offset,
+ self.index,
+ self.scale,
+ self.width
+ )
end
end
end
end