Subversion Repositories Kolibri OS

Rev

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