Subversion Repositories Kolibri OS

Rev

Rev 31 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 halyavin 1
 
2
; Copyright (c) 1999-2006, Tomasz Grysztar.
109 heavyiron 3
; All rights reserved.
31 halyavin 4
5
 
6
        xor     eax,eax
109 heavyiron 7
        mov     [stub_size],eax
8
        mov     [number_of_sections],eax
9
        mov     [current_pass],ax
10
        mov     [resolver_flags],eax
11
      assembler_loop:
31 halyavin 12
        mov     eax,[labels_list]
109 heavyiron 13
        mov     [display_buffer],eax
14
        mov     eax,[additional_memory]
15
        mov     [free_additional_memory],eax
16
        mov     eax,[additional_memory_end]
17
        mov     [structures_buffer],eax
18
        mov     esi,[source_start]
19
        mov     edi,[code_start]
20
        xor     eax,eax
21
        mov     dword [adjustment],eax
22
        mov     dword [adjustment+4],eax
23
        mov     dword [org_origin],edi
24
        mov     dword [org_origin+4],eax
25
        mov     [org_start],edi
26
        mov     [org_registers],eax
27
        mov     [org_symbol],eax
28
        mov     [error_line],eax
29
        mov     [counter],eax
30
        mov     [format_flags],eax
31
        mov     [number_of_relocations],eax
32
        mov     [undefined_data_end],eax
33
        mov     [next_pass_needed],al
34
        mov     [output_format],al
35
        mov     [labels_type],al
36
        mov     [virtual_data],al
37
        mov     [code_type],16
38
      pass_loop:
31 halyavin 39
        call    assemble_line
109 heavyiron 40
        jnc     pass_loop
41
        mov     eax,[additional_memory_end]
42
        cmp     eax,[structures_buffer]
43
        je      pass_done
44
        sub     eax,20h
45
        mov     eax,[eax+4]
46
        mov     [current_line],eax
47
        jmp     missing_end_directive
48
      pass_done:
31 halyavin 49
        call    close_pass
109 heavyiron 50
        mov     eax,[labels_list]
51
      check_symbols:
31 halyavin 52
        cmp     eax,[memory_end]
109 heavyiron 53
        jae     symbols_checked
54
        test    byte [eax+8],8
55
        jz      symbol_defined_ok
56
        mov     cx,[current_pass]
57
        cmp     cx,[eax+18]
58
        jne     symbol_defined_ok
59
        test    byte [eax+8],1
60
        jz      symbol_defined_ok
61
        sub     cx,[eax+16]
62
        cmp     cx,1
63
        jne     symbol_defined_ok
64
        and     byte [eax+8],not 1
65
        or      [next_pass_needed],-1
66
      symbol_defined_ok:
31 halyavin 67
        test    byte [eax+8],10h
109 heavyiron 68
        jz      use_prediction_ok
69
        mov     cx,[current_pass]
70
        and     byte [eax+8],not 10h
71
        test    byte [eax+8],20h
72
        jnz     check_use_prediction
73
        cmp     cx,[eax+18]
74
        jne     use_prediction_ok
75
        test    byte [eax+8],8
76
        jz      use_prediction_ok
77
        jmp     use_misprediction
78
      check_use_prediction:
31 halyavin 79
        test    byte [eax+8],8
109 heavyiron 80
        jz      use_misprediction
81
        cmp     cx,[eax+18]
82
        je      use_prediction_ok
83
      use_misprediction:
31 halyavin 84
        or      [next_pass_needed],-1
109 heavyiron 85
      use_prediction_ok:
31 halyavin 86
        test    byte [eax+8],40h
109 heavyiron 87
        jz      check_next_symbol
88
        and     byte [eax+8],not 40h
89
        test    byte [eax+8],4
90
        jnz     define_misprediction
91
        mov     cx,[current_pass]
92
        test    byte [eax+8],80h
93
        jnz     check_define_prediction
94
        cmp     cx,[eax+16]
95
        jne     check_next_symbol
96
        test    byte [eax+8],1
97
        jz      check_next_symbol
98
        jmp     define_misprediction
99
      check_define_prediction:
31 halyavin 100
        test    byte [eax+8],1
109 heavyiron 101
        jz      define_misprediction
102
        cmp     cx,[eax+16]
103
        je      check_next_symbol
104
      define_misprediction:
31 halyavin 105
        or      [next_pass_needed],-1
109 heavyiron 106
      check_next_symbol:
31 halyavin 107
        add     eax,LABEL_STRUCTURE_SIZE
109 heavyiron 108
        jmp     check_symbols
109
      symbols_checked:
31 halyavin 110
        cmp     [next_pass_needed],0
109 heavyiron 111
        jne     next_pass
112
        mov     eax,[error_line]
113
        or      eax,eax
114
        jz      assemble_ok
115
        mov     [current_line],eax
116
        jmp     near [error]
117
      next_pass:
31 halyavin 118
        inc     [current_pass]
109 heavyiron 119
        mov     ax,[current_pass]
120
        cmp     ax,[passes_limit]
121
        je      code_cannot_be_generated
122
        jmp     assembler_loop
123
      assemble_ok:
31 halyavin 124
        ret
109 heavyiron 125
31 halyavin 126
 
127
        mov     eax,[display_buffer]
109 heavyiron 128
        sub     eax,100h
129
        cmp     edi,eax
130
        ja      out_of_memory
131
        lods    byte [esi]
132
        cmp     al,1
133
        je      assemble_instruction
134
        jb      source_end
135
        cmp     al,3
136
        jb      define_label
137
        je      define_constant
138
        cmp     al,0Fh
139
        je      new_line
140
        cmp     al,13h
141
        je      code_type_setting
142
        cmp     al,10h
143
        jne     illegal_instruction
144
        lods    byte [esi]
145
        jmp     segment_prefix
146
      code_type_setting:
31 halyavin 147
        lods    byte [esi]
109 heavyiron 148
        mov     [code_type],al
149
        jmp     line_assembled
150
      new_line:
31 halyavin 151
        lods    dword [esi]
109 heavyiron 152
        mov     [current_line],eax
153
        mov     [prefixed_instruction],0
154
      continue_line:
31 halyavin 155
        cmp     byte [esi],0Fh
109 heavyiron 156
        je      line_assembled
157
        jmp     assemble_line
158
      define_label:
31 halyavin 159
        lods    dword [esi]
109 heavyiron 160
        cmp     eax,0Fh
161
        jb      invalid_use_of_symbol
162
        je      reserved_word_used_as_symbol
163
        mov     ebx,eax
164
        lods    byte [esi]
165
        mov     cl,al
166
        mov     eax,edi
167
        xor     edx,edx
168
        sub     eax,dword [org_origin]
169
        sbb     edx,dword [org_origin+4]
170
        mov     ch,[labels_type]
171
        cmp     [virtual_data],0
172
        jne     make_virtual_label
173
        or      byte [ebx+9],1
174
        xchg    eax,[ebx]
175
        xchg    edx,[ebx+4]
176
        sub     eax,[ebx]
177
        sbb     edx,[ebx+4]
178
        mov     dword [adjustment],eax
179
        mov     dword [adjustment+4],edx
180
        or      eax,edx
181
        setnz   ah
182
        jmp     finish_label_symbol
183
      make_virtual_label:
31 halyavin 184
        and     byte [ebx+9],not 1
109 heavyiron 185
        cmp     eax,[ebx]
186
        mov     [ebx],eax
187
        setne   ah
188
        cmp     edx,[ebx+4]
189
        mov     [ebx+4],edx
190
        setne   al
191
        or      ah,al
192
      finish_label_symbol:
31 halyavin 193
        cmp     cl,[ebx+10]
109 heavyiron 194
        mov     [ebx+10],cl
195
        setne   al
196
        or      ah,al
197
        cmp     ch,[ebx+11]
198
        mov     [ebx+11],ch
199
        setne   al
200
        or      ah,al
201
        mov     edx,[org_registers]
202
        cmp     edx,[ebx+12]
203
        mov     [ebx+12],edx
204
        setne   al
205
        or      ah,al
206
        or      ch,ch
207
        jz      label_symbol_ok
208
        mov     edx,[org_symbol]
209
        cmp     edx,[ebx+20]
210
        mov     [ebx+20],edx
211
        setne   al
212
        or      ah,al
213
      label_symbol_ok:
31 halyavin 214
        mov     cx,[current_pass]
109 heavyiron 215
        xchg    [ebx+16],cx
216
        mov     edx,[current_line]
217
        mov     [ebx+28],edx
218
        and     byte [ebx+8],not 2
219
        test    byte [ebx+8],1
220
        jz      new_label
221
        cmp     cx,[ebx+16]
222
        je      symbol_already_defined
223
        inc     cx
224
        sub     cx,[ebx+16]
225
        setnz   al
226
        or      ah,al
227
        jz      continue_line
228
        test    byte [ebx+8],8
229
        jz      continue_line
230
        mov     cx,[current_pass]
231
        cmp     cx,[ebx+18]
232
        jne     continue_line
233
        or      [next_pass_needed],-1
234
        jmp     continue_line
