Class: RMasm::BinIO

Inherits:
Object show all
Defined in:
lib/rmasm/binio.rb

Overview

BinIO handles optimized IO in assembler primitive format : db, dw, dd, dq, float, double Each xx (db,dw,dd,dq,float,double) type has 3 methods :

  • write_xx(val) : write the val in xx format

  • write_xx_n(val) : write the val n times in xx format

  • write_xx_array(array_of_val) : write the array_of_val in xx format

TODO, implements Input?

Constant Summary collapse

MODE =
BinIOMode.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, mode = nil) ⇒ BinIO

Returns a new instance of BinIO.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rmasm/binio.rb', line 111

def initialize(io, mode=nil)
  @io = io
  @value = [0]
  @dq_value = [0, 0]
  @mode = (mode.nil?)? MODE.value : BinIOMode.check_value(mode)?mode: :little
  case @mode
    when :little
      @fmt_dw = 'v' # short little endian
      @fmt_dw_arr= 'v*' # short little endian
      @fmt_dd = 'V' # dword little endian
      @fmt_dd_arr= 'V*' # dword little endian
      @fmt_dq = 'V*' # 2 x dword little endian TODO CHECK 64 BIT
      @fmt_float = 'e' # float little endian
      @fmt_float_arr = 'e*'# float little endian
      @fmt_double= 'E' # double little endian
      @fmt_double_arr = 'E*' # double little endian
    when :big
      @fmt_dw = 'n' # short big endian
      @fmt_dw_arr= 'n*' # short little endian
      @fmt_dd = 'N' # dword big endian
      @fmt_dd_arr= 'N*' # dword little endian
      @fmt_dq = 'N*' # 2 x dword big endian  TODO CHECK 64 BIT
      @fmt_float = 'g' # float big endian
      @fmt_float_arr = 'g*'# float little endian
      @fmt_double= 'G' # double big endian
      @fmt_double_arr = 'G*' # double little endian
  end
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



109
110
111
# File 'lib/rmasm/binio.rb', line 109

def io
  @io
end

Class Method Details

.mode(*args) ⇒ Object



140
141
142
143
144
145
# File 'lib/rmasm/binio.rb', line 140

def self.mode(*args)
  return MODE if args.empty?
  return Report.error(:ARGS, args.length, "1", "BinIO.mode") if args.length != 1
  # report an error if any arguments are passed to the data directive
  MODE.value = args[0]
end

.mode=(arg) ⇒ Object



147
148
149
# File 'lib/rmasm/binio.rb', line 147

def self.mode=(arg)
  MODE.value = arg
end

Instance Method Details

#write_db(val) ⇒ Object


Write db




155
156
157
158
# File 'lib/rmasm/binio.rb', line 155

def write_db(val)
  @value[0] = val
  @io.write(@value.pack('C') )
end

#write_db_array(arr) ⇒ Object



166
167
168
# File 'lib/rmasm/binio.rb', line 166

def write_db_array(arr)
  @io.write(arr.pack('C*'))
end

#write_db_n(val, n) ⇒ Object



160
161
162
163
164
# File 'lib/rmasm/binio.rb', line 160

def write_db_n(val, n)
  @value[0] = val
  pack = @value.pack('C')
  n.times { @io.write(pack) }
end

#write_dd(val) ⇒ Object


Write dd




193
194
195
196
# File 'lib/rmasm/binio.rb', line 193

def write_dd(val)
  @value[0] = val
  @io.write(@value.pack(@fmt_dd) )
end

#write_dd_array(arr) ⇒ Object



204
205
206
# File 'lib/rmasm/binio.rb', line 204

def write_dd_array(arr)
  @io.write(arr.pack(@fmt_dd_arr))
end

#write_dd_n(val, n) ⇒ Object



198
199
200
201
202
# File 'lib/rmasm/binio.rb', line 198

def write_dd_n(val, n)
  @value[0] = val
  pack = @value.pack(@fmt_dd)
  n.times { @io.write(pack) }
end

#write_double(val) ⇒ Object


Write double




265
266
267
268
# File 'lib/rmasm/binio.rb', line 265

def write_double(val)
  @value[0] = val
  @io.write(@value.pack(@fmt_double) )
end

#write_double_array(arr) ⇒ Object



276
277
278
# File 'lib/rmasm/binio.rb', line 276

def write_double_array(arr)
  @io.write(arr.pack(@fmt_double_arr))
end

#write_double_n(val, n) ⇒ Object



270
271
272
273
274
# File 'lib/rmasm/binio.rb', line 270

def write_double_n(val, n)
  @value[0] = val
  pack = @value.pack(@fmt_double)
  n.times { @io.write(pack) }
end

#write_dq(val) ⇒ Object


Write dq




212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rmasm/binio.rb', line 212

def write_dq(val)
  case @mode
    when :little
      @dq_value[0] = val & 0xFFFFFFFF
      @dq_value[1] = val >> 32
    when :big
      @dq_value[0] = val >> 32
      @dq_value[1] = val & 0xFFFFFFFF
  end
  @io.write(@dq_value.pack(@fmt_dq))
end

#write_dq_array(arr) ⇒ Object



236
237
238
239
240
# File 'lib/rmasm/binio.rb', line 236

def write_dq_array(arr)
  arr.each do |val|
    write_dq(val)
  end
end

#write_dq_n(val, n) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rmasm/binio.rb', line 224

def write_dq_n(val, n)
  case @mode
    when :little
      @dq_value[0] = val & 0xFFFFFFFF
      @dq_value[1] = val >> 32
    when :big
      @dq_value[0] = val >> 32
      @dq_value[1] = val & 0xFFFFFFFF
  end
  n.times { @io.write(@dq_value.pack(@fmt_dq)) }
end

#write_dw(val) ⇒ Object


Write dw




174
175
176
177
# File 'lib/rmasm/binio.rb', line 174

def write_dw(val)
  @value[0] = val
  @io.write(@value.pack(@fmt_dw) )
end

#write_dw_array(arr) ⇒ Object



185
186
187
# File 'lib/rmasm/binio.rb', line 185

def write_dw_array(arr)
  @io.write(arr.pack(@fmt_dw_arr))
end

#write_dw_n(val, n) ⇒ Object



179
180
181
182
183
# File 'lib/rmasm/binio.rb', line 179

def write_dw_n(val, n)
  @value[0] = val
  pack = @value.pack(@fmt_dw)
  n.times { @io.write(pack) }
end

#write_float(val) ⇒ Object


Write float




246
247
248
249
# File 'lib/rmasm/binio.rb', line 246

def write_float(val)
  @value[0] = val
  @io.write(@value.pack(@fmt_float) )
end

#write_float_array(arr) ⇒ Object



257
258
259
# File 'lib/rmasm/binio.rb', line 257

def write_float_array(arr)
  @io.write(arr.pack(@fmt_float_arr))
end

#write_float_n(val, n) ⇒ Object



251
252
253
254
255
# File 'lib/rmasm/binio.rb', line 251

def write_float_n(val, n)
  @value[0] = val
  pack = @value.pack(@fmt_float)
  n.times { @io.write(pack) }
end