-
Notifications
You must be signed in to change notification settings - Fork 2
/
Disio.cpp
2601 lines (2551 loc) · 77.5 KB
/
Disio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* disio.cpp *
* This is a fairly big class, and yet it was originally part of the *
* larger disasm class. I finally got around to splitting off some of *
* 3500 lines of code from that class into here. This represents the *
* disasm I/O functions for window I/O and file I/O. Version 2.11 was *
* when this was split. There are still a huge number of confusing *
* functions and code in here which probably needs more work to clean it *
* up, and better split/define the classes. Some of the functions are *
* friends of disasm because it has been difficult to split the classes *
* effectively and both access the complex main disassembly structures *
************************************************************************/
#include <windows.h>
#include <stdio.h> // for sprintf, for float printing
#include "disio.h"
#include "disasm.h"
#include "data.h"
#include "dasm.h"
#include "schedule.h"
#include "mainwind.h"
#include "gname.h"
#include "xref.h"
#include "range.h"
#include "debug.h"
/************************************************************************
* globals *
* - actually some constants used in file i/o as a header *
************************************************************************/
char hdr[] ="; Created by Borg Disassembler\r\n";
char hdr2[]="; written by Cronos\r\n";
/************************************************************************
* constructor function *
* - this just enables window updates and sets the subline to null *
* - the subline (subitem) is basically the line in the disassembly that *
* you see for any given loc. As a loc may refer to many lines (like *
* segheader, comments, xref, actual instruction are all separate *
* line, and subline says which of these it is) *
************************************************************************/
disio::disio()
{ subitem=dsmnull;
}
/************************************************************************
* destructor function *
* - currently null *
* - note that disio has no particular structs the same way most of the *
* other classes in Borg do *
************************************************************************/
disio::~disio()
{
}
/************************************************************************
* argoverdec *
* - disio also acts as a go between for the user and the disasm engine *
* - here we translate the users current line of the disassembly and *
* their request for a decimal override into a call to the disasm *
* engine to add the override to the instruction that is there *
* - note that these kind of calls come from the scheduler and are part *
* of the secondary thread *
************************************************************************/
void disio::argoverdec(void)
{ lptr outhere;
findcurrentaddr(&outhere);
dsm.disargoverdec(outhere);
}
/************************************************************************
* argoversingle *
* - very similar to argoverdec, for a single (float) override *
************************************************************************/
void disio::argoversingle(void)
{ lptr outhere;
findcurrentaddr(&outhere);
dsm.disargoversingle(outhere);
}
/************************************************************************
* argovernegate *
* - very similar to argoverdec, for an argument negation override *
************************************************************************/
void disio::arg_negate(void)
{ lptr outhere;
findcurrentaddr(&outhere);
dsm.disargnegate(outhere);
}
/************************************************************************
* argoverhex *
* - very similar to argoverdec, for a hex override *
************************************************************************/
void disio::argoverhex(void)
{ lptr outhere;
findcurrentaddr(&outhere);
dsm.disargoverhex(outhere);
}
/************************************************************************
* argoveroffsetdseg *
* - very similar to argoverdec, for a dseg override *
************************************************************************/
void disio::argoveroffsetdseg(void)
{ lptr outhere;
findcurrentaddr(&outhere);
dsm.disargoveroffsetdseg(outhere);
}
/************************************************************************
* argoverchar *
* - very similar to argoverdec, for a char override *
************************************************************************/
void disio::argoverchar(void)
{ lptr outhere;
findcurrentaddr(&outhere);
dsm.disargoverchar(outhere);
}
/************************************************************************
* makedword *
* - disio acts as a go between for the user and the disasm engine *
* - here we translate the users current line of the disassembly and *
* their request for a dword into a call to the disasm engine to *
* disassemble a dword at the current point *
* - note that these kind of calls come from the scheduler and are part *
* of the secondary thread *
************************************************************************/
void disio::makedword(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatadword(outhere);
}
/************************************************************************
* makesingle *
* - very similar to makedword, this time to disassemble a single *
* (float) *
************************************************************************/
void disio::makesingle(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatasingle(outhere);
}
/************************************************************************
* makedouble *
* - very similar to makedword, this time to disassemble a double *
* (double float) *
************************************************************************/
void disio::makedouble(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatadouble(outhere);
}
/************************************************************************
* makelongdouble *
* - very similar to makedword, this time to disassemble a long double *
* (long double) *
************************************************************************/
void disio::makelongdouble(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatalongdouble(outhere);
}
/************************************************************************
* makeword *
* - very similar to makedword, this time to disassemble a word *
************************************************************************/
void disio::makeword(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdataword(outhere);
}
/************************************************************************
* makestring *
* - very similar to makedword, this time to disassemble a string *
* (standard C string) *
************************************************************************/
void disio::makestring(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatastring(outhere);
}
/************************************************************************
* pascalstring *
* - very similar to makedword, this time to disassemble a string *
* (standard pascal string) *
************************************************************************/
void disio::pascalstring(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatapstring(outhere);
}
/************************************************************************
* ucstring *
* - very similar to makedword, this time to disassemble a string *
* (unicode C string) *
************************************************************************/
void disio::ucstring(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdataucstring(outhere);
}
/************************************************************************
* upstring *
* - very similar to makedword, this time to disassemble a string *
* (unicode pascal string) *
************************************************************************/
void disio::upstring(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdataupstring(outhere);
}
/************************************************************************
* dosstring *
* - very similar to makedword, this time to disassemble a string *
* (dos style string) *
************************************************************************/
void disio::dosstring(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatadosstring(outhere);
}
/************************************************************************
* generalstring *
* - very similar to makedword, this time to disassemble a string *
* (general string) *
************************************************************************/
void disio::generalstring(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disdatageneralstring(outhere);
}
/************************************************************************
* makecode *
* - very similar to makedword, but this time to disassemble as code *
* from the current location, and continue disassembling *
************************************************************************/
void disio::makecode(void)
{ lptr outhere;
findcurrentaddr(&outhere);
if(outhere==curraddr)
subitem=dsmnull;
dsm.disblock(outhere);
}
/************************************************************************
* vertsetpos *
* - this function takes a position of the vertical scroll bar, from 0 *
* to the VERTSCROLLRANGE and it sets the current address to that *
* point *
************************************************************************/
void disio::vertsetpos(int pos)
{ float sbarpos;
sbarpos=((float)pos)/((float)VERTSCROLLRANGE)*(float)total_data_size;
setcuraddr(dta.getlocpos((unsigned long)sbarpos));
}
/************************************************************************
* setcuraddr *
* - sets the address as a location for the screen output to start at *
* - also sets the scroll bar position for the vertical scroll bar *
* - also ensures that its not set to a mid-instruction address and *
* sets the subline *
* - called from many places in the source code *
************************************************************************/
void disio::setcuraddr(lptr loc)
{ dsmitem titem,*tblock;
float sbarpos;
dsegitem *l_ds;
// check for non-existant address added v2.20
l_ds=dta.findseg(loc);
if(l_ds==NULL)
return;
curraddr=loc;
titem.addr=curraddr;
titem.type=dsmnull;
dsm.findnext(&titem);
tblock=dsm.nextiterator();
if(tblock!=NULL)
subitem=tblock->type;
else
subitem=dsmnull;
tblock=dsm.nextiter_code(tblock);
if(tblock!=NULL)
{ if(loc.between(tblock->addr,tblock->addr+tblock->length-1))
curraddr=tblock->addr;
}
outend=loc+50;
usersel=0;
userselonscreen=true;
updatewindow();
current_data_pos=dta.datagetpos(loc);
sbarpos=((float)current_data_pos)/((float)total_data_size+(float)1.0)*(float)VERTSCROLLRANGE;
SetScrollPos(mainwindow,SB_VERT,(int)sbarpos,true);
}
/************************************************************************
* setpos *
* - when the left mouse button is pressed the line is changed to the *
* line the mouse points at. This routine sets the line, and asks for *
* a screen update if needed (the screen update method here is a *
* primary thread request that simply invalidates the window rather *
* than requesting through the scheduler...... but this is very very *
* old code.... *
************************************************************************/
void disio::setpos(int ypos)
{ if(usersel!=ypos/cyc)
{ usersel=ypos/cyc;
InvalidateRect(mainwindow,NULL,true);
}
}
/************************************************************************
* printlineheader *
* - prints an appropriate line header with the address if needed and *
* sets the cursor position for the next LastPrintBuff call *
************************************************************************/
void disio::printlineheader(lptr loc,bool printaddrs)
{ if(options.mode32)
{ if(printaddrs)
PrintBuff("%04lx:%08lx",loc.segm,loc.offs);
else
PrintBuff("");
LastPrintBuffEpos(BYTEPOS);
}
else
{ if(printaddrs)
PrintBuff("%04lx:%04lx",loc.segm,loc.offs);
else
PrintBuff("");
LastPrintBuffEpos(BYTEPOS-4);
}
}
/************************************************************************
* outinst *
* - this is the routine which results in the output of an address, hex *
* bytes, instruction and arguments *
************************************************************************/
void disio::outinst(dsmitem *inst,bool printaddrs)
{ dsegitem *dblock;
int i;
int prefixptr;
byte pbyte;
printlineheader(inst->addr,printaddrs);
dblock=dta.findseg(inst->addr); // find segment item.
switch(inst->type)
{ case dsmcode:
i=inst->length;
if(printaddrs)
{ while(i)
{ if(dblock!=NULL)
{ if(dblock->typ==uninitdata)
LastPrintBuff("??");
else
LastPrintBuff("%02x",inst->data[inst->length-i]);
}
else
LastPrintBuff("%02x",inst->data[inst->length-i]);
i--;
if(((i+8)<inst->length)&&(inst->length>10))
{ LastPrintBuff("..");
break;
}
}
}
if(options.mode32)
LastPrintBuffEpos(ASMPOS+4);
else
LastPrintBuffEpos(ASMPOS);
if(inst->flags&FLAGS_PREFIX)
{ prefixptr=0;
while(!isprefix(inst->data[prefixptr])&&(prefixptr<15))
prefixptr++;
pbyte=inst->data[prefixptr];
outprefix(pbyte);
}
LastPrintBuff(DSMITEM_NAME(inst));
LastPrintBuff(" ");
if(options.mode32)
LastPrintBuffEpos(ARGPOS+4);
else
LastPrintBuffEpos(ARGPOS);
if(dblock!=NULL)
{ if(dblock->typ==uninitdata)
LastPrintBuff("?");
else
{ outargs(inst,DSMITEM_ARG1(inst));
if(DSMITEM_ARG2(inst)!=ARG_NONE)
{ LastPrintBuff(", ");
outargs(inst,DSMITEM_ARG2(inst));
}
if(DSMITEM_ARG3(inst)!=ARG_NONE)
{ LastPrintBuff(", ");
outargs(inst,DSMITEM_ARG3(inst));
}
}
}
else
{ outargs(inst,DSMITEM_ARG1(inst));
if(DSMITEM_ARG2(inst)!=ARG_NONE)
{ LastPrintBuff(", ");
outargs(inst,DSMITEM_ARG2(inst));
}
if(DSMITEM_ARG3(inst)!=ARG_NONE)
{ LastPrintBuff(", ");
outargs(inst,DSMITEM_ARG3(inst));
}
}
break;
default:
break;
}
}
/************************************************************************
* outdb *
* - this is similar to outinst, but when there is no disassembly for a *
* loc we call this to output a db xxh or a db ? if its uninitdata *
************************************************************************/
void disio::outdb(lptr *lp,bool printaddrs)
{ dsegitem *dblock;
dword aaddr;
byte *mcode;
printlineheader(*lp,printaddrs);
dblock=dta.findseg(*lp); // find segment item.
if(dblock==NULL)
{ if(printaddrs)
LastPrintBuff("??");
}
else if(dblock->typ==uninitdata)
{ if(printaddrs)
LastPrintBuff("??");
}
else
{ aaddr=(*lp-dblock->addr);
mcode=dblock->data+aaddr;
if(printaddrs)
LastPrintBuff("%02x",mcode[0]);
}
if(options.mode32)
LastPrintBuffEpos(ASMPOS+4);
else
LastPrintBuffEpos(ASMPOS);
LastPrintBuff("db");
if(options.mode32)
LastPrintBuffEpos(ARGPOS+4);
else
LastPrintBuffEpos(ARGPOS);
// changed to single ? - 2.25
if(dblock==NULL)
LastPrintBuff("?");
else if(dblock->typ==uninitdata)
{ LastPrintBuff("?");
}
else
{ LastPrintBuffHexValue(mcode[0]);
if(isprint(mcode[0]))
{ LastPrintBuffEpos(COMMENTPOS);
LastPrintBuff(";\'%c\'",mcode[0]);
}
}
}
/************************************************************************
* issegprefix *
* - returns true if byte is a segment prefix valid value *
************************************************************************/
bool disio::issegprefix(byte byt)
{ if((byt==0x2e)||(byt==0x36)||(byt==0x3e)||(byt==0x26)||(byt==0x64)||(byt==0x65))
return true;
return false;
}
/************************************************************************
* isprefix *
* - returns true if byte is a prefix valid value (rep/repne/lock) *
************************************************************************/
bool disio::isprefix(byte byt)
{ if((byt==0xf0)||(byt==0xf2)||(byt==0xf3))
return true;
return false;
}
/************************************************************************
* outprefix *
* - here we output a prefix segment override *
************************************************************************/
void disio::outprefix(byte prefixbyte)
{ switch(prefixbyte)
{ case 0x2e:
LastPrintBuff("cs:");
break;
case 0x36:
LastPrintBuff("ss:");
break;
case 0x3e:
LastPrintBuff("ds:");
break;
case 0x26:
LastPrintBuff("es:");
break;
case 0x64:
LastPrintBuff("fs:");
break;
case 0x65:
LastPrintBuff("gs:");
break;
case 0xf0:
LastPrintBuff("lock ");
break;
case 0xf2:
LastPrintBuff("repne ");
break;
case 0xf3:
LastPrintBuff("rep ");
break;
default:
LastPrintBuff("err:");
break;
}
}
/************************************************************************
* jumpback *
* - when the user presses ESC, or selects jump back we get the last *
* address from the top of the address stack and call setcuraddr and *
* update the window and so the disassembly listing flicks back *
************************************************************************/
void disio::jumpback(void)
{ lptr outhere;
outhere=retstack.pop();
if(outhere.segm)
setcuraddr(outhere);
updatewindow();
}
/************************************************************************
* jumpto *
* - more complex than the jumpback is the jumpto. The complexity lies *
* in deciding where we are jumping to and what the arguments value is *
* and if the location exists. Actually making the jump consists of *
* saving the current location to the address stack and then changing *
* the curraddr for output, and updating the window *
* - most of this routine is a complex decipherment of a modrm address *
* to jump to *
* NB I need to stick this in a function of its own at some point, as it *
* would be quite useful to just get an argument address in several *
* places in the code *
************************************************************************/
void disio::jumpto(bool arg1)
{ dsmitem *tblock;
lptr outhere;
byte *data;
bool madejump;
byte modrm,sib;
word rm;
tblock=findcurrentline();
outhere.assign(0,0);
if(tblock!=NULL)
if(tblock->type!=dsmcode)
return;
madejump=false;
if(tblock!=NULL)
{ if(arg1)
{ switch(DSMITEM_ARG1(tblock))
{ case ARG_FADDR:
data=tblock->data+tblock->length;
if(tblock->mode32)
{ data-=6;
outhere.assign(((word *)(&data[4]))[0],((dword *)(&data[0]))[0]);
}
else
{ data-=4;
outhere.assign(((word *)(&data[2]))[0],((word *)(&data[0]))[0]);
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
break;
case ARG_IMM32:
if(tblock->override!=over_dsoffset)
break;
data=tblock->data+tblock->length;
data-=4;
outhere.assign(options.dseg,((dword *)(&data[0]))[0]);
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
break;
case ARG_MEMLOC:
data=tblock->data+tblock->length;
if(options.mode32)
{ data-=4;
outhere.assign(tblock->addr.segm,((dword *)data)[0]);
}
else
{ data-=2;
outhere.assign(tblock->addr.segm,((word *)data)[0]);
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
break;
case ARG_IMM:
if(tblock->override!=over_dsoffset)
break;
if(options.mode32)
{ data=tblock->data+tblock->length;
data-=4;
outhere.assign(options.dseg,((dword *)(&data[0]))[0]);
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
}
break;
case ARG_RELIMM:
data=tblock->data+tblock->length;
outhere=tblock->addr;
if(tblock->mode32)
{ data-=4;
outhere+=((dword *)data)[0]+tblock->length;
}
else
{ data-=2;
outhere+=(word)(((word *)data)[0]+tblock->length);
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
break;
case ARG_RELIMM8:
data=tblock->data+tblock->length-1;
outhere=tblock->addr;
if(tblock->mode32)
{ if(data[0]&0x80)
outhere+=(dword)(data[0]+0xffffff00+tblock->length);
else
outhere+=(dword)(data[0]+tblock->length);
}
else
{ if(data[0]&0x80)
outhere+=(word)(data[0]+0xff00+tblock->length);
else
outhere+=(word)(data[0]+tblock->length);
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
break;
case ARG_MMXMODRM:
case ARG_XMMMODRM:
case ARG_MODRM_S:
case ARG_MODRMM512:
case ARG_MODRMQ:
case ARG_MODRM_SREAL:
case ARG_MODRM_PTR:
case ARG_MODRM_WORD:
case ARG_MODRM_SINT:
case ARG_MODRM_EREAL:
case ARG_MODRM_DREAL:
case ARG_MODRM_WINT:
case ARG_MODRM_LINT:
case ARG_MODRM_BCD:
case ARG_MODRM_FPTR:
case ARG_MODRM:
data=tblock->data+tblock->modrm;
rm=(byte)((data[0]&0xc0)>>6);
modrm=(byte)(data[0]&0x07);
switch(rm)
{ case 0:
if(options.mode32)
{ if(modrm==5)
{ outhere.assign(tblock->addr.segm,((dword *)(&data[1]))[0]);
}
else if(modrm==4) // case 4=sib
{ sib=data[1];
if((sib&0x07)==5) // disp32
{ outhere.assign(tblock->addr.segm,((dword *)(&data[2]))[0]);
}
}
}
else
{ if(modrm==6)
{ outhere.assign(tblock->addr.segm,((word *)(&data[1]))[0]);
}
}
break;
case 1:
break;
case 2:
if(options.mode32)
{ outhere.assign(tblock->addr.segm,((dword *)(&data[1]))[0]);
if(modrm==4) // case 4=sib
{ outhere.assign(tblock->addr.segm,((dword *)(&data[2]))[0]);
}
}
else
{ outhere.assign(tblock->addr.segm,((word *)(&data[1]))[0]);
}
break;
case 3:
break;
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
madejump=true;
}
break;
default:
break;
}
}
if(!madejump)
{ switch(DSMITEM_ARG2(tblock))
{ case ARG_IMM32:
if(tblock->override!=over_dsoffset)
break;
data=tblock->data+tblock->length;
data-=4;
outhere.assign(options.dseg,((dword *)(&data[0]))[0]);
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
}
break;
case ARG_IMM:
if(tblock->override!=over_dsoffset)
break;
if(options.mode32)
{ data=tblock->data+tblock->length;
data-=4;
outhere.assign(options.dseg,((dword *)(&data[0]))[0]);
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
}
}
break;
case ARG_MEMLOC:
data=tblock->data+tblock->length;
if(options.mode32)
{ data-=4;
outhere.assign(tblock->addr.segm,((dword *)data)[0]);
}
else
{ data-=2;
outhere.assign(tblock->addr.segm,((word *)data)[0]);
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
}
break;
case ARG_MMXMODRM:
case ARG_XMMMODRM:
case ARG_MODRM_S:
case ARG_MODRMM512:
case ARG_MODRMQ:
case ARG_MODRM_SREAL:
case ARG_MODRM_PTR:
case ARG_MODRM_WORD:
case ARG_MODRM_SINT:
case ARG_MODRM_EREAL:
case ARG_MODRM_DREAL:
case ARG_MODRM_WINT:
case ARG_MODRM_LINT:
case ARG_MODRM_BCD:
case ARG_MODRM_FPTR:
case ARG_MODRM:
data=tblock->data+tblock->modrm;
rm=(byte)((data[0]&0xc0)>>6);
modrm=(byte)(data[0]&0x07);
switch(rm)
{ case 0:
if(options.mode32)
{ if(modrm==5)
{ outhere.assign(tblock->addr.segm,((dword *)(&data[1]))[0]);
}
else if(modrm==4) // case 4=sib
{ sib=data[1];
if((sib&0x07)==5) // disp32
{ outhere.assign(tblock->addr.segm,((dword *)(&data[2]))[0]);
}
}
}
else
{ if(modrm==6)
{ outhere.assign(tblock->addr.segm,((word *)(&data[1]))[0]);
}
}
break;
case 1:
break;
case 2:
if(options.mode32)
{ outhere.assign(tblock->addr.segm,((dword *)(&data[1]))[0]);
if(modrm==4) // case 4=sib
{ outhere.assign(tblock->addr.segm,((dword *)(&data[2]))[0]);
}
}
else
{ outhere.assign(tblock->addr.segm,((word *)(&data[1]))[0]);
}
break;
case 3:
break;
}
if(dta.findseg(outhere)!=NULL)
{ retstack.push(curraddr);
setcuraddr(outhere);
updatewindow();
}
break;
default:
break;
}
}
}
}
/************************************************************************
* findcurrentline *
* - this routine finds the current screen address and output line in *
* the disassembly database and from there works out the disassembly *
* item on the currently selected line. *
* - it is used by jumpto, when the user presses return to follow a jump *
* - comments added to the procedure, it is a useful one to follow and *
* see the strategy employed, which is a fairly common Borg strategy *
************************************************************************/
dsmitem *disio::findcurrentline(void)
{ dsmitem titem,*tblock;
lptr outhere;
unsigned int i;
// strategy
// - use pointer to first item if available (so comments,etc included in list
// - otherwise use address.
titem.addr=curraddr;
titem.type=subitem;
// hunt for current addr and subitem
tblock=dsm.find(&titem);
if(tblock!=NULL)
tblock=dsm.nextiterator();
// on overlap - reset the curraddr.
// [on the spot error correction]
if(tblock!=NULL)
{ if(curraddr.between(tblock->addr,tblock->addr+tblock->length-1))
curraddr.offs=tblock->addr.offs;
}
// ensure we point to the right item, or the next one
if(tblock!=NULL)
{ if((tblock->addr<curraddr)||((tblock->addr==curraddr)&&(tblock->type<subitem)))
tblock=dsm.nextiterator();
}
// now at the top of the screen, the next loop moves down to the user selection line
outhere=curraddr;
for(i=0;i<usersel;i++)
{ if(tblock!=NULL)
{ if(outhere==tblock->addr)
{ outhere+=tblock->length;
tblock=dsm.nextiterator();
}
else
outhere++;
}
else
outhere++;
// check if gone beyond seg, get next seg.
if(dta.beyondseg(outhere))
{ outhere.offs--;
dta.nextseg(&outhere);
}
if(!outhere.segm)
break;
}
// now we either have the line we are pointing to, in the database
// or we have moved beyond the database and have a null
// or we have an address which would be a db
if(tblock!=NULL)
if(outhere!=tblock->addr)
return NULL;
return tblock;
}
/************************************************************************
* findcurrentaddr *
* - this is very similar to findcurrentline, but it instead finds just *
* the location. the search strategy is the same *
************************************************************************/
void disio::findcurrentaddr(lptr *loc)
{ dsmitem titem,*tblock;
lptr outhere;
unsigned int i;
// strategy
// - use pointer to first item if available (so comments,etc included in list
// - otherwise use address.
titem.addr=curraddr;
titem.type=subitem;
tblock=dsm.find(&titem);
if(tblock!=NULL)
tblock=dsm.nextiterator();
if(tblock!=NULL)
{ if(curraddr.between(tblock->addr,tblock->addr+tblock->length-1))
curraddr.offs=tblock->addr.offs;
}
if(tblock!=NULL)
{ if(tblock->addr<curraddr)
tblock=dsm.nextiterator();
}
// added 2.25 - bugfix
// - wasnt finding correct lines when in mid-line......
if(tblock!=NULL)
{ while((tblock->addr==curraddr)&&(tblock->type<subitem))
{ tblock=dsm.nextiterator();
if(tblock==NULL)
break;
}
}
outhere=curraddr;
for(i=0;i<usersel;i++)
{ if(tblock!=NULL)
{ if(outhere==tblock->addr)
{ outhere+=tblock->length;
tblock=dsm.nextiterator();
}
else
outhere++;
}
else
outhere++;
// check if gone beyond seg, get next seg.
if(dta.beyondseg(outhere))
{ outhere.offs--;
dta.nextseg(&outhere);
}
if(!outhere.segm)
break;
}
if(outhere.segm)
(*loc)=outhere;