235
      new_label:
31 halyavin 236
        or      byte [ebx+8],1
109 heavyiron 237
        jmp     continue_line
238
      define_constant:
31 halyavin 239
        lods    dword [esi]
109 heavyiron 240
        inc     esi
241
        cmp     eax,0Fh
242
        jb      invalid_use_of_symbol
243
        je      reserved_word_used_as_symbol
244
        mov     edx,[eax+8]
245
        push    edx
246
        cmp     [current_pass],0
247
        je      get_constant_value
248
        test    dl,4
249
        jnz     get_constant_value
250
        mov     cx,[current_pass]
251
        cmp     cx,[eax+16]
252
        je      get_constant_value
253
        and     dl,not 1
254
        mov     [eax+8],dl
255
      get_constant_value:
31 halyavin 256
        push    eax
109 heavyiron 257
        mov     al,byte [esi-1]
258
        push    eax
259
        call    get_value
260
        pop     ebx
261
        mov     ch,bl
262
        pop     ebx
263
        pop     dword [ebx+8]
264
        cmp     ebx,0Fh
265
        jb      invalid_use_of_symbol
266
        je      reserved_word_used_as_symbol
267
        xor     cl,cl
268
        mov     ch,[value_type]
269
        cmp     ch,3
270
        je      invalid_use_of_symbol
271
      make_constant:
31 halyavin 272
        and     byte [ebx+9],not 1
109 heavyiron 273
        cmp     eax,[ebx]
274
        mov     [ebx],eax
275
        setne   ah
276
        cmp     edx,[ebx+4]
277
        mov     [ebx+4],edx
278
        setne   al
279
        or      ah,al
280
        cmp     cl,[ebx+10]
281
        mov     [ebx+10],cl
282
        setne   al
283
        or      ah,al
284
        cmp     ch,[ebx+11]
285
        mov     [ebx+11],ch
286
        setne   al
287
        or      ah,al
288
        xor     edx,edx
289
        cmp     edx,[ebx+12]
290
        mov     [ebx+12],edx
291
        setne   al
292
        or      ah,al
293
        or      ch,ch
294
        jz      constant_symbol_ok
295
        mov     edx,[symbol_identifier]
296
        cmp     edx,[ebx+20]
297
        mov     [ebx+20],edx
298
        setne   al
299
        or      ah,al
300
      constant_symbol_ok:
31 halyavin 301
        mov     cx,[current_pass]
109 heavyiron 302
        xchg    [ebx+16],cx
303
        mov     edx,[current_line]
304
        mov     [ebx+28],edx
305
        test    byte [ebx+8],1
306
        jz      new_constant
307
        cmp     cx,[ebx+16]
308
        jne     redeclare_constant
309
        test    byte [ebx+8],2
310
        jz      symbol_already_defined
311
        or      byte [ebx+8],4
312
        jmp     instruction_assembled
313
      redeclare_constant:
31 halyavin 314
        inc     cx
109 heavyiron 315
        sub     cx,[ebx+16]
316
        setnz   al
317
        or      ah,al
318
        jz      instruction_assembled
319
        test    byte [ebx+8],4
320
        jnz     instruction_assembled
321
        test    byte [ebx+8],8
322
        jz      instruction_assembled
323
        mov     cx,[current_pass]
324
        cmp     cx,[ebx+18]
325
        jne     instruction_assembled
326
        or      [next_pass_needed],-1
327
        jmp     instruction_assembled
328
      new_constant:
31 halyavin 329
        or      byte [ebx+8],1+2
109 heavyiron 330
        jmp     instruction_assembled
331
      assemble_instruction:
31 halyavin 332
        mov     [operand_size],0
109 heavyiron 333
        mov     [size_override],0
334
        mov     [operand_prefix],0
335
        mov     [rex_prefix],0
336
        mov     [immediate_size],0
337
        movzx   ebx,word [esi]
338
        mov     al,[esi+2]
339
        add     ebx,assembler
340
        add     esi,3
341
        jmp     near ebx
342
      instruction_assembled:
31 halyavin 343
        mov     al,[esi]
109 heavyiron 344
        cmp     al,0Fh
345
        je      line_assembled
346
        or      al,al
347
        jnz     extra_characters_on_line
348
      line_assembled:
31 halyavin 349
        clc
109 heavyiron 350
        ret
351
      source_end:
31 halyavin 352
        dec     esi
109 heavyiron 353
        stc
354
        ret
355
skip_line:
31 halyavin 356
        call    skip_symbol
109 heavyiron 357
        jnc     skip_line
358
        ret
359
skip_symbol:
31 halyavin 360
        lods    byte [esi]
109 heavyiron 361
        or      al,al
362
        jz      nothing_to_skip
363
        cmp     al,0Fh
364
        je      nothing_to_skip
365
        cmp     al,1
366
        je      skip_instruction
367
        cmp     al,2
368
        je      skip_label
369
        cmp     al,3
370
        je      skip_label
371
        cmp     al,20h
372
        jb      skip_assembler_symbol
373
        cmp     al,'('
374
        je      skip_expression
375
        cmp     al,'['
376
        je      skip_address
377
      skip_done:
31 halyavin 378
        clc
109 heavyiron 379
        ret
380
      skip_label:
31 halyavin 381
        add     esi,2
109 heavyiron 382
      skip_instruction:
31 halyavin 383
        add     esi,2
109 heavyiron 384
      skip_assembler_symbol:
31 halyavin 385
        inc     esi
109 heavyiron 386
        jmp     skip_done
387
      skip_address:
31 halyavin 388
        mov     al,[esi]
109 heavyiron 389
        and     al,11110000b
390
        cmp     al,60h
391
        jb      skip_expression
392
        cmp     al,70h
393
        ja      skip_expression
394
        inc     esi
395
        jmp     skip_address
396
      skip_expression:
31 halyavin 397
        lods    byte [esi]
109 heavyiron 398
        or      al,al
399
        jz      skip_string
400
        cmp     al,'.'
401
        je      skip_fp_value
402
        cmp     al,')'
403
        je      skip_done
404
        cmp     al,']'
405
        je      skip_done
406
        cmp     al,'!'
407
        je      skip_expression
408
        cmp     al,0Fh
409
        je      skip_expression
410
        cmp     al,10h
411
        je      skip_register
412
        cmp     al,11h
413
        je      skip_label_value
414
        cmp     al,80h
415
        jae     skip_expression
416
        movzx   eax,al
417
        add     esi,eax
418
        jmp     skip_expression
419
      skip_label_value:
31 halyavin 420
        add     esi,3
109 heavyiron 421
      skip_register:
31 halyavin 422
        inc     esi
109 heavyiron 423
        jmp     skip_expression
424
      skip_fp_value:
31 halyavin 425
        add     esi,12
109 heavyiron 426
        jmp     skip_done
427
      skip_string:
31 halyavin 428
        lods    dword [esi]
109 heavyiron 429
        add     esi,eax
430
        inc     esi
431
        jmp     skip_done
432
      nothing_to_skip:
31 halyavin 433
        dec     esi
109 heavyiron 434
        stc
435
        ret
436
31 halyavin 437
 
438
        lods    byte [esi]
109 heavyiron 439
        cmp     al,'('
440
        jne     invalid_argument
441
        cmp     byte [esi],'.'
442
        je      invalid_value
443
        call    get_qword_value
444
        mov     cl,[value_type]
445
        test    cl,1
446
        jnz     invalid_use_of_symbol
447
        mov     [labels_type],cl
448
        mov     ecx,edi
449
        sub     ecx,eax
450
        adc     edx,0
451
        neg     edx
452
        mov     dword [org_origin],ecx
453
        mov     dword [org_origin+4],edx
454
        mov     [org_registers],0
455
        mov     [org_start],edi
456
        mov     edx,[symbol_identifier]
457
        mov     [org_symbol],edx
458
        cmp     [output_format],1
459
        ja      instruction_assembled
460
        cmp     edi,[code_start]
461
        jne     instruction_assembled
462
        cmp     eax,100h
463
        jne     instruction_assembled
464
        bts     [format_flags],0
465
        jmp     instruction_assembled
466
label_directive:
31 halyavin 467
        lods    byte [esi]
109 heavyiron 468
        cmp     al,2
469
        jne     invalid_argument
470
        lods    dword [esi]
471
        cmp     eax,0Fh
472
        jb      invalid_use_of_symbol
473
        je      reserved_word_used_as_symbol
474
        inc     esi
475
        mov     ebx,eax
476
        xor     cl,cl
477
        lods    byte [esi]
478
        cmp     al,':'
479
        je      get_label_size
480
        dec     esi
481
        cmp     al,11h
482
        jne     label_size_ok
483
      get_label_size:
31 halyavin 484
        lods    word [esi]
109 heavyiron 485
        cmp     al,11h
486
        jne     invalid_argument
487
        mov     cl,ah
488
      label_size_ok:
31 halyavin 489
        mov     eax,edi
109 heavyiron 490
        xor     edx,edx
491
        sub     eax,dword [org_origin]
492
        sbb     edx,dword [org_origin+4]
493
        mov     ebp,[org_registers]
