wilson 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,591 @@
1
+
2
+ require 'test/unit'
3
+ require 'wilson'
4
+
5
+ class TestWilson < Test::Unit::TestCase
6
+ # MachineCodeX86Test initialize-release
7
+
8
+ def setup
9
+ @asm = Wilson::MachineCodeX86.new
10
+ @stream = asm.stream
11
+ end
12
+
13
+ attr_reader :asm, :stream
14
+
15
+ # MachineCodeX86Test testing ow/od
16
+
17
+ def test_mov_offset_eax
18
+ 256.m.mov asm.eax
19
+ assert_equal [0xA3, 0, 1, 0, 0], stream
20
+ end
21
+
22
+ def test_mov_eax_offset
23
+ asm.eax.mov 256.m
24
+ assert_equal [0xA1, 0, 1, 0, 0], stream
25
+ end
26
+
27
+ # MachineCodeX86Test testing reg<32, mem
28
+
29
+ def test_mov_al_big_offset
30
+ asm.al.mov 256.m
31
+ assert_equal [0xA0, 0, 1, 0, 0], stream
32
+ end
33
+
34
+ def test_mov_cl_m_edx
35
+ asm.cl.mov asm.edx.m
36
+ assert_equal [0x8A, 0b00001010], stream
37
+ end
38
+
39
+ def test_mov_ax_m_ecx
40
+ asm.ax.mov asm.ecx.m
41
+ assert_equal [0x66, 0x8B, 1], stream
42
+ end
43
+
44
+ def test_mov_cl_offset
45
+ asm.cl.mov 1.m
46
+ assert_equal [0x8A, 0x0D, 1, 0, 0, 0], stream
47
+ end
48
+
49
+ def test_mov_ax_big_offset
50
+ asm.ax.mov 256.m
51
+ assert_equal [0x66, 0xA1, 0, 1, 0, 0], stream
52
+ end
53
+
54
+ def test_mov_al_offset
55
+ asm.al.mov 1.m
56
+ assert_equal [0xA0, 1, 0, 0, 0], stream
57
+ end
58
+
59
+ def test_mov_cx_offset
60
+ asm.cx.mov 1.m
61
+ assert_equal [0x66, 0x8B, 0x0D, 1, 0, 0, 0], stream
62
+ end
63
+
64
+ def test_mov_cx_m_edx
65
+ asm.cx.mov asm.edx.m
66
+ assert_equal [0x66, 0x8B, 0b00001010], stream
67
+ end
68
+
69
+ def test_mov_ax_offset
70
+ asm.ax.mov 1.m
71
+ assert_equal [0x66, 0xA1, 1, 0, 0, 0], stream
72
+ end
73
+
74
+ def test_mov_al_m_ecx
75
+ asm.al.mov asm.ecx.m
76
+ assert_equal [0x8A, 1], stream
77
+ end
78
+
79
+ # MachineCodeX86Test testing /r reg/reg
80
+
81
+ def test_mov_eax_ebx
82
+ asm.eax.mov asm.ebx
83
+ assert_equal [0x89, 0b11011000], stream
84
+ end
85
+
86
+ def test_mov_ebx_ecx
87
+ asm.ebx.mov asm.ecx
88
+ assert_equal [0x89, 0b11001011], stream
89
+ end
90
+
91
+ # MachineCodeX86Test testing /n reg
92
+
93
+ def test_bt_ecx_literal
94
+ asm.ecx.bt 1
95
+ assert_equal [0x0F, 0xBA, 0b11100001, 1], stream
96
+ end
97
+
98
+ def test_fdivr
99
+ asm.ecx.m.fdivr
100
+ assert_equal [0xD8, 0b00000001], stream
101
+ end
102
+
103
+ # MachineCodeX86Test testing []
104
+
105
+ def test_bracket_argument
106
+ asm.eax.mov { asm.ecx }
107
+ assert_equal [0x8B, 1], stream
108
+ end
109
+
110
+ # def test_bracket_receiver # HACK
111
+ # asm.assemble { [asm.ecx].mov asm.eax }
112
+ # assert_equal [0x89, 1], stream
113
+ # end
114
+
115
+ # MachineCodeX86Test testing ib/iw/id
116
+
117
+ def test_add_eax_literal
118
+ asm.eax.add 256
119
+ assert_equal [5, 0, 1, 0, 0], stream
120
+ end
121
+
122
+ def test_add_ax_literal
123
+ asm.ax.add 256
124
+ assert_equal [0x66, 5, 0, 1], stream
125
+ end
126
+
127
+ def test_add_al_literal
128
+ asm.al.add 1
129
+ assert_equal [4, 1], stream
130
+ end
131
+
132
+ # MachineCodeX86Test testing rb/rw/rd
133
+
134
+ def test_label_jmp_veryFar
135
+ label = asm.label
136
+ 65536.times { asm.stream << 0x90 } # cheaters way to do nop, so it runs faster
137
+ label.jmp
138
+ assert_equal 65536 + 5, stream.size
139
+ assert_equal [0xE9, 0xFB, 0xFF, 0xFE, 0xFF], stream[65536..-1]
140
+ end
141
+
142
+ def test_jmp_big_offset
143
+ asm.jmp 256.m
144
+ assert_equal [0xFF, 0b00100101, 0, 1, 0, 0], stream
145
+ end
146
+
147
+ def test_jmp_m_ecx
148
+ asm.jmp asm.ecx.m
149
+ assert_equal [0xFF, 0b00100001], stream
150
+ end
151
+
152
+ def test_jmp_infinite
153
+ label = asm.label
154
+ label.jmp
155
+ assert_equal [0xEB, 0xFE], stream
156
+ end
157
+
158
+ def test_label_jmp_far
159
+ asm.bits = 16
160
+ label = asm.label
161
+ 256.times { stream << 0x90 } # cheaters way to do nop, so it runs faster
162
+ label.jmp
163
+ assert_equal 256 + 3, stream.size
164
+ assert_equal [0xE9, 0xFD, 0xFE], stream[256..-1]
165
+ end
166
+
167
+ def test_jmp_forward_and_backward
168
+ label = asm.future_label
169
+ label.jmp
170
+ asm.nop
171
+ label.plant
172
+ label.jmp
173
+ assert_equal [0xE9, 1, 0, 0, 0, 0x90, 0xEB, 0xFE], stream
174
+ end
175
+
176
+ def test_jmp_forward
177
+ label = asm.future_label
178
+ label.jmp
179
+ asm.nop
180
+ label.plant
181
+ assert_equal [0xE9, 1, 0, 0, 0, 0x90], stream
182
+ end
183
+
184
+ def test_jmp_offset
185
+ asm.jmp 1.m
186
+ assert_equal [0xFF, 0b00100101, 1, 0, 0, 0], stream
187
+ end
188
+
189
+ def test_jmp_far
190
+ asm.jmp 256
191
+ assert_equal [0xE9, 0xFB, 0, 0, 0], stream
192
+ end
193
+
194
+ def test_loop_infinite
195
+ label = asm.label
196
+ label.loop
197
+ assert_equal [0xE2, 0xFE], stream
198
+ end
199
+
200
+ # MachineCodeX86Test testing o16/o32
201
+
202
+ def test_adc_ax_literal
203
+ asm.bits = 32
204
+ asm.ax.add 1
205
+ assert_equal [0x66, 5, 1, 0], stream
206
+ end
207
+
208
+ def test_adc_ax_literal16
209
+ asm.bits = 32
210
+ asm.ax.add 256
211
+ assert_equal [0x66, 5, 0, 1], stream
212
+ end
213
+
214
+ def test_adc_eax_literal
215
+ asm.bits = 16
216
+ asm.eax.add 1
217
+ assert_equal [0x67, 0x83, 0b11000000, 1], stream
218
+ end
219
+
220
+ def test_adc_eax_literal16
221
+ asm.bits = 16
222
+ asm.eax.add 256
223
+ assert_equal [0x67, 5, 0, 1, 0, 0], stream
224
+ end
225
+
226
+ # MachineCodeX86Test testing cpuregs
227
+
228
+ def test_mov_ecx_cr0
229
+ asm.ecx.mov asm.cr0
230
+ assert_equal [0x0f, 0x20, 0b11000001], stream
231
+ end
232
+
233
+ def test_mov_dr0Ecx
234
+ asm.dr0.mov asm.ecx
235
+ assert_equal [0x0f, 0x23, 0b11000001], stream
236
+ end
237
+
238
+ def test_mov_ecx_tr3
239
+ asm.ecx.mov asm.tr3
240
+ assert_equal [0x0f, 0x24, 0b11011001], stream
241
+ end
242
+
243
+ def test_mov_tr3_ecx
244
+ asm.tr3.mov asm.ecx
245
+ assert_equal [0x0f, 0x26, 0b11011001], stream
246
+ end
247
+
248
+ def test_mov_ecx_dr0
249
+ asm.ecx.mov asm.dr0
250
+ assert_equal [0x0f, 0x21, 0b11000001], stream
251
+ end
252
+
253
+ def test_mov_cr0_ecx
254
+ asm.cr0.mov asm.ecx
255
+ assert_equal [0x0f, 0x22, 0b11000001], stream
256
+ end
257
+
258
+ # MachineCodeX86Test testing /r reg/mem
259
+
260
+ def test_mov_eax_m_ecx
261
+ asm.eax.mov asm.ecx.m
262
+ assert_equal [0x8B, 0b00000001], stream
263
+ end
264
+
265
+ def test_mov_ebx_m_ecx_edx_offset
266
+ asm.ebx.mov(asm.ecx + asm.edx + 1)
267
+ assert_equal [0x8B, 0b00011100, 0b01010001, 1], stream
268
+ end
269
+
270
+ def test_mov_ebx_m_ecx_edx
271
+ asm.ebx.mov(asm.ecx + asm.edx)
272
+ assert_equal [0x8B, 0b00011100, 0b00010001], stream
273
+ end
274
+
275
+ def test_mov_ebx_m_ecx_edx_big_offset
276
+ asm.ebx.mov(asm.ecx + asm.edx + 256)
277
+ assert_equal [0x8B, 0b00011100, 0b10010001, 0, 1, 0, 0], stream
278
+ end
279
+
280
+ def test_mov_eax_m_ecx_big_offset
281
+ asm.eax.mov asm.ecx + 256
282
+ assert_equal [0x8B, 0b10000001, 0, 1, 0, 0], stream
283
+ end
284
+
285
+ def test_mov_ecx_m_offset
286
+ asm.ecx.mov 256.m
287
+ assert_equal [0x8B, 0x0D, 0, 1, 0, 0], stream
288
+ end
289
+
290
+ def test_mov_eax_m_ecx_offset
291
+ asm.eax.mov asm.ecx + 1
292
+ assert_equal [0x8B, 0b01000001, 1], stream
293
+ end
294
+
295
+ # MachineCodeX86Test testing imm:imm
296
+
297
+ def test_jmp_imm_Imm32
298
+ asm.jmp 256, 65537
299
+ assert_equal [0xEA, 1, 0, 1, 0, 0, 1], stream
300
+ end
301
+
302
+ def test_call_imm_Imm16
303
+ asm.call 1, 2
304
+ assert_equal [0x66, 0x9A, 2, 0, 1, 0], stream
305
+ end
306
+
307
+ def test_jmp_imm_Imm16
308
+ asm.jmp 1, 2
309
+ assert_equal [0x66, 0xEA, 2, 0, 1, 0], stream
310
+ end
311
+
312
+ def test_call_imm_Imm32
313
+ asm.call 256, 65537
314
+ assert_equal [0x9A, 1, 0, 1, 0, 0, 1], stream
315
+ end
316
+
317
+ # MachineCodeX86Test testing 0 args
318
+
319
+ def test_ret
320
+ asm.ret
321
+ assert_equal [0xC3], stream
322
+ end
323
+
324
+ def test_nop
325
+ asm.nop
326
+ assert_equal [0x90], stream
327
+ end
328
+
329
+ def test_hlt
330
+ asm.hlt
331
+ assert_equal [0xF4], stream
332
+ end
333
+
334
+ # MachineCodeX86Test testing 3 args
335
+
336
+ def test_imul_ecx_edx_immediate
337
+ asm.ecx.imul asm.edx, 1
338
+ assert_equal [0x6B, 0xCA, 1], stream
339
+ end
340
+
341
+ def test_imul_ecx_edx_big_immediate
342
+ asm.ecx.imul asm.edx, 256
343
+ assert_equal [0x69, 0xCA, 0, 1, 0, 0], stream
344
+ end
345
+
346
+ # MachineCodeX86Test testing fpureg
347
+
348
+ def test_fadd_st0St1
349
+ asm.st0.fadd asm.st1
350
+ assert_equal [0xD8, 0xC1], stream
351
+ end
352
+
353
+ def test_fmulp_st1St0
354
+ asm.st1.fmulp asm.st0
355
+ assert_equal [0xDE, 0xC9], stream
356
+ end
357
+
358
+ def test_fadd_st0
359
+ asm.st0.fadd
360
+ assert_equal [0xD8, 0xC0], stream
361
+ end
362
+
363
+ def test_fadd_st1
364
+ asm.st1.fadd
365
+ assert_equal [0xD8, 0xC1], stream
366
+ end
367
+
368
+ # MachineCodeX86Test testing /n mem
369
+
370
+ def test_fild_m_ecx_edx_offset
371
+ (asm.ecx + asm.edx + 1).fild
372
+ assert_equal [0xDB, 0b00000100, 0b01010001, 1], stream
373
+ end
374
+
375
+ def test_bt_m_ecx_literal
376
+ # ... this shouldn't work. The machine code generated here does
377
+ # not work and nasm complains that a size was not specified if you
378
+ # try to write the same code in nasm -> mov [ecx], 1
379
+ #
380
+ # Some how we're meant to know this is invalid and throw an
381
+ # error.. not sure how yet.
382
+ asm.ecx.m.bt 1
383
+ assert_equal [0x0F, 0xBA, 0b00100001, 1], stream
384
+ end
385
+
386
+ def test_fild_m_ecx_big_offset
387
+ (asm.ecx + 256).fild
388
+ assert_equal [0xDB, 0b10000001, 0, 1, 0, 0], stream
389
+ end
390
+
391
+ def test_fild_m_ecx_edx
392
+ (asm.ecx + asm.edx).fild
393
+ assert_equal [0xDB, 0b00000100, 0b00010001], stream
394
+ end
395
+
396
+ def test_fild_m_ecx_edx_big_offset
397
+ (asm.ecx + asm.edx + 256).fild
398
+ assert_equal [0xDB, 0b00000100, 0b10010001, 0, 1, 0, 0], stream
399
+ end
400
+
401
+ def test_fild_m_ecx
402
+ asm.ecx.m.fild
403
+ assert_equal [0xDB, 0b00000001], stream
404
+ end
405
+
406
+ def test_fild_m_ecx_offset
407
+ (asm.ecx + 1).fild
408
+ assert_equal [0xDB, 0b01000001, 1], stream
409
+ end
410
+
411
+ # MachineCodeX86Test testing processors
412
+
413
+ # def testing_adc_edx_ecx # HACK
414
+ # asm.processors.reject! { |processor| processor == '386' }
415
+ # assert_raise NoMethodError do
416
+ # asm.edx.adc asm.ecx
417
+ # end
418
+ # end
419
+
420
+ # MachineCodeX86Test testing mmxreg
421
+
422
+ def test_psllw_mm1Immediate
423
+ asm.mm1.psllw 1
424
+ assert_equal [0x0F, 0x71, 0xF1, 1], stream
425
+ end
426
+
427
+ def test_movd_eax_mm0
428
+ asm.eax.movd asm.mm0
429
+ assert_equal [0x0F, 0x7E, 0xC0], stream
430
+ end
431
+
432
+ def test_movd_mm0Eax
433
+ asm.mm0.movd asm.eax
434
+ assert_equal [0x0F, 0x6E, 0xC0], stream
435
+ end
436
+
437
+ # MachineCodeX86Test testing +r
438
+
439
+ def test_mov_ecx_literal
440
+ asm.ecx.mov 1
441
+ assert_equal [0xB9, 1, 0, 0, 0], stream
442
+ end
443
+
444
+ def test_mov_edx_literal
445
+ asm.edx.mov 1
446
+ assert_equal [0xBA, 1, 0, 0, 0], stream
447
+ end
448
+
449
+ def test_mov_eax_literal
450
+ asm.eax.mov 1
451
+ assert_equal [0xB8, 1, 0, 0, 0], stream
452
+ end
453
+
454
+ def test_mov_ebx_literal
455
+ asm.ebx.mov 1
456
+ assert_equal [0xBB, 1, 0, 0, 0], stream
457
+ end
458
+
459
+ # MachineCodeX86Test testing /r mem/reg
460
+
461
+ def test_mov_m_ecx_edx_offset_ebx
462
+ (asm.ecx + asm.edx + 1).mov asm.ebx
463
+ assert_equal [0x89, 0b00011100, 0b01010001, 1], stream
464
+ end
465
+
466
+ def test_mov_m_eax_ecx
467
+ asm.eax.m.mov asm.ecx
468
+ assert_equal [0x89, 0b00001000], stream
469
+ end
470
+
471
+ def test_mov_m_ecx_big_offset_ebx
472
+ (asm.ecx + 256).mov asm.ebx
473
+ assert_equal [0x89, 0b10011001, 0, 1, 0, 0], stream
474
+ end
475
+
476
+ def test_mov_m_ecx_edx_big_offset_ebx
477
+ (asm.ecx + asm.edx + 256).mov asm.ebx
478
+ assert_equal [0x89, 0b00011100, 0b10010001, 0, 1, 0, 0], stream
479
+ end
480
+
481
+ def test_mov_m_ecx_offset_ebx
482
+ (asm.ecx + 1).mov asm.ebx
483
+ assert_equal [0x89, 0b01011001, 1], stream
484
+ end
485
+
486
+ def test_mov_m_ecx_eax
487
+ asm.ecx.m.mov asm.eax
488
+ assert_equal [0x89, 0b00000001], stream
489
+ end
490
+
491
+ def test_mov_m_ecx_edx_ebx
492
+ (asm.ecx + asm.edx).mov asm.ebx
493
+ assert_equal [0x89, 0b00011100, 0b00010001], stream
494
+ end
495
+
496
+ # MachineCodeX86Test testing +cc
497
+
498
+ def test_cmovne_eax_ecx
499
+ asm.eax.cmovne asm.ecx
500
+ assert_equal [0x0F, 0x45, 0xC1], stream
501
+ end
502
+
503
+ def test_cmove_eax_ecx
504
+ asm.eax.cmove asm.ecx
505
+ assert_equal [0x0F, 0x44, 0xC1], stream
506
+ end
507
+
508
+ def test_jnb
509
+ asm.label.jnb
510
+ assert_equal [0x73, 0xFE], stream
511
+ end
512
+
513
+ def test_jnc
514
+ asm.label.jnc
515
+ assert_equal [0x73, 0xFE], stream
516
+ end
517
+
518
+ def test_jno
519
+ asm.label.jno
520
+ assert_equal [0x71, 0xFE], stream
521
+ end
522
+
523
+ def test_jae
524
+ asm.label.jae
525
+ assert_equal [0x73, 0xFE], stream
526
+ end
527
+
528
+ def test_jnae
529
+ asm.label.jnae
530
+ assert_equal [0x72, 0xFE], stream
531
+ end
532
+
533
+ def test_cmovz_eax_ecx
534
+ asm.eax.cmovz asm.ecx
535
+ assert_equal [0x0F, 0x44, 0xC1], stream
536
+ end
537
+
538
+ def test_jb
539
+ asm.label.jb
540
+ assert_equal [0x72, 0xFE], stream
541
+ end
542
+
543
+ def test_jc
544
+ asm.label.jc
545
+ assert_equal [0x72, 0xFE], stream
546
+ end
547
+
548
+ def test_cmovnz_eax_ecx
549
+ asm.eax.cmovnz asm.ecx
550
+ assert_equal [0x0F, 0x45, 0xC1], stream
551
+ end
552
+
553
+ def test_jo
554
+ asm.label.jo
555
+ assert_equal [0x70, 0xFE], stream
556
+ end
557
+
558
+ # MachineCodeX86Test testing reg,1/cl
559
+
560
+ def test_rcr_eax1
561
+ asm.eax.rcr 1
562
+ assert_equal [0xD1, 0xD8], stream
563
+ end
564
+
565
+ def test_rcr_eaxCl
566
+ asm.eax.rcr asm.cl
567
+ assert_equal [0xD3, 0xD8], stream
568
+ end
569
+
570
+ # MachineCodeX86Test testing segreg
571
+
572
+ def test_mov_fs_m_eax
573
+ asm.fs.mov asm.eax.m
574
+ assert_equal [0x8E, 0x20], stream
575
+ end
576
+
577
+ def test_mov_eaxFs
578
+ asm.eax.mov asm.fs
579
+ assert_equal [0x8C, 0xE0], stream
580
+ end
581
+
582
+ def test_mov_m_eaxFs
583
+ asm.eax.m.mov asm.fs
584
+ assert_equal [0x8C, 0x20], stream
585
+ end
586
+
587
+ def test_mov_fs_eax
588
+ asm.fs.mov asm.eax
589
+ assert_equal [0x8E, 0xE0], stream
590
+ end
591
+ end