Skip to content

Commit d297980

Browse files
committed
Added Register#type, Immediate#type, Memory#type (closes #61).
1 parent 1861443 commit d297980

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

lib/ronin/asm/immediate.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ module ASM
3131
#
3232
class Immediate < Operand
3333

34+
# The assembly class type.
35+
#
36+
# @return [:imm8, :imm16, :imm32, :imm64]
37+
attr_reader :type
38+
3439
# The immediate operand's value.
3540
#
3641
# @return [Integer]
@@ -57,6 +62,7 @@ def initialize(value,width=nil)
5762
else
5863
(@value.bit_length / 8.0).ceil
5964
end
65+
@type = :"imm#{@width * 8}"
6066
end
6167

6268
#

lib/ronin/asm/memory.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ module ASM
3232
#
3333
class Memory < Operand
3434

35+
# The assembly class type.
36+
#
37+
# @return [nil, :mem8, :mem16, :mem32, :mem64]
38+
attr_reader :type
39+
3540
# The base of the memory operand.
3641
#
3742
# @return [Register, nil]
@@ -102,6 +107,10 @@ def initialize(base=nil,displacement=0,index=nil,scale=1,width=nil)
102107
@width = width || if base
103108
base.width
104109
end
110+
111+
if @width
112+
@type = :"mem#{@width * 8}"
113+
end
105114
end
106115

107116
#

lib/ronin/asm/register.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module ASM
2828
#
2929
class Register < Operand
3030

31+
# The assembly class type.
32+
#
33+
# @return [:reg8, :reg16, :reg32, :reg64]
34+
attr_reader :type
35+
3136
# The register name.
3237
#
3338
# @return [Symbol]
@@ -55,6 +60,8 @@ def initialize(name, width: , general: false)
5560

5661
@width = width
5762
@general = general
63+
64+
@type = :"reg#{width * 8}"
5865
end
5966

6067
#

spec/immediate_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,48 @@
1616
end
1717
end
1818

19+
describe "#type" do
20+
context "when #width is 1" do
21+
let(:width) { 1 }
22+
23+
subject { described_class.new(value,width) }
24+
25+
it "must return :imm8" do
26+
expect(subject.type).to eq(:imm8)
27+
end
28+
end
29+
30+
context "when #width is 2" do
31+
let(:width) { 2 }
32+
33+
subject { described_class.new(value,width) }
34+
35+
it "must return :imm16" do
36+
expect(subject.type).to eq(:imm16)
37+
end
38+
end
39+
40+
context "when #width is 4" do
41+
let(:width) { 4 }
42+
43+
subject { described_class.new(value,width) }
44+
45+
it "must return :imm32" do
46+
expect(subject.type).to eq(:imm32)
47+
end
48+
end
49+
50+
context "when #width is 8" do
51+
let(:width) { 8 }
52+
53+
subject { described_class.new(value,width) }
54+
55+
it "must return :imm64" do
56+
expect(subject.type).to eq(:imm64)
57+
end
58+
end
59+
end
60+
1961
describe "#width" do
2062
context "when #width is not explicitly set by #initialize" do
2163
context "and it is 0" do

spec/memory_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@
6767
end
6868
end
6969

70+
describe "#type" do
71+
context "when the #width is 1" do
72+
let(:width) { 1 }
73+
74+
subject { described_class.new(register,0,nil,1,width) }
75+
76+
it "must return :mem8" do
77+
expect(subject.type).to be(:mem8)
78+
end
79+
end
80+
81+
context "when the #width is 2" do
82+
let(:width) { 2 }
83+
84+
subject { described_class.new(register,0,nil,1,width) }
85+
86+
it "must return :mem16" do
87+
expect(subject.type).to be(:mem16)
88+
end
89+
end
90+
91+
context "when the #width is 4" do
92+
let(:width) { 4 }
93+
94+
subject { described_class.new(register,0,nil,1,width) }
95+
96+
it "must return :mem32" do
97+
expect(subject.type).to be(:mem32)
98+
end
99+
end
100+
101+
context "when the #width is 8" do
102+
let(:width) { 8 }
103+
104+
subject { described_class.new(register,0,nil,1,width) }
105+
106+
it "must return :mem64" do
107+
expect(subject.type).to be(:mem64)
108+
end
109+
end
110+
end
111+
70112
describe "#+" do
71113
let(:operand) { described_class.new(register,4,register,2) }
72114

spec/register_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,48 @@
66

77
subject { register }
88

9+
describe "#type" do
10+
context "when #width is 1" do
11+
let(:width) { 1 }
12+
13+
subject { described_class.new(:eax, width: width) }
14+
15+
it "must return :reg8" do
16+
expect(subject.type).to eq(:reg8)
17+
end
18+
end
19+
20+
context "when #width is 2" do
21+
let(:width) { 2 }
22+
23+
subject { described_class.new(:eax, width: width) }
24+
25+
it "must return :reg16" do
26+
expect(subject.type).to eq(:reg16)
27+
end
28+
end
29+
30+
context "when #width is 4" do
31+
let(:width) { 4 }
32+
33+
subject { described_class.new(:eax, width: width) }
34+
35+
it "must return :reg32" do
36+
expect(subject.type).to eq(:reg32)
37+
end
38+
end
39+
40+
context "when #width is 8" do
41+
let(:width) { 8 }
42+
43+
subject { described_class.new(:eax, width: width) }
44+
45+
it "must return :reg64" do
46+
expect(subject.type).to eq(:reg64)
47+
end
48+
end
49+
end
50+
951
describe "#+" do
1052
context "when given an Ronin::ASM::Memory" do
1153
let(:operand) do

0 commit comments

Comments
 (0)