494
        cmp     byte [esi],80h
495
        je      get_free_label_value
496
        mov     ch,[labels_type]
497
        push    [org_symbol]
498
        pop     [address_symbol]
499
        cmp     [virtual_data],0
500
        jne     make_free_label
501
        or      byte [ebx+9],1
502
        xchg    eax,[ebx]
503
        xchg    edx,[ebx+4]
504
        sub     eax,[ebx]
505
        sbb     edx,[ebx+4]
506
        mov     dword [adjustment],eax
507
        mov     dword [adjustment+4],edx
508
        or      eax,edx
509
        setne   ah
510
        jmp     finish_label
511
      get_free_label_value:
31 halyavin 512
        inc     esi
109 heavyiron 513
        lods    byte [esi]
514
        cmp     al,'('
515
        jne     invalid_argument
516
        push    dword [ebx+8]
517
        push    ebx ecx
518
        and     byte [ebx+8],not 1
519
        cmp     byte [esi],'.'
520
        je      invalid_value
521
        call    get_address_value
522
        or      bh,bh
523
        setnz   ch
524
        xchg    ch,cl
525
        mov     bp,cx
526
        shl     ebp,16
527
        xchg    bl,bh
528
        mov     bp,bx
529
        pop     ecx ebx
530
        pop     dword [ebx+8]
531
        mov     ch,[value_type]
532
        or      ch,ch
533
        jz      make_free_label
534
        cmp     ch,4
535
        je      make_free_label
536
        cmp     ch,2
537
        jne     invalid_use_of_symbol
538
      make_free_label:
31 halyavin 539
        and     byte [ebx+9],not 1
109 heavyiron 540
        cmp     eax,[ebx]
541
        mov     [ebx],eax
542
        setne   ah
543
        cmp     edx,[ebx+4]
544
        mov     [ebx+4],edx
545
        setne   al
546
        or      ah,al
547
        jmp     finish_label
548
      finish_label:
31 halyavin 549
        cmp     cl,[ebx+10]
109 heavyiron 550
        mov     [ebx+10],cl
551
        setne   al
552
        or      ah,al
553
        cmp     ch,[ebx+11]
554
        mov     [ebx+11],ch
555
        setne   al
556
        or      ah,al
557
        cmp     ebp,[ebx+12]
558
        mov     [ebx+12],ebp
559
        setne   al
560
        or      ah,al
561
        or      ch,ch
562
        jz      free_label_symbol_ok
563
        mov     edx,[address_symbol]
564
        cmp     edx,[ebx+20]
565
        mov     [ebx+20],edx
566
        setne   al
567
        or      ah,al
568
      free_label_symbol_ok:
31 halyavin 569
        mov     cx,[current_pass]
109 heavyiron 570
        xchg    [ebx+16],cx
571
        mov     edx,[current_line]
572
        mov     [ebx+28],edx
573
        and     byte [ebx+8],not 2
574
        test    byte [ebx+8],1
575
        jz      new_free_label
576
        cmp     cx,[ebx+16]
577
        je      symbol_already_defined
578
        inc     cx
579
        sub     cx,[ebx+16]
580
        setnz   al
581
        or      ah,al
582
        jz      instruction_assembled
583
        test    byte [ebx+8],8
584
        jz      instruction_assembled
585
        mov     cx,[current_pass]
586
        cmp     cx,[ebx+18]
587
        jne     instruction_assembled
588
        or      [next_pass_needed],-1
589
        jmp     instruction_assembled
590
      new_free_label:
31 halyavin 591
        or      byte [ebx+8],1
109 heavyiron 592
        jmp     instruction_assembled
593
load_directive:
31 halyavin 594
        lods    byte [esi]
109 heavyiron 595
        cmp     al,2
596
        jne     invalid_argument
597
        lods    dword [esi]
598
        cmp     eax,0Fh
599
        jb      invalid_use_of_symbol
600
        je      reserved_word_used_as_symbol
601
        inc     esi
602
        push    eax
603
        mov     al,1
604
        cmp     byte [esi],11h
605
        jne     load_size_ok
606
        lods    byte [esi]
607
        lods    byte [esi]
608
      load_size_ok:
31 halyavin 609
        cmp     al,8
109 heavyiron 610
        ja      invalid_value
611
        mov     [operand_size],al
612
        mov     dword [value],0
613
        mov     dword [value+4],0
614
        lods    word [esi]
615
        cmp     ax,82h+'(' shl 8
616
        jne     invalid_argument
617
      load_from_code:
31 halyavin 618
        cmp     byte [esi],'.'
109 heavyiron 619
        je      invalid_value
620
        call    get_relative_offset
621
        neg     eax
622
        cmp     [next_pass_needed],0
623
        jne     load_address_ok
624
        cmp     [value_type],0
625
        jne     invalid_use_of_symbol
626
      load_address_ok:
31 halyavin 627
        push    esi edi
109 heavyiron 628
        mov     esi,edi
629
        sub     esi,eax
630
        jc      bad_load_address
631
        cmp     esi,[org_start]
632
        jb      bad_load_address
633
        mov     edi,value
634
        movzx   ecx,[operand_size]
635
        cmp     ecx,eax
636
        ja      bad_load_address
637
        rep     movs byte [edi],[esi]
638
        jmp     value_loaded
639
      bad_load_address:
31 halyavin 640
        cmp     [error_line],0
109 heavyiron 641
        jne     value_loaded
642
        mov     eax,[current_line]
643
        mov     [error_line],eax
644
        mov     [error],value_out_of_range
645
      value_loaded:
31 halyavin 646
        pop     edi esi
109 heavyiron 647
        mov     eax,dword [value]
648
        mov     edx,dword [value+4]
649
        pop     ebx
650
        xor     cx,cx
651
        jmp     make_constant
652
store_directive:
31 halyavin 653
        cmp     byte [esi],11h
109 heavyiron 654
        je      sized_store
655
        lods    byte [esi]
656
        cmp     al,'('
657
        jne     invalid_argument
658
        call    get_byte_value
659
        xor     edx,edx
660
        movzx   eax,al
661
        mov     [operand_size],1
662
        jmp     store_value_ok
663
      sized_store:
31 halyavin 664
        call    get_value
109 heavyiron 665
      store_value_ok:
31 halyavin 666
        cmp     [value_type],0
109 heavyiron 667
        jne     invalid_use_of_symbol
668
        mov     dword [value],eax
669
        mov     dword [value+4],edx
670
        lods    word [esi]
671
        cmp     ax,80h+'(' shl 8
672
        jne     invalid_argument
673
        cmp     byte [esi],'.'
674
        je      invalid_value
675
        call    get_relative_offset
676
        neg     eax
677
        cmp     [next_pass_needed],0
678
        jne     store_address_ok
679
        cmp     [value_type],0
680
        jne     invalid_use_of_symbol
681
      store_address_ok:
31 halyavin 682
        push    esi edi
109 heavyiron 683
        sub     edi,eax
684
        jc      bad_store_address
685
        cmp     edi,[org_start]
686
        jb      bad_store_address
687
        mov     esi,value
688
        movzx   ecx,[operand_size]
689
        cmp     ecx,eax
690
        ja      bad_store_address
691
        rep     movs byte [edi],[esi]
692
        mov     eax,edi
693
        pop     edi esi
694
        cmp     edi,[undefined_data_end]
695
        jne     instruction_assembled
696
        cmp     eax,[undefined_data_start]
697
        jbe     instruction_assembled
698
        mov     [undefined_data_start],eax
699
        jmp     instruction_assembled
700
      bad_store_address:
31 halyavin 701
        pop     edi esi
109 heavyiron 702
        cmp     [error_line],0
703
        jne     instruction_assembled
704
        mov     eax,[current_line]
705
        mov     [error_line],eax
706
        mov     [error],value_out_of_range
707
        jmp     instruction_assembled
708
31 halyavin 709
 
710
        lods    byte [esi]
109 heavyiron 711
        cmp     al,'('
712
        jne     invalid_argument
713
        cmp     byte [esi],0
714
        jne     display_byte
715
        inc     esi
716
        lods    dword [esi]
717
        mov     ecx,eax
718
        push    edi
719
        mov     edi,[display_buffer]
720
        sub     edi,4
721
        sub     edi,eax
722
        mov     [display_buffer],edi
723
        rep     movs byte [edi],[esi]
724
        stos    dword [edi]
725
        pop     edi
726
        inc     esi
727
        jmp     display_next
728
      display_byte:
31 halyavin 729
        call    get_byte_value
109 heavyiron 730
        push    edi
731
        mov     edi,[display_buffer]
732
        sub     edi,4+1
733
        mov     [display_buffer],edi
734
        stos    byte [edi]
735
        mov     eax,1
736
        stos    dword [edi]
737
        pop     edi
738
      display_next:
31 halyavin 739
        cmp     edi,[display_buffer]
109 heavyiron 740
        ja      out_of_memory
741
        lods    byte [esi]
742
        cmp     al,','
743
        je      display_directive
744
        dec     esi
745
        jmp     instruction_assembled
746
flush_display_buffer:
31 halyavin 747
        mov     eax,[display_buffer]
109 heavyiron 748
        or      eax,eax
749
        jz      display_done
750
        mov     esi,[labels_list]
751
        cmp     esi,eax
752
        je      display_done
753
      display_messages:
31 halyavin 754
        sub     esi,4
109 heavyiron 755
        mov     ecx,[esi]
756
        sub     esi,ecx
757
        push    esi
758
        call    display_block
759
        pop     esi
760
        cmp     esi,[display_buffer]
761
        jne     display_messages
762
        mov     eax,[labels_list]
763
        mov     [display_buffer],eax
764
      display_done:
31 halyavin 765
        ret
109 heavyiron 766
times_directive:
31 halyavin 767
        lods    byte [esi]
109 heavyiron 768
        cmp     al,'('
769
        jne     invalid_argument
770
        cmp     byte [esi],'.'
771
        je      invalid_value
772
        call    get_dword_value
773
        cmp     [next_pass_needed],0
774
        jne     times_value_ok
775
        cmp     [value_type],0
776
        jne     invalid_use_of_symbol
777
      times_value_ok:
31 halyavin 778
        cmp     eax,0
109 heavyiron 779
        je      zero_times
780
        jl      negative_times
781
        cmp     byte [esi],':'
782
        jne     times_argument_ok
783
        inc     esi
784
      times_argument_ok:
31 halyavin 785
        push    [counter]
109 heavyiron 786
        push    [counter_limit]
787
        mov     [counter_limit],eax
788
        mov     [counter],1
789
      times_loop:
31 halyavin 790
        mov     eax,esp
109 heavyiron 791
        sub     eax,100h
792
        jc      stack_overflow
793
        cmp     eax,[stack_limit]
794
        jb      stack_overflow
795
        push    esi
796
        or      [prefixed_instruction],-1
797
        call    continue_line
798
        mov     eax,[counter_limit]
799
        cmp     [counter],eax
800
        je      times_done
801
        inc     [counter]
802
        pop     esi
803
        jmp     times_loop
804
      times_done:
31 halyavin 805
        pop     eax
109 heavyiron 806
        pop     [counter_limit]
807
        pop     [counter]
808
        jmp     instruction_assembled
809
      negative_times:
31 halyavin 810
        cmp     [error_line],0
109 heavyiron 811
        jne     zero_times
812
        mov     eax,[current_line]
813
        mov     [error_line],eax
814
        mov     [error],invalid_value
815
      zero_times:
31 halyavin 816
        call    skip_line
109 heavyiron 817
        jmp     instruction_assembled
818
31 halyavin 819
 
820
        lods    byte [esi]
109 heavyiron 821
        cmp     al,80h
822
        jne     virtual_at_current
823
        lods    byte [esi]
824
        cmp     al,'('
825
        jne     invalid_argument
826
        cmp     byte [esi],'.'
827
        je      invalid_value
828
        call    get_address_value
829
        mov     ebp,[address_symbol]
830
        xor     ch,ch
831
        or      bh,bh
832
        jz      set_virtual
833
        mov     ch,1
834
        jmp     set_virtual
835
      virtual_at_current:
31 halyavin 836
        dec     esi
109 heavyiron 837
        mov     al,[labels_type]
838
        mov     [value_type],al
839
        mov     ebp,[org_symbol]
840
        mov     eax,edi
841
        xor     edx,edx
842
        sub     eax,dword [org_origin]
843
        sbb     edx,dword [org_origin+4]
844
        mov     bx,word [org_registers]
845
        mov     cx,word [org_registers+2]
846
        xchg    bh,bl
847
        xchg    ch,cl
848
      set_virtual:
31 halyavin 849
        push    [org_registers]
109 heavyiron 850
        mov     byte [org_registers],bh
851
        mov     byte [org_registers+1],bl
852
        mov     byte [org_registers+2],ch
853
        mov     byte [org_registers+3],cl
854
        call    allocate_structure_data
855
        mov     word [ebx],virtual_directive-assembler
856
        not     eax
857
        not     edx
858
        add     eax,1
859
        adc     edx,0
860
        add     eax,edi
861
        adc     edx,0
862
        xchg    dword [org_origin],eax
863
        xchg    dword [org_origin+4],edx
864
        mov     [ebx+10h],eax
865
        mov     [ebx+14h],edx
866
        pop     eax
867
        mov     [ebx+18h],eax
868
        mov     al,[virtual_data]
869
        mov     [ebx+2],al
870
        mov     al,[labels_type]
871
        mov     [ebx+3],al
872
        mov     eax,edi
873
        xchg    eax,[org_start]
874
        mov     [ebx+0Ch],eax
875
        xchg    ebp,[org_symbol]
876
        mov     [ebx+1Ch],ebp
877
        mov     [ebx+8],edi
878
        mov     eax,[current_line]
879
        mov     [ebx+4],eax
880
        or      [virtual_data],-1
881
        mov     al,[value_type]
882
        test    al,1
883
        jnz     invalid_use_of_symbol
884
        mov     [labels_type],al
885
        jmp     instruction_assembled
886
      allocate_structure_data:
31 halyavin 887
        mov     ebx,[structures_buffer]
109 heavyiron 888
        sub     ebx,20h
889
        cmp     ebx,[free_additional_memory]
890
        jb      out_of_memory
891
        mov     [structures_buffer],ebx
892
        ret
893
      find_structure_data:
31 halyavin 894
        mov     ebx,[structures_buffer]
109 heavyiron 895
      scan_structures:
31 halyavin 896
        cmp     ebx,[additional_memory_end]
109 heavyiron 897
        je      no_such_structure
898
        cmp     ax,[ebx]
899
        je      structure_data_found
900
        add     ebx,20h
901
        jmp     scan_structures
902
      structure_data_found:
903
        ret
904
      no_such_structure:
31 halyavin 905
        stc
109 heavyiron 906
        ret
907
      end_virtual:
31 halyavin 908
        call    find_structure_data
109 heavyiron 909
        jc      unexpected_instruction
910
        mov     al,[ebx+2]
911
        mov     [virtual_data],al
912
        mov     al,[ebx+3]
913
        mov     [labels_type],al
914
        mov     eax,[ebx+10h]
915
        mov     dword [org_origin],eax
916
        mov     eax,[ebx+14h]
917
        mov     dword [org_origin+4],eax
918
        mov     eax,[ebx+18h]
919
        mov     [org_registers],eax
920
        mov     eax,[ebx+0Ch]
921
        mov     [org_start],eax
922
        mov     eax,[ebx+1Ch]
923
        mov     [org_symbol],eax
924
        mov     edi,[ebx+8]
925
      remove_structure_data:
31 halyavin 926
        push    esi edi
109 heavyiron 927
        mov     esi,[structures_buffer]
928
        mov     ecx,ebx
929
        sub     ecx,esi
930
        lea     edi,[esi+20h]
931
        mov     [structures_buffer],edi
932
        shr     ecx,2
933
        rep     movs dword [edi],[esi]
934
        pop     edi esi
935
        ret
936
repeat_directive:
31 halyavin 937
        cmp     [prefixed_instruction],0
109 heavyiron 938
        jne     unexpected_instruction
939
        lods    byte [esi]
940
        cmp     al,'('
941
        jne     invalid_argument
942
        cmp     byte [esi],'.'
943
        je      invalid_value
944
        call    get_dword_value
945
        cmp     [next_pass_needed],0
946
        jne     repeat_value_ok
947
        cmp     [value_type],0
948
        jne     invalid_use_of_symbol
949
      repeat_value_ok:
31 halyavin 950
        cmp     eax,0
109 heavyiron 951
        je      zero_repeat
952
        jl      negative_repeat
953
        call    allocate_structure_data
954
        mov     word [ebx],repeat_directive-assembler
955
        xchg    eax,[counter_limit]
956
        mov     [ebx+10h],eax
957
        mov     eax,1
958
        xchg    eax,[counter]
959
        mov     [ebx+14h],eax
960
        mov     [ebx+8],esi
961
        mov     eax,[current_line]
962
        mov     [ebx+4],eax
963
        jmp     instruction_assembled
964
      end_repeat:
31 halyavin 965
        cmp     [prefixed_instruction],0
109 heavyiron 966
        jne     unexpected_instruction
967
        call    find_structure_data
968
        jc      unexpected_instruction
969
        mov     eax,[counter_limit]
970
        inc     [counter]
971
        cmp     [counter],eax
972
        jbe     continue_repeating
973
      stop_repeat:
31 halyavin 974
        mov     eax,[ebx+10h]
109 heavyiron 975
        mov     [counter_limit],eax
976
        mov     eax,[ebx+14h]
977
        mov     [counter],eax
978
        call    remove_structure_data
979
        jmp     instruction_assembled
980
      continue_repeating:
31 halyavin 981
        mov     esi,[ebx+8]
109 heavyiron 982
        jmp     instruction_assembled
983
      negative_repeat:
31 halyavin 984
        cmp     [error_line],0
109 heavyiron 985
        jne     zero_repeat
986
        mov     eax,[current_line]
987
        mov     [error_line],eax
988
        mov     [error],invalid_value
989
      zero_repeat:
31 halyavin 990
        mov     al,[esi]
109 heavyiron 991
        or      al,al
992
        jz      missing_end_directive
993
        cmp     al,0Fh
994
        jne     extra_characters_on_line
995
        call    find_end_repeat
996
        jmp     instruction_assembled
997
      find_end_repeat:
31 halyavin 998
        call    find_structure_end
109 heavyiron 999
        cmp     ax,repeat_directive-assembler
1000
        jne     unexpected_instruction
1001
        ret
1002
while_directive:
31 halyavin 1003
        cmp     [prefixed_instruction],0
109 heavyiron 1004
        jne     unexpected_instruction
1005
        call    allocate_structure_data
1006
        mov     word [ebx],while_directive-assembler
1007
        mov     eax,1
1008
        xchg    eax,[counter]
1009
        mov     [ebx+10h],eax
1010
        mov     [ebx+8],esi
1011
        mov     eax,[current_line]
1012
        mov     [ebx+4],eax
1013
      do_while:
31 halyavin 1014
        push    ebx
109 heavyiron 1015
        call    calculate_logical_expression
1016
        or      al,al
1017
        jnz     while_true
1018
        mov     al,[esi]
1019
        or      al,al
1020
        jz      missing_end_directive
1021
        cmp     al,0Fh
1022
        jne     extra_characters_on_line
1023
      stop_while:
31 halyavin 1024
        call    find_end_while
109 heavyiron 1025
        pop     ebx
1026
        mov     eax,[ebx+10h]
1027
        mov     [counter],eax
1028
        call    remove_structure_data
1029
        jmp     instruction_assembled
1030
      while_true:
31 halyavin 1031
        pop     ebx
109 heavyiron 1032
        jmp     instruction_assembled
1033
      end_while:
31 halyavin 1034
        cmp     [prefixed_instruction],0
109 heavyiron 1035
        jne     unexpected_instruction
1036
        call    find_structure_data
1037
        jc      unexpected_instruction
1038
        mov     eax,[ebx+4]
1039
        mov     [current_line],eax
1040
        inc     [counter]
1041
        jz      too_many_repeats
1042
        mov     esi,[ebx+8]
1043
        jmp     do_while
1044
      find_end_while:
31 halyavin 1045
        call    find_structure_end
109 heavyiron 1046
        cmp     ax,while_directive-assembler
1047
        jne     unexpected_instruction
1048
        ret
1049
if_directive:
31 halyavin 1050
        cmp     [prefixed_instruction],0
109 heavyiron 1051
        jne     unexpected_instruction
1052
        call    calculate_logical_expression
1053
        mov     dl,al
1054
        mov     al,[esi]
1055
        or      al,al
1056
        jz      missing_end_directive
1057
        cmp     al,0Fh
1058
        jne     extra_characters_on_line
1059
        or      dl,dl
1060
        jnz     if_true
1061
        call    find_else
1062
        jc      instruction_assembled
1063
        mov     al,[esi]
1064
        cmp     al,1
1065
        jne     else_true
1066
        cmp     word [esi+1],if_directive-assembler
1067
        jne     else_true
1068
        add     esi,4
1069
        jmp     if_directive
1070
      if_true:
31 halyavin 1071
        xor     al,al
109 heavyiron 1072
      make_if_structure:
31 halyavin 1073
        call    allocate_structure_data
109 heavyiron 1074
        mov     word [ebx],if_directive-assembler
1075
        mov     byte [ebx+2],al
1076
        mov     eax,[current_line]
1077
        mov     [ebx+4],eax
1078
        jmp     instruction_assembled
1079
      else_true:
31 halyavin 1080
        or      al,al
109 heavyiron 1081
        jz      missing_end_directive
1082
        cmp     al,0Fh
1083
        jne     extra_characters_on_line
1084
        or      al,-1
1085
        jmp     make_if_structure
1086
      else_directive:
31 halyavin 1087
        cmp     [prefixed_instruction],0
109 heavyiron 1088
        jne     unexpected_instruction
1089
        mov     ax,if_directive-assembler
1090
        call    find_structure_data
1091
        jc      unexpected_instruction
1092
        cmp     byte [ebx+2],0
1093
        jne     unexpected_instruction
1094
      found_else:
31 halyavin 1095
        mov     al,[esi]
109 heavyiron 1096
        cmp     al,1
1097
        jne     skip_else
1098
        cmp     word [esi+1],if_directive-assembler
1099
        jne     skip_else
1100
        add     esi,4
1101
        call    find_else
1102
        jnc     found_else
1103
        call    remove_structure_data
1104
        jmp     instruction_assembled
1105
      skip_else:
31 halyavin 1106
        or      al,al
109 heavyiron 1107
        jz      missing_end_directive
1108
        cmp     al,0Fh
1109
        jne     extra_characters_on_line
1110
        call    find_end_if
1111
        call    remove_structure_data
1112
        jmp     instruction_assembled
1113
      end_if:
31 halyavin 1114
        cmp     [prefixed_instruction],0
109 heavyiron 1115
        jne     unexpected_instruction
1116
        call    find_structure_data
1117
        jc      unexpected_instruction
1118
        call    remove_structure_data
1119
        jmp     instruction_assembled
1120
      find_else:
31 halyavin 1121
        call    find_structure_end
109 heavyiron 1122
        cmp     ax,else_directive-assembler
1123
        je      else_found
1124
        cmp     ax,if_directive-assembler
1125
        jne     unexpected_instruction
1126
        stc
1127
        ret
1128
      else_found:
31 halyavin 1129
        clc
109 heavyiron 1130
        ret
1131
      find_end_if:
31 halyavin 1132
        call    find_structure_end
109 heavyiron 1133
        cmp     ax,if_directive-assembler
1134
        jne     unexpected_instruction
1135
        ret
1136
      find_structure_end:
31 halyavin 1137
        push    [error_line]
109 heavyiron 1138
        mov     eax,[current_line]
1139
        mov     [error_line],eax
1140
      find_end_directive:
31 halyavin 1141
        call    skip_line
109 heavyiron 1142
        lods    byte [esi]
1143
        cmp     al,0Fh
1144
        jne     no_end_directive
1145
        lods    dword [esi]
1146
        mov     [current_line],eax
1147
      skip_labels:
31 halyavin 1148
        cmp     byte [esi],2
109 heavyiron 1149
        jne     labels_ok
1150
        add     esi,6
1151
        jmp     skip_labels
1152
      labels_ok:
31 halyavin 1153
        cmp     byte [esi],1
109 heavyiron 1154
        jne     find_end_directive
1155
        mov     ax,[esi+1]
1156
        cmp     ax,prefix_instruction-assembler
1157
        je      find_end_directive
1158
        add     esi,4
1159
        cmp     ax,repeat_directive-assembler
1160
        je      skip_repeat
1161
        cmp     ax,while_directive-assembler
1162
        je      skip_while
1163
        cmp     ax,if_directive-assembler
1164
        je      skip_if
1165
        cmp     ax,else_directive-assembler
1166
        je      structure_end
1167
        cmp     ax,end_directive-assembler
1168
        jne     find_end_directive
1169
        cmp     byte [esi],1
1170
        jne     find_end_directive
1171
        mov     ax,[esi+1]
1172
        add     esi,4
1173
        cmp     ax,repeat_directive-assembler
1174
        je      structure_end
1175
        cmp     ax,while_directive-assembler
1176
        je      structure_end
1177
        cmp     ax,if_directive-assembler
1178
        jne     find_end_directive
1179
      structure_end:
31 halyavin 1180
        pop     [error_line]
109 heavyiron 1181
        ret
1182
      no_end_directive:
31 halyavin 1183
        mov     eax,[error_line]
109 heavyiron 1184
        mov     [current_line],eax
1185
        jmp     missing_end_directive
1186
      skip_repeat:
31 halyavin 1187
        call    find_end_repeat
109 heavyiron 1188
        jmp     find_end_directive
1189
      skip_while:
31 halyavin 1190
        call    find_end_while
109 heavyiron 1191
        jmp     find_end_directive
1192
      skip_if:
31 halyavin 1193
        call    skip_if_block
109 heavyiron 1194
        jmp     find_end_directive
1195
      skip_if_block:
31 halyavin 1196
        call    find_else
109 heavyiron 1197
        jc      if_block_skipped
1198
        cmp     byte [esi],1
1199
        jne     skip_after_else
1200
        cmp     word [esi+1],if_directive-assembler
1201
        jne     skip_after_else
1202
        add     esi,4
1203
        jmp     skip_if_block
1204
      skip_after_else:
31 halyavin 1205
        call    find_end_if
109 heavyiron 1206
      if_block_skipped:
31 halyavin 1207
        ret
109 heavyiron 1208
end_directive:
31 halyavin 1209
        lods    byte [esi]
109 heavyiron 1210
        cmp     al,1
1211
        jne     invalid_argument
1212
        lods    word [esi]
1213
        inc     esi
1214
        cmp     ax,virtual_directive-assembler
1215
        je      end_virtual
1216
        cmp     ax,repeat_directive-assembler
1217
        je      end_repeat
1218
        cmp     ax,while_directive-assembler
1219
        je      end_while
1220
        cmp     ax,if_directive-assembler
1221
        je      end_if
1222
        cmp     ax,data_directive-assembler
1223
        je      end_data
1224
        jmp     invalid_argument
1225
break_directive:
31 halyavin 1226
        mov     ebx,[structures_buffer]
109 heavyiron 1227
        mov     al,[esi]
1228
        or      al,al
1229
        jz      find_breakable_structure
1230
        cmp     al,0Fh
1231
        jne     extra_characters_on_line
1232
      find_breakable_structure:
31 halyavin 1233
        cmp     ebx,[additional_memory_end]
109 heavyiron 1234
        je      unexpected_instruction
1235
        mov     ax,[ebx]
1236
        cmp     ax,repeat_directive-assembler
1237
        je      break_repeat
1238
        cmp     ax,while_directive-assembler
1239
        je      break_while
1240
        cmp     ax,if_directive-assembler
1241
        je      break_if
1242
        add     ebx,20h
1243
        jmp     find_breakable_structure
1244
      break_if:
31 halyavin 1245
        push    [current_line]
109 heavyiron 1246
        mov     eax,[ebx+4]
1247
        mov     [current_line],eax
1248
        call    remove_structure_data
1249
        call    skip_if_block
1250
        pop     [current_line]
1251
        mov     ebx,[structures_buffer]
1252
        jmp     find_breakable_structure
1253
      break_repeat:
31 halyavin 1254
        push    ebx
109 heavyiron 1255
        call    find_end_repeat
1256
        pop     ebx
1257
        jmp     stop_repeat
1258
      break_while:
31 halyavin 1259
        push    ebx
109 heavyiron 1260
        jmp     stop_while
1261
31 halyavin 1262
 
1263
        call    define_data
109 heavyiron 1264
        lods    byte [esi]
1265
        cmp     al,'('
1266
        je      get_byte
1267
        cmp     al,'?'
1268
        jne     invalid_argument
1269
        mov     eax,edi
1270
        mov     byte [edi],0
1271
        inc     edi
1272
        jmp     undefined_data
1273
      get_byte:
31 halyavin 1274
        cmp     byte [esi],0
109 heavyiron 1275
        je      get_string
1276
        call    get_byte_value
1277
        stos    byte [edi]
1278
        ret
1279
      get_string:
31 halyavin 1280
        inc     esi
109 heavyiron 1281
        lods    dword [esi]
1282
        mov     ecx,eax
1283
        lea     eax,[edi+ecx]
1284
        cmp     eax,[display_buffer]
1285
        ja      out_of_memory
1286
        rep     movs byte [edi],[esi]
1287
        inc     esi
1288
        ret
1289
      undefined_data:
31 halyavin 1290
        cmp     [virtual_data],0
109 heavyiron 1291
        je      mark_undefined_data
1292
        ret
1293
      mark_undefined_data:
31 halyavin 1294
        cmp     eax,[undefined_data_end]
109 heavyiron 1295
        je      undefined_data_ok
1296
        mov     [undefined_data_start],eax
1297
      undefined_data_ok:
31 halyavin 1298
        mov     [undefined_data_end],edi
109 heavyiron 1299
        ret
1300
      define_data:
31 halyavin 1301
        cmp     edi,[display_buffer]
109 heavyiron 1302
        jae     out_of_memory
1303
        cmp     byte [esi],'('
1304
        jne     simple_data_value
1305
        mov     ebx,esi
1306
        inc     esi
1307
        call    skip_expression
1308
        xchg    esi,ebx
1309
        cmp     byte [ebx],81h
1310
        jne     simple_data_value
1311
        inc     esi
1312
        call    get_dword_value
1313
        cmp     [next_pass_needed],0
1314
        jne     dup_value_ok
1315
        cmp     [value_type],0
1316
        jne     invalid_use_of_symbol
1317
      dup_value_ok:
31 halyavin 1318
        inc     esi
109 heavyiron 1319
        cmp     eax,0
1320
        jg      dup_positive
1321
        cmp     [error_line],0
1322
        jne     dup_invalid
1323
        mov     eax,[current_line]
1324
        mov     [error_line],eax
1325
        mov     [error],invalid_value
1326
      dup_invalid:
31 halyavin 1327
        mov     eax,1
109 heavyiron 1328
      dup_positive:
31 halyavin 1329
        cmp     byte [esi],'{'
109 heavyiron 1330
        jne     duplicate_single_data_value
1331
        inc     esi
1332
      duplicate_data:
31 halyavin 1333
        push    eax esi
109 heavyiron 1334
      duplicated_values:
31 halyavin 1335
        cmp     edi,[display_buffer]
109 heavyiron 1336
        jae     out_of_memory
1337
        call    near dword [esp+8]
1338
        lods    byte [esi]
1339
        cmp     al,','
1340
        je      duplicated_values
1341
        cmp     al,'}'
1342
        jne     invalid_argument
1343
        pop     ebx eax
1344
        dec     eax
1345
        jz      data_defined
1346
        mov     esi,ebx
1347
        jmp     duplicate_data
1348
      duplicate_single_data_value:
31 halyavin 1349
        cmp     edi,[display_buffer]
109 heavyiron 1350
        jae     out_of_memory
1351
        push    eax esi
1352
        call    near dword [esp+8]
1353
        pop     ebx eax
1354
        dec     eax
1355
        jz      data_defined
1356
        mov     esi,ebx
1357
        jmp     duplicate_single_data_value
1358
      simple_data_value:
31 halyavin 1359
        cmp     edi,[display_buffer]
109 heavyiron 1360
        jae     out_of_memory
1361
        call    near dword [esp]
1362
      data_defined:
31 halyavin 1363
        lods    byte [esi]
109 heavyiron 1364
        cmp     al,','
1365
        je      define_data
1366
        dec     esi
1367
        add     esp,4
1368
        jmp     instruction_assembled
1369
data_unicode:
31 halyavin 1370
        or      [base_code],-1
109 heavyiron 1371
        jmp     define_words
1372
data_words:
31 halyavin 1373
        mov     [base_code],0
109 heavyiron 1374
      define_words:
31 halyavin 1375
        call    define_data
109 heavyiron 1376
        lods    byte [esi]
1377
        cmp     al,'('
1378
        je      get_word
1379
        cmp     al,'?'
1380
        jne     invalid_argument
1381
        mov     eax,edi
1382
        mov     word [edi],0
1383
        scas    word [edi]
1384
        jmp     undefined_data
1385
        ret
1386
      get_word:
31 halyavin 1387
        cmp     [base_code],0
109 heavyiron 1388
        je      word_data_value
1389
        cmp     byte [esi],0
1390
        je      word_string
1391
      word_data_value:
31 halyavin 1392
        call    get_word_value
109 heavyiron 1393
        call    mark_relocation
1394
        stos    word [edi]
1395
        ret
1396
      word_string:
31 halyavin 1397
        inc     esi
109 heavyiron 1398
        lods    dword [esi]
1399
        mov     ecx,eax
1400
        jecxz   word_string_ok
1401
        lea     eax,[edi+ecx*2]
1402
        cmp     eax,[display_buffer]
1403
        ja      out_of_memory
1404
        xor     ah,ah
1405
      copy_word_string:
31 halyavin 1406
        lods    byte [esi]
109 heavyiron 1407
        stos    word [edi]
1408
        loop    copy_word_string
1409
      word_string_ok:
31 halyavin 1410
        inc     esi
109 heavyiron 1411
        ret
1412
data_dwords:
31 halyavin 1413
        call    define_data
109 heavyiron 1414
        lods    byte [esi]
1415
        cmp     al,'('
1416
        je      get_dword
1417
        cmp     al,'?'
1418
        jne     invalid_argument
1419
        mov     eax,edi
1420
        mov     dword [edi],0
1421
        scas    dword [edi]
1422
        jmp     undefined_data
1423
      get_dword:
31 halyavin 1424
        push    esi
109 heavyiron 1425
        call    get_dword_value
1426
        pop     ebx
1427
        cmp     byte [esi],':'
1428
        je      complex_dword
1429
        call    mark_relocation
1430
        stos    dword [edi]
1431
        ret
1432
      complex_dword:
31 halyavin 1433
        mov     esi,ebx
109 heavyiron 1434
        cmp     byte [esi],'.'
1435
        je      invalid_value
1436
        call    get_word_value
1437
        push    eax
1438
        inc     esi
1439
        lods    byte [esi]
1440
        cmp     al,'('
1441
        jne     invalid_operand
1442
        mov     al,[value_type]
1443
        push    eax
1444
        cmp     byte [esi],'.'
1445
        je      invalid_value
1446
        call    get_word_value
1447
        call    mark_relocation
1448
        stos    word [edi]
1449
        pop     eax
1450
        mov     [value_type],al
1451
        pop     eax
1452
        call    mark_relocation
1453
        stos    word [edi]
1454
        ret
1455
data_pwords:
31 halyavin 1456
        call    define_data
109 heavyiron 1457
        lods    byte [esi]
1458
        cmp     al,'('
1459
        je      get_pword
1460
        cmp     al,'?'
1461
        jne     invalid_argument
1462
        mov     eax,edi
1463
        mov     dword [edi],0
1464
        scas    dword [edi]
1465
        mov     word [edi],0
1466
        scas    word [edi]
1467
        jmp     undefined_data
1468
      get_pword:
31 halyavin 1469
        push    esi
109 heavyiron 1470
        call    get_pword_value
1471
        pop     ebx
1472
        cmp     byte [esi],':'
1473
        je      complex_pword
1474
        call    mark_relocation
1475
        stos    dword [edi]
1476
        mov     ax,dx
1477
        stos    word [edi]
1478
        ret
1479
      complex_pword:
31 halyavin 1480
        mov     esi,ebx
109 heavyiron 1481
        cmp     byte [esi],'.'
1482
        je      invalid_value
1483
        call    get_word_value
1484
        push    eax
1485
        inc     esi
1486
        lods    byte [esi]
1487
        cmp     al,'('
1488
        jne     invalid_operand
1489
        mov     al,[value_type]
1490
        push    eax
1491
        cmp     byte [esi],'.'
1492
        je      invalid_value
1493
        call    get_dword_value
1494
        call    mark_relocation
1495
        stos    dword [edi]
1496
        pop     eax
1497
        mov     [value_type],al
1498
        pop     eax
1499
        call    mark_relocation
1500
        stos    word [edi]
1501
        ret
1502
data_qwords:
31 halyavin 1503
        call    define_data
109 heavyiron 1504
        lods    byte [esi]
1505
        cmp     al,'('
1506
        je      get_qword
1507
        cmp     al,'?'
1508
        jne     invalid_argument
1509
        mov     eax,edi
1510
        mov     dword [edi],0
1511
        scas    dword [edi]
1512
        mov     dword [edi],0
1513
        scas    dword [edi]
1514
        jmp     undefined_data
1515
      get_qword:
31 halyavin 1516
        call    get_qword_value
109 heavyiron 1517
        call    mark_relocation
1518
        stos    dword [edi]
1519
        mov     eax,edx
1520
        stos    dword [edi]
1521
        ret
1522
data_twords:
31 halyavin 1523
        call    define_data
109 heavyiron 1524
        lods    byte [esi]
1525
        cmp     al,'('
1526
        je      get_tword
1527
        cmp     al,'?'
1528
        jne     invalid_argument
1529
        mov     eax,edi
1530
        mov     dword [edi],0
1531
        scas    dword [edi]
1532
        mov     dword [edi],0
1533
        scas    dword [edi]
1534
        mov     word [edi],0
1535
        scas    word [edi]
1536
        jmp     undefined_data
1537
      get_tword:
31 halyavin 1538
        cmp     byte [esi],'.'
109 heavyiron 1539
        jne     complex_tword
1540
        inc     esi
1541
        cmp     word [esi+8],8000h
1542
        je      fp_zero_tword
1543
        mov     eax,[esi]
1544
        stos    dword [edi]
1545
        mov     eax,[esi+4]
1546
        stos    dword [edi]
1547
        mov     ax,[esi+8]
1548
        add     ax,3FFFh
1549
        cmp     ax,8000h
1550
        jae     value_out_of_range
1551
        mov     bl,[esi+11]
1552
        shl     bx,15
1553
        or      ax,bx
1554
        stos    word [edi]
1555
        add     esi,13
1556
        ret
1557
      fp_zero_tword:
31 halyavin 1558
        xor     eax,eax
109 heavyiron 1559
        stos    dword [edi]
1560
        stos    dword [edi]
1561
        mov     al,[esi+11]
1562
        shl     ax,15
1563
        stos    word [edi]
1564
        add     esi,13
1565
        ret
1566
      complex_tword:
31 halyavin 1567
        call    get_word_value
109 heavyiron 1568
        push    eax
1569
        inc     esi
1570
        lods    byte [esi]
1571
        cmp     al,'('
1572
        jne     invalid_operand
1573
        mov     al,[value_type]
1574
        push    eax
1575
        cmp     byte [esi],'.'
1576
        je      invalid_value
1577
        call    get_qword_value
1578
        call    mark_relocation
1579
        stos    dword [edi]
1580
        mov     eax,edx
1581
        stos    dword [edi]
1582
        pop     eax
1583
        mov     [value_type],al
1584
        pop     eax
1585
        call    mark_relocation
1586
        stos    word [edi]
1587
        ret
1588
data_file:
31 halyavin 1589
        lods    word [esi]
109 heavyiron 1590
        cmp     ax,'('
1591
        jne     invalid_argument
1592
        add     esi,4
1593
        call    open_binary_file
1594
        mov     eax,[esi-4]
1595
        lea     esi,[esi+eax+1]
1596
        mov     al,2
1597
        xor     edx,edx
1598
        call    lseek
1599
        push    eax
1600
        xor     edx,edx
1601
        cmp     byte [esi],':'
1602
        jne     position_ok
1603
        inc     esi
1604
        cmp     byte [esi],'('
1605
        jne     invalid_argument
1606
        inc     esi
1607
        cmp     byte [esi],'.'
1608
        je      invalid_value
1609
        push    ebx
1610
        call    get_dword_value
1611
        pop     ebx
1612
        mov     edx,eax
1613
        sub     [esp],edx
1614
      position_ok:
31 halyavin 1615
        cmp     byte [esi],','
109 heavyiron 1616
        jne     size_ok
1617
        inc     esi
1618
        cmp     byte [esi],'('
1619
        jne     invalid_argument
1620
        inc     esi
1621
        cmp     byte [esi],'.'
1622
        je      invalid_value
1623
        push    ebx edx
1624
        call    get_dword_value
1625
        pop     edx ebx
1626
        mov     [esp],eax
1627
      size_ok:
31 halyavin 1628
        xor     al,al
109 heavyiron 1629
        call    lseek
1630
        pop     ecx
1631
        mov     edx,edi
1632
        add     edi,ecx
1633
        jc      out_of_memory
1634
        cmp     edi,[display_buffer]
1635
        ja      out_of_memory
1636
        call    read
1637
        jc      error_reading_file
1638
        call    close
1639
        lods    byte [esi]
1640
        cmp     al,','
1641
        je      data_file
1642
        dec     esi
1643
        jmp     instruction_assembled
1644
      open_binary_file:
31 halyavin 1645
        push    esi
109 heavyiron 1646
        push    edi
1647
        mov     esi,[current_line]
1648
        mov     esi,[esi]
1649
      get_current_path:
31 halyavin 1650
        lodsb
109 heavyiron 1651
        stosb
1652
        or      al,al
1653
        jnz     get_current_path
1654
      cut_current_path:
31 halyavin 1655
        cmp     edi,[esp]
109 heavyiron 1656
        je      current_path_ok
1657
        cmp     byte [edi-1],'\'
1658
        je      current_path_ok
1659
        cmp     byte [edi-1],'/'
1660
        je      current_path_ok
1661
        dec     edi
1662
        jmp     cut_current_path
1663
      current_path_ok:
31 halyavin 1664
        mov     esi,[esp+4]
109 heavyiron 1665
        call    preprocess_path
1666
        pop     edx
1667
        mov     esi,edx
1668
        call    open
1669
        jnc     file_opened
1670
        mov     edi,esi
1671
        mov     esi,[esp]
1672
        push    edi
1673
        call    preprocess_path
1674
        pop     edx
1675
        mov     esi,edx
1676
        call    open
1677
        jc      file_not_found
1678
      file_opened:
31 halyavin 1679
        mov     edi,esi
109 heavyiron 1680
        pop     esi
1681
        ret
1682
reserve_bytes:
31 halyavin 1683
        lods    byte [esi]
109 heavyiron 1684
        cmp     al,'('
1685
        jne     invalid_argument
1686
        cmp     byte [esi],'.'
1687
        je      invalid_value
1688
        call    get_dword_value
1689
        cmp     [next_pass_needed],0
1690
        jne     rb_value_ok
1691
        cmp     [value_type],0
1692
        jne     invalid_use_of_symbol
1693
      rb_value_ok:
31 halyavin 1694
        cmp     eax,0
109 heavyiron 1695
        jl      reserve_negative
1696
        mov     ecx,eax
1697
        mov     edx,ecx
1698
        add     edx,edi
1699
        jc      out_of_memory
1700
        cmp     edx,[display_buffer]
1701
        ja      out_of_memory
1702
        push    edi
1703
        cmp     [next_pass_needed],0
1704
        je      zero_bytes
1705
        add     edi,ecx
1706
        jmp     reserved_data
1707
      zero_bytes:
31 halyavin 1708
        xor     eax,eax
109 heavyiron 1709
        shr     ecx,1
1710
        jnc     bytes_stosb_ok
1711
        stos    byte [edi]
1712
      bytes_stosb_ok:
31 halyavin 1713
        shr     ecx,1
109 heavyiron 1714
        jnc     bytes_stosw_ok
1715
        stos    word [edi]
1716
      bytes_stosw_ok:
31 halyavin 1717
        rep     stos dword [edi]
109 heavyiron 1718
      reserved_data:
31 halyavin 1719
        pop     eax
109 heavyiron 1720
        call    undefined_data
1721
        jmp     instruction_assembled
1722
      reserve_negative:
31 halyavin 1723
        cmp     [error_line],0
109 heavyiron 1724
        jne     instruction_assembled
1725
        mov     eax,[current_line]
1726
        mov     [error_line],eax
1727
        mov     [error],invalid_value
1728
        jmp     instruction_assembled
1729
reserve_words:
31 halyavin 1730
        lods    byte [esi]
109 heavyiron 1731
        cmp     al,'('
1732
        jne     invalid_argument
1733
        cmp     byte [esi],'.'
1734
        je      invalid_value
1735
        call    get_dword_value
1736
        cmp     [next_pass_needed],0
1737
        jne     rw_value_ok
1738
        cmp     [value_type],0
1739
        jne     invalid_use_of_symbol
1740
      rw_value_ok:
31 halyavin 1741
        cmp     eax,0
109 heavyiron 1742
        jl      reserve_negative
1743
        mov     ecx,eax
1744
        mov     edx,ecx
1745
        shl     edx,1
1746
        jc      out_of_memory
1747
        add     edx,edi
1748
        jc      out_of_memory
1749
        cmp     edx,[display_buffer]
1750
        ja      out_of_memory
1751
        push    edi
1752
        cmp     [next_pass_needed],0
1753
        je      zero_words
1754
        lea     edi,[edi+ecx*2]
1755
        jmp     reserved_data
1756
      zero_words:
31 halyavin 1757
        xor     eax,eax
109 heavyiron 1758
        shr     ecx,1
1759
        jnc     words_stosw_ok
1760
        stos    word [edi]
1761
      words_stosw_ok:
31 halyavin 1762
        rep     stos dword [edi]
109 heavyiron 1763
        jmp     reserved_data
1764
reserve_dwords:
31 halyavin 1765
        lods    byte [esi]
109 heavyiron 1766
        cmp     al,'('
1767
        jne     invalid_argument
1768
        cmp     byte [esi],'.'
1769
        je      invalid_value
1770
        call    get_dword_value
1771
        cmp     [next_pass_needed],0
1772
        jne     rd_value_ok
1773
        cmp     [value_type],0
1774
        jne     invalid_use_of_symbol
1775
      rd_value_ok:
31 halyavin 1776
        cmp     eax,0
109 heavyiron 1777
        jl      reserve_negative
1778
        mov     ecx,eax
1779
        mov     edx,ecx
1780
        shl     edx,1
1781
        jc      out_of_memory
1782
        shl     edx,1
1783
        jc      out_of_memory
1784
        add     edx,edi
1785
        jc      out_of_memory
1786
        cmp     edx,[display_buffer]
1787
        ja      out_of_memory
1788
        push    edi
1789
        cmp     [next_pass_needed],0
1790
        je      zero_dwords
1791
        lea     edi,[edi+ecx*4]
1792
        jmp     reserved_data
1793
      zero_dwords:
31 halyavin 1794
        xor     eax,eax
109 heavyiron 1795
        rep     stos dword [edi]
1796
        jmp     reserved_data
1797
reserve_pwords:
31 halyavin 1798
        lods    byte [esi]
109 heavyiron 1799
        cmp     al,'('
1800
        jne     invalid_argument
1801
        cmp     byte [esi],'.'
1802
        je      invalid_value
1803
        call    get_dword_value
1804
        cmp     [next_pass_needed],0
1805
        jne     rp_value_ok
1806
        cmp     [value_type],0
1807
        jne     invalid_use_of_symbol
1808
      rp_value_ok:
31 halyavin 1809
        cmp     eax,0
109 heavyiron 1810
        jl      reserve_negative
1811
        mov     ecx,eax
1812
        shl     ecx,1
1813
        jc      out_of_memory
1814
        add     ecx,eax
1815
        mov     edx,ecx
1816
        shl     edx,1
1817
        jc      out_of_memory
1818
        add     edx,edi
1819
        jc      out_of_memory
1820
        cmp     edx,[display_buffer]
1821
        ja      out_of_memory
1822
        push    edi
1823
        cmp     [next_pass_needed],0
1824
        je      zero_words
1825
        lea     edi,[edi+ecx*2]
1826
        jmp     reserved_data
1827
reserve_qwords:
31 halyavin 1828
        lods    byte [esi]
109 heavyiron 1829
        cmp     al,'('
1830
        jne     invalid_argument
1831
        cmp     byte [esi],'.'
1832
        je      invalid_value
1833
        call    get_dword_value
1834
        cmp     [next_pass_needed],0
1835
        jne     rq_value_ok
1836
        cmp     [value_type],0
1837
        jne     invalid_use_of_symbol
1838
      rq_value_ok:
31 halyavin 1839
        cmp     eax,0
109 heavyiron 1840
        jl      reserve_negative
1841
        mov     ecx,eax
1842
        shl     ecx,1
1843
        jc      out_of_memory
1844
        mov     edx,ecx
1845
        shl     edx,1
1846
        jc      out_of_memory
1847
        shl     edx,1
1848
        jc      out_of_memory
1849
        add     edx,edi
1850
        jc      out_of_memory
1851
        cmp     edx,[display_buffer]
1852
        ja      out_of_memory
1853
        push    edi
1854
        cmp     [next_pass_needed],0
1855
        je      zero_dwords
1856
        lea     edi,[edi+ecx*4]
1857
        jmp     reserved_data
1858
reserve_twords:
31 halyavin 1859
        lods    byte [esi]
109 heavyiron 1860
        cmp     al,'('
1861
        jne     invalid_argument
1862
        cmp     byte [esi],'.'
1863
        je      invalid_value
1864
        call    get_dword_value
1865
        cmp     [next_pass_needed],0
1866
        jne     rt_value_ok
1867
        cmp     [value_type],0
1868
        jne     invalid_use_of_symbol
1869
      rt_value_ok:
31 halyavin 1870
        cmp     eax,0
109 heavyiron 1871
        jl      reserve_negative
1872
        mov     ecx,eax
1873
        shl     ecx,2
1874
        jc      out_of_memory
1875
        add     ecx,eax
1876
        mov     edx,ecx
1877
        shl     edx,1
1878
        jc      out_of_memory
1879
        add     edx,edi
1880
        jc      out_of_memory
1881
        cmp     edx,[display_buffer]
1882
        ja      out_of_memory
1883
        push    edi
1884
        cmp     [next_pass_needed],0
1885
        je      zero_words
1886
        lea     edi,[edi+ecx*2]
1887
        jmp     reserved_data
1888
align_directive:
31 halyavin 1889
        lods    byte [esi]
109 heavyiron 1890
        cmp     al,'('
1891
        jne     invalid_argument
1892
        cmp     byte [esi],'.'
1893
        je      invalid_value
1894
        call    get_dword_value
1895
        cmp     [value_type],0
1896
        jne     invalid_use_of_symbol
1897
        mov     edx,eax
1898
        dec     edx
1899
        test    eax,edx
1900
        jnz     negative_times
1901
        or      eax,eax
1902
        jz      negative_times
1903
        cmp     eax,1
1904
        je      instruction_assembled
1905
        mov     ecx,edi
1906
        sub     ecx,dword [org_origin]
1907
        cmp     [org_registers],0
1908
        jne     section_not_aligned_enough
1909
        cmp     [labels_type],0
1910
        je      make_alignment
1911
        cmp     [output_format],3
1912
        je      pe_alignment
1913
        mov     ebx,[org_symbol]
1914
        cmp     byte [ebx],0
1915
        jne     section_not_aligned_enough
1916
        cmp     eax,[ebx+10h]
1917
        jbe     make_alignment
1918
        jmp     section_not_aligned_enough
1919
      pe_alignment:
31 halyavin 1920
        cmp     eax,1000h
109 heavyiron 1921
        ja      section_not_aligned_enough
1922
      make_alignment:
31 halyavin 1923
        dec     eax
109 heavyiron 1924
        and     ecx,eax
1925
        jz      instruction_assembled
1926
        neg     ecx
1927
        add     ecx,eax
1928
        inc     ecx
1929
        mov     edx,ecx
1930
        add     edx,edi
1931
        jc      out_of_memory
1932
        cmp     edx,[display_buffer]
1933
        ja      out_of_memory
1934
        push    edi
1935
        cmp     [next_pass_needed],0
1936
        je      nops
1937
        add     edi,ecx
1938
        jmp     reserved_data
1939
      nops:
31 halyavin 1940
        mov     eax,90909090h
109 heavyiron 1941
        shr     ecx,1
1942
        jnc     nops_stosb_ok
1943
        stos    byte [edi]
1944
      nops_stosb_ok:
31 halyavin 1945
        shr     ecx,1
109 heavyiron 1946
        jnc     nops_stosw_ok
1947
        stos    word [edi]
1948
      nops_stosw_ok:
31 halyavin 1949
        rep     stos dword [edi]
109 heavyiron 1950
        jmp     reserved_data
1951