Subversion Repositories Kolibri OS

Rev

Rev 692 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. ; flat assembler core
  3. ; Copyright (c) 1999-2007, Tomasz Grysztar.
  4. ; All rights reserved.
  5.  
  6. formatter:
  7.         cmp     [output_file],0
  8.         jne     output_path_ok
  9.         push    edi
  10.         mov     esi,[input_file]
  11.         mov     edi,[free_additional_memory]
  12.       copy_output_path:
  13.         lods    byte [esi]
  14.         cmp     edi,[structures_buffer]
  15.         jae     out_of_memory
  16.         stos    byte [edi]
  17.         or      al,al
  18.         jnz     copy_output_path
  19.         dec     edi
  20.         mov     eax,edi
  21.       find_extension:
  22.         dec     eax
  23.         cmp     eax,[free_additional_memory]
  24.         jb      extension_found
  25.         cmp     byte [eax],'\'
  26.         je      extension_found
  27.         cmp     byte [eax],'/'
  28.         je      extension_found
  29.         cmp     byte [eax],'.'
  30.         jne     find_extension
  31.         mov     edi,eax
  32.       extension_found:
  33.         lea     eax,[edi+9]
  34.         cmp     eax,[structures_buffer]
  35.         jae     out_of_memory
  36.         cmp     [file_extension],0
  37.         jne     extension_specified
  38.         cmp     [output_format],2
  39.         je      exe_extension
  40.         jb      bin_extension
  41.         cmp     [output_format],4
  42.         je      obj_extension
  43.         cmp     [output_format],5
  44.         je      o_extension
  45.         cmp     [output_format],3
  46.         jne     no_extension
  47.         cmp     [subsystem],1
  48.         je      sys_extension
  49.         bt      [format_flags],8
  50.         jnc     exe_extension
  51.         mov     eax,'.dll'
  52.         jmp     make_extension
  53.       sys_extension:
  54.         mov     eax,'.sys'
  55.         jmp     make_extension
  56.       bin_extension:
  57.         mov     eax,'.bin'
  58.         bt      [format_flags],0
  59.         jnc     make_extension
  60.         mov     eax,'.com'
  61.         jmp     make_extension
  62.       obj_extension:
  63.         mov     eax,'.obj'
  64.         jmp     make_extension
  65.       o_extension:
  66.         mov     eax,'.o'
  67.         bt      [format_flags],0
  68.         jnc     make_extension
  69.       no_extension:
  70.         xor     eax,eax
  71.         jmp     make_extension
  72.       exe_extension:
  73.         mov     eax,'.exe'
  74.       make_extension:
  75.         xchg    eax,[edi]
  76.         scas    dword [edi]
  77.         mov     byte [edi],0
  78.         scas    byte [edi]
  79.         mov     esi,edi
  80.         stos    dword [edi]
  81.         sub     edi,9
  82.         xor     eax,eax
  83.         mov     ebx,characters
  84.       adapt_case:
  85.         mov     al,[esi]
  86.         or      al,al
  87.         jz      adapt_next
  88.         xlat    byte [ebx]
  89.         cmp     al,[esi]
  90.         je      adapt_ok
  91.         sub     byte [edi],20h
  92.       adapt_ok:
  93.         inc     esi
  94.       adapt_next:
  95.         inc     edi
  96.         cmp     byte [edi],0
  97.         jne     adapt_case
  98.         jmp     extension_ok
  99.       extension_specified:
  100.         mov     al,'.'
  101.         stos    byte [edi]
  102.         mov     esi,[file_extension]
  103.       copy_extension:
  104.         lods    byte [esi]
  105.         stos    byte [edi]
  106.         test    al,al
  107.         jnz     copy_extension
  108.         dec     edi
  109.       extension_ok:
  110.         mov     esi,edi
  111.         lea     ecx,[esi+1]
  112.         sub     ecx,[free_additional_memory]
  113.         mov     edi,[structures_buffer]
  114.         dec     edi
  115.         std
  116.         rep     movs byte [edi],[esi]
  117.         cld
  118.         inc     edi
  119.         mov     [structures_buffer],edi
  120.         mov     [output_file],edi
  121.         pop     edi
  122.       output_path_ok:
  123.         cmp     [output_format],4
  124.         je      coff_formatter
  125.         cmp     [output_format],5
  126.         jne     common_formatter
  127.         bt      [format_flags],0
  128.         jnc     elf_formatter
  129.       common_formatter:
  130.         mov     eax,edi
  131.         sub     eax,[code_start]
  132.         mov     [real_code_size],eax
  133.         cmp     edi,[undefined_data_end]
  134.         jne     calculate_code_size
  135.         mov     edi,[undefined_data_start]
  136.       calculate_code_size:
  137.         sub     edi,[code_start]
  138.         mov     [code_size],edi
  139.         mov     [written_size],0
  140.         mov     edx,[output_file]
  141.         call    create
  142.         jc      write_failed
  143.         cmp     [output_format],3
  144.         jne     stub_written
  145.         mov     edx,[code_start]
  146.         mov     ecx,[stub_size]
  147.         sub     edx,ecx
  148.         add     [written_size],ecx
  149.         call    write
  150.       stub_written:
  151.         cmp     [output_format],2
  152.         jne     write_output
  153.         call    write_mz_header
  154.       write_output:
  155.         call    write_code
  156.       output_written:
  157.         call    close
  158.         ret
  159.       write_code:
  160.         mov     eax,[written_size]
  161.         mov     [headers_size],eax
  162.         mov     edx,[code_start]
  163.         mov     ecx,[code_size]
  164.         add     [written_size],ecx
  165.         call    write
  166.         jc      write_failed
  167.         ret
  168. format_directive:
  169.         cmp     edi,[code_start]
  170.         jne     unexpected_instruction
  171.         cmp     [virtual_data],0
  172.         jne     unexpected_instruction
  173.         cmp     [output_format],0
  174.         jne     unexpected_instruction
  175.         lods    byte [esi]
  176.         cmp     al,17h
  177.         je      format_prefix
  178.         cmp     al,18h
  179.         jne     invalid_argument
  180.         lods    byte [esi]
  181.       select_format:
  182.         mov     dl,al
  183.         shr     al,4
  184.         mov     [output_format],al
  185.         and     edx,0Fh
  186.         or      [format_flags],edx
  187.         cmp     al,2
  188.         je      format_mz
  189.         cmp     al,3
  190.         je      format_pe
  191.         cmp     al,4
  192.         je      format_coff
  193.         cmp     al,5
  194.         je      format_elf
  195.       format_defined:
  196.         cmp     byte [esi],86h
  197.         jne     instruction_assembled
  198.         cmp     word [esi+1],'('
  199.         jne     invalid_argument
  200.         mov     eax,[esi+3]
  201.         add     esi,3+4
  202.         mov     [file_extension],esi
  203.         lea     esi,[esi+eax+1]
  204.         jmp     instruction_assembled
  205.       format_prefix:
  206.         lods    byte [esi]
  207.         mov     ah,al
  208.         lods    byte [esi]
  209.         cmp     al,18h
  210.         jne     invalid_argument
  211.         lods    byte [esi]
  212.         mov     edx,eax
  213.         shr     dl,4
  214.         shr     dh,4
  215.         cmp     dl,dh
  216.         jne     invalid_argument
  217.         or      al,ah
  218.         jmp     select_format
  219. entry_directive:
  220.         bts     [format_flags],10h
  221.         jc      setting_already_specified
  222.         mov     al,[output_format]
  223.         cmp     al,2
  224.         je      mz_entry
  225.         cmp     al,3
  226.         je      pe_entry
  227.         cmp     al,5
  228.         jne     illegal_instruction
  229.         bt      [format_flags],0
  230.         jc      elf_entry
  231.         jmp     illegal_instruction
  232. stack_directive:
  233.         bts     [format_flags],11h
  234.         jc      setting_already_specified
  235.         mov     al,[output_format]
  236.         cmp     al,2
  237.         je      mz_stack
  238.         cmp     al,3
  239.         je      pe_stack
  240.         jmp     illegal_instruction
  241. heap_directive:
  242.         bts     [format_flags],12h
  243.         jc      setting_already_specified
  244.         mov     al,[output_format]
  245.         cmp     al,2
  246.         je      mz_heap
  247.         cmp     al,3
  248.         je      pe_heap
  249.         jmp     illegal_instruction
  250. segment_directive:
  251.         cmp     [virtual_data],0
  252.         jne     illegal_instruction
  253.         mov     al,[output_format]
  254.         cmp     al,2
  255.         je      mz_segment
  256.         cmp     al,5
  257.         je      elf_segment
  258.         jmp     illegal_instruction
  259. section_directive:
  260.         cmp     [virtual_data],0
  261.         jne     illegal_instruction
  262.         mov     al,[output_format]
  263.         cmp     al,3
  264.         je      pe_section
  265.         cmp     al,4
  266.         je      coff_section
  267.         cmp     al,5
  268.         je      elf_section
  269.         jmp     illegal_instruction
  270. public_directive:
  271.         mov     al,[output_format]
  272.         cmp     al,4
  273.         je      public_allowed
  274.         cmp     al,5
  275.         jne     illegal_instruction
  276.         bt      [format_flags],0
  277.         jc      illegal_instruction
  278.       public_allowed:
  279.         mov     [base_code],0C0h
  280.         lods    byte [esi]
  281.         cmp     al,2
  282.         je      public_label
  283.         cmp     al,1Dh
  284.         jne     invalid_argument
  285.         lods    byte [esi]
  286.         and     al,7
  287.         add     [base_code],al
  288.         lods    byte [esi]
  289.         cmp     al,2
  290.         jne     invalid_argument
  291.       public_label:
  292.         lods    dword [esi]
  293.         cmp     eax,0Fh
  294.         jb      invalid_use_of_symbol
  295.         je      reserved_word_used_as_symbol
  296.         mov     dx,[current_pass]
  297.         mov     [eax+18],dx
  298.         or      byte [eax+8],8
  299.         inc     esi
  300.         mov     ebx,[free_additional_memory]
  301.         lea     edx,[ebx+10h]
  302.         cmp     edx,[structures_buffer]
  303.         jae     out_of_memory
  304.         mov     [free_additional_memory],edx
  305.         mov     [ebx+8],eax
  306.         mov     eax,[current_line]
  307.         mov     [ebx+0Ch],eax
  308.         lods    byte [esi]
  309.         cmp     al,86h
  310.         jne     invalid_argument
  311.         lods    word [esi]
  312.         cmp     ax,'('
  313.         jne     invalid_argument
  314.         mov     [ebx+4],esi
  315.         lods    dword [esi]
  316.         lea     esi,[esi+eax+1]
  317.         mov     al,[base_code]
  318.         mov     [ebx],al
  319.         jmp     instruction_assembled
  320. extrn_directive:
  321.         mov     al,[output_format]
  322.         cmp     al,4
  323.         je      extrn_allowed
  324.         cmp     al,5
  325.         jne     illegal_instruction
  326.         bt      [format_flags],0
  327.         jc      illegal_instruction
  328.       extrn_allowed:
  329.         lods    word [esi]
  330.         cmp     ax,'('
  331.         jne     invalid_argument
  332.         mov     ebx,esi
  333.         lods    dword [esi]
  334.         lea     esi,[esi+eax+1]
  335.         mov     edx,[free_additional_memory]
  336.         lea     eax,[edx+0Ch]
  337.         cmp     eax,[structures_buffer]
  338.         jae     out_of_memory
  339.         mov     [free_additional_memory],eax
  340.         mov     byte [edx],80h
  341.         mov     [edx+4],ebx
  342.         lods    byte [esi]
  343.         cmp     al,86h
  344.         jne     invalid_argument
  345.         lods    byte [esi]
  346.         cmp     al,2
  347.         jne     invalid_argument
  348.         lods    dword [esi]
  349.         cmp     eax,0Fh
  350.         jb      invalid_use_of_symbol
  351.         je      reserved_word_used_as_symbol
  352.         inc     esi
  353.         mov     ebx,eax
  354.         xor     ah,ah
  355.         lods    byte [esi]
  356.         cmp     al,':'
  357.         je      get_extrn_size
  358.         dec     esi
  359.         cmp     al,11h
  360.         jne     extrn_size_ok
  361.       get_extrn_size:
  362.         lods    word [esi]
  363.         cmp     al,11h
  364.         jne     invalid_argument
  365.       extrn_size_ok:
  366.         mov     [address_symbol],edx
  367.         movzx   ecx,ah
  368.         mov     [edx+8],ecx
  369.         xor     eax,eax
  370.         xor     edx,edx
  371.         xor     ebp,ebp
  372.         mov     ch,2
  373.         test    [format_flags],8
  374.         jz      make_free_label
  375.         mov     ch,4
  376.         jmp     make_free_label
  377. mark_relocation:
  378.         cmp     [value_type],0
  379.         je      relocation_ok
  380.         cmp     [virtual_data],0
  381.         jne     relocation_ok
  382.         cmp     [output_format],2
  383.         je      mark_mz_relocation
  384.         cmp     [output_format],3
  385.         je      mark_pe_relocation
  386.         cmp     [output_format],4
  387.         je      mark_coff_relocation
  388.         cmp     [output_format],5
  389.         je      mark_elf_relocation
  390.       relocation_ok:
  391.         ret
  392. close_pass:
  393.         mov     al,[output_format]
  394.         cmp     al,3
  395.         je      close_pe
  396.         cmp     al,4
  397.         je      close_coff
  398.         cmp     al,5
  399.         je      close_elf
  400.         ret
  401.  
  402. format_mz:
  403.         mov     edx,[additional_memory]
  404.         push    edi
  405.         mov     edi,edx
  406.         mov     ecx,1Ch shr 2
  407.         xor     eax,eax
  408.         rep     stos dword [edi]
  409.         mov     [free_additional_memory],edi
  410.         pop     edi
  411.         mov     word [edx+0Ch],0FFFFh
  412.         mov     word [edx+10h],1000h
  413.         mov     [code_type],16
  414.         jmp     format_defined
  415. mark_mz_relocation:
  416.         push    eax ebx
  417.         inc     [number_of_relocations]
  418.         mov     ebx,[free_additional_memory]
  419.         mov     eax,edi
  420.         sub     eax,[code_start]
  421.         mov     [ebx],ax
  422.         shr     eax,16
  423.         shl     ax,12
  424.         mov     [ebx+2],ax
  425.         cmp     word [ebx],0FFFFh
  426.         jne     mz_relocation_ok
  427.         inc     word [ebx+2]
  428.         sub     word [ebx],10h
  429.       mz_relocation_ok:
  430.         add     ebx,4
  431.         cmp     ebx,[structures_buffer]
  432.         jae     out_of_memory
  433.         mov     [free_additional_memory],ebx
  434.         pop     ebx eax
  435.         ret
  436. mz_segment:
  437.         lods    byte [esi]
  438.         cmp     al,2
  439.         jne     invalid_argument
  440.         lods    dword [esi]
  441.         cmp     eax,0Fh
  442.         jb      invalid_use_of_symbol
  443.         je      reserved_word_used_as_symbol
  444.         inc     esi
  445.         mov     ebx,eax
  446.         mov     eax,edi
  447.         sub     eax,[code_start]
  448.         mov     ecx,0Fh
  449.         add     eax,0Fh
  450.         and     eax,1111b
  451.         sub     ecx,eax
  452.         mov     edx,edi
  453.         xor     al,al
  454.         rep     stos byte [edi]
  455.         mov     dword [org_origin],edi
  456.         mov     dword [org_origin+4],0
  457.         mov     [org_registers],0
  458.         mov     [org_start],edi
  459.         mov     eax,edx
  460.         call    undefined_data
  461.         mov     eax,edi
  462.         sub     eax,[code_start]
  463.         shr     eax,4
  464.         cmp     eax,10000h
  465.         jae     value_out_of_range
  466.         mov     edx,eax
  467.         mov     al,16
  468.         cmp     byte [esi],13h
  469.         jne     segment_type_ok
  470.         inc     esi
  471.         lods    byte [esi]
  472.       segment_type_ok:
  473.         mov     [code_type],al
  474.         mov     eax,edx
  475.         mov     cx,0100h
  476.         xor     edx,edx
  477.         xor     ebp,ebp
  478.         mov     [address_symbol],edx
  479.         jmp     make_free_label
  480. mz_entry:
  481.         lods    byte [esi]
  482.         cmp     al,'('
  483.         jne     invalid_argument
  484.         call    get_word_value
  485.         cmp     [value_type],1
  486.         je      initial_cs_ok
  487.         cmp     [error_line],0
  488.         jne     initial_cs_ok
  489.         mov     eax,[current_line]
  490.         mov     [error_line],eax
  491.         mov     [error],invalid_address
  492.       initial_cs_ok:
  493.         mov     edx,[additional_memory]
  494.         mov     [edx+16h],ax
  495.         lods    byte [esi]
  496.         cmp     al,':'
  497.         jne     invalid_argument
  498.         lods    byte [esi]
  499.         cmp     al,'('
  500.         jne     invalid_argument
  501.         ja      invalid_address
  502.         call    get_word_value
  503.         cmp     [value_type],0
  504.         jne     invalid_use_of_symbol
  505.         mov     edx,[additional_memory]
  506.         mov     [edx+14h],ax
  507.         jmp     instruction_assembled
  508. mz_stack:
  509.         lods    byte [esi]
  510.         cmp     al,'('
  511.         jne     invalid_argument
  512.         call    get_word_value
  513.         cmp     byte [esi],':'
  514.         je      stack_pointer
  515.         cmp     ax,10h
  516.         jb      invalid_value
  517.         cmp     [value_type],0
  518.         jne     invalid_use_of_symbol
  519.         mov     edx,[additional_memory]
  520.         mov     [edx+10h],ax
  521.         jmp     instruction_assembled
  522.       stack_pointer:
  523.         cmp     [value_type],1
  524.         je      initial_ss_ok
  525.         cmp     [error_line],0
  526.         jne     initial_ss_ok
  527.         mov     eax,[current_line]
  528.         mov     [error_line],eax
  529.         mov     [error],invalid_address
  530.       initial_ss_ok:
  531.         mov     edx,[additional_memory]
  532.         mov     [edx+0Eh],ax
  533.         lods    byte [esi]
  534.         cmp     al,':'
  535.         jne     invalid_argument
  536.         lods    byte [esi]
  537.         cmp     al,'('
  538.         jne     invalid_argument
  539.         call    get_word_value
  540.         cmp     [value_type],0
  541.         jne     invalid_use_of_symbol
  542.         mov     edx,[additional_memory]
  543.         mov     [edx+10h],ax
  544.         bts     [format_flags],4
  545.         jmp     instruction_assembled
  546. mz_heap:
  547.         cmp     [output_format],2
  548.         jne     illegal_instruction
  549.         lods    byte [esi]
  550.         call    get_size_operator
  551.         cmp     ah,1
  552.         je      invalid_value
  553.         cmp     ah,2
  554.         ja      invalid_value
  555.         cmp     al,'('
  556.         jne     invalid_argument
  557.         call    get_word_value
  558.         cmp     [value_type],0
  559.         jne     invalid_use_of_symbol
  560.         mov     edx,[additional_memory]
  561.         mov     [edx+0Ch],ax
  562.         jmp     instruction_assembled
  563. write_mz_header:
  564.         mov     edx,[additional_memory]
  565.         bt      [format_flags],4
  566.         jc      mz_stack_ok
  567.         mov     eax,[real_code_size]
  568.         dec     eax
  569.         shr     eax,4
  570.         inc     eax
  571.         mov     [edx+0Eh],ax
  572.         shl     eax,4
  573.         movzx   ecx,word [edx+10h]
  574.         add     eax,ecx
  575.         mov     [real_code_size],eax
  576.       mz_stack_ok:
  577.         mov     edi,[free_additional_memory]
  578.         mov     eax,[number_of_relocations]
  579.         shl     eax,2
  580.         add     eax,1Ch
  581.         sub     edi,eax
  582.         xchg    edi,[free_additional_memory]
  583.         mov     ecx,0Fh
  584.         add     eax,0Fh
  585.         and     eax,1111b
  586.         sub     ecx,eax
  587.         xor     al,al
  588.         rep     stos byte [edi]
  589.         sub     edi,[free_additional_memory]
  590.         mov     ecx,edi
  591.         shr     edi,4
  592.         mov     word [edx],'MZ'         ; signature
  593.         mov     [edx+8],di              ; header size in paragraphs
  594.         mov     eax,[number_of_relocations]
  595.         mov     [edx+6],ax              ; number of relocation entries
  596.         mov     eax,[code_size]
  597.         add     eax,ecx
  598.         mov     esi,eax
  599.         shr     esi,9
  600.         and     eax,1FFh
  601.         inc     si
  602.         or      ax,ax
  603.         jnz     mz_size_ok
  604.         dec     si
  605.       mz_size_ok:
  606.         mov     [edx+2],ax              ; number of bytes in last page
  607.         mov     [edx+4],si              ; number of pages
  608.         mov     eax,[real_code_size]
  609.         dec     eax
  610.         shr     eax,4
  611.         inc     eax
  612.         mov     esi,[code_size]
  613.         dec     esi
  614.         shr     esi,4
  615.         inc     esi
  616.         sub     eax,esi
  617.         mov     [edx+0Ah],ax            ; minimum memory in addition to code
  618.         add     [edx+0Ch],ax            ; maximum memory in addition to code
  619.         salc
  620.         mov     ah,al
  621.         or      [edx+0Ch],ax
  622.         mov     word [edx+18h],1Ch      ; offset of relocation table
  623.         add     [written_size],ecx
  624.         call    write
  625.         jc      write_failed
  626.         ret
  627.  
  628. make_stub:
  629.         mov     [stub_file],edx
  630.         or      edx,edx
  631.         jnz     stub_from_file
  632.         push    esi
  633.         mov     edx,edi
  634.         xor     eax,eax
  635.         mov     ecx,20h
  636.         rep     stos dword [edi]
  637.         mov     eax,40h+default_stub_end-default_stub
  638.         mov     cx,100h+default_stub_end-default_stub
  639.         mov     word [edx],'MZ'
  640.         mov     word [edx+4],1
  641.         mov     word [edx+2],ax
  642.         mov     word [edx+8],4
  643.         mov     word [edx+0Ah],10h
  644.         mov     word [edx+0Ch],0FFFFh
  645.         mov     word [edx+10h],cx
  646.         mov     word [edx+3Ch],ax
  647.         mov     word [edx+18h],40h
  648.         lea     edi,[edx+40h]
  649.         mov     esi,default_stub
  650.         mov     ecx,default_stub_end-default_stub
  651.         rep     movs byte [edi],[esi]
  652.         pop     esi
  653.         jmp     stub_ok
  654.       default_stub:
  655.         use16
  656.         push    cs
  657.         pop     ds
  658.         mov     dx,stub_message-default_stub
  659.         mov     ah,9
  660.         int     21h
  661.         mov     ax,4C01h
  662.         int     21h
  663.       stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
  664.         rq      1
  665.       default_stub_end:
  666.         use32
  667.       stub_from_file:
  668.         push    esi
  669.         mov     esi,edx
  670.         call    open_binary_file
  671.         mov     edx,edi
  672.         mov     ecx,1Ch
  673.         mov     esi,edx
  674.         call    read
  675.         jc      binary_stub
  676.         cmp     word [esi],'MZ'
  677.         jne     binary_stub
  678.         add     edi,1Ch
  679.         movzx   ecx,word [esi+6]
  680.         add     ecx,11b
  681.         and     ecx,not 11b
  682.         add     ecx,(40h-1Ch) shr 2
  683.         lea     eax,[edi+ecx*4]
  684.         cmp     edi,[display_buffer]
  685.         jae     out_of_memory
  686.         xor     eax,eax
  687.         rep     stos dword [edi]
  688.         mov     edx,40h
  689.         xchg    dx,[esi+18h]
  690.         xor     al,al
  691.         call    lseek
  692.         movzx   ecx,word [esi+6]
  693.         shl     ecx,2
  694.         lea     edx,[esi+40h]
  695.         call    read
  696.         mov     edx,edi
  697.         sub     edx,esi
  698.         shr     edx,4
  699.         xchg    dx,[esi+8]
  700.         shl     edx,4
  701.         xor     al,al
  702.         call    lseek
  703.         movzx   ecx,word [esi+4]
  704.         dec     ecx
  705.         shl     ecx,9
  706.         movzx   edx,word [esi+2]
  707.         test    edx,edx
  708.         jnz     stub_header_size_ok
  709.         mov     dx,200h
  710.      stub_header_size_ok:
  711.         add     ecx,edx
  712.         mov     edx,edi
  713.         sub     ecx,eax
  714.         je      read_stub_code
  715.         jb      stub_code_ok
  716.         push    ecx
  717.         dec     ecx
  718.         shr     ecx,3
  719.         inc     ecx
  720.         shl     ecx,1
  721.         lea     eax,[edi+ecx*4]
  722.         cmp     eax,[display_buffer]
  723.         jae     out_of_memory
  724.         xor     eax,eax
  725.         rep     stos dword [edi]
  726.         pop     ecx
  727.      read_stub_code:
  728.         call    read
  729.      stub_code_ok:
  730.         call    close
  731.         mov     edx,edi
  732.         sub     edx,esi
  733.         mov     ax,dx
  734.         and     ax,1FFh
  735.         mov     [esi+2],ax
  736.         dec     edx
  737.         shr     edx,9
  738.         inc     edx
  739.         mov     [esi+4],dx
  740.         mov     eax,edi
  741.         sub     eax,esi
  742.         mov     [esi+3Ch],eax
  743.         pop     esi
  744.       stub_ok:
  745.         ret
  746.       binary_stub:
  747.         mov     esi,edi
  748.         mov     ecx,40h shr 2
  749.         xor     eax,eax
  750.         rep     stos dword [edi]
  751.         mov     al,2
  752.         xor     edx,edx
  753.         call    lseek
  754.         push    eax
  755.         xor     al,al
  756.         xor     edx,edx
  757.         call    lseek
  758.         mov     ecx,[esp]
  759.         add     ecx,40h+111b
  760.         and     ecx,not 111b
  761.         mov     ax,cx
  762.         and     ax,1FFh
  763.         mov     [esi+2],ax
  764.         lea     eax,[ecx+1FFh]
  765.         shr     eax,9
  766.         mov     [esi+4],ax
  767.         mov     [esi+3Ch],ecx
  768.         sub     ecx,40h
  769.         mov     eax,10000h
  770.         sub     eax,ecx
  771.         jbe     binary_heap_ok
  772.         shr     eax,4
  773.         mov     [esi+0Ah],ax
  774.       binary_heap_ok:
  775.         mov     word [esi],'MZ'
  776.         mov     word [esi+8],4
  777.         mov     ax,0FFFFh
  778.         mov     [esi+0Ch],ax
  779.         dec     ax
  780.         mov     [esi+10h],ax
  781.         sub     ax,0Eh
  782.         mov     [esi+0Eh],ax
  783.         mov     [esi+16h],ax
  784.         mov     word [esi+14h],100h
  785.         mov     word [esi+18h],40h
  786.         mov     eax,[display_buffer]
  787.         sub     eax,ecx
  788.         cmp     edi,eax
  789.         jae     out_of_memory
  790.         mov     edx,edi
  791.         shr     ecx,2
  792.         xor     eax,eax
  793.         rep     stos dword [edi]
  794.         pop     ecx
  795.         call    read
  796.         call    close
  797.         pop     esi
  798.         ret
  799.  
  800. format_pe:
  801.         xor     edx,edx
  802.         mov     [machine],14Ch
  803.         mov     [subsystem],3
  804.         mov     [subsystem_version],3 + 10 shl 16
  805.         mov     [image_base],400000h
  806.         test    [format_flags],8
  807.         jz      pe_settings
  808.         mov     [machine],8664h
  809.         mov     [subsystem_version],5 + 0 shl 16
  810.         mov     [image_base_high],0
  811.       pe_settings:
  812.         cmp     byte [esi],84h
  813.         je      get_stub_name
  814.         cmp     byte [esi],80h
  815.         je      get_pe_base
  816.         cmp     byte [esi],1Bh
  817.         jne     pe_settings_ok
  818.         lods    byte [esi]
  819.         lods    byte [esi]
  820.         test    al,80h+40h
  821.         jz      subsystem_setting
  822.         cmp     al,80h
  823.         je      dll_flag
  824.         cmp     al,81h
  825.         je      wdm_flag
  826.         jmp     pe_settings
  827.       dll_flag:
  828.         bts     [format_flags],8
  829.         jc      setting_already_specified
  830.         jmp     pe_settings
  831.       wdm_flag:
  832.         bts     [format_flags],9
  833.         jc      setting_already_specified
  834.         jmp     pe_settings
  835.       subsystem_setting:
  836.         bts     [format_flags],7
  837.         jc      setting_already_specified
  838.         and     ax,3Fh
  839.         mov     [subsystem],ax
  840.         cmp     byte [esi],'('
  841.         jne     pe_settings
  842.         inc     esi
  843.         cmp     byte [esi],'.'
  844.         jne     invalid_value
  845.         inc     esi
  846.         push    edx
  847.         cmp     byte [esi+11],0
  848.         jne     invalid_value
  849.         cmp     byte [esi+10],2
  850.         ja      invalid_value
  851.         mov     dx,[esi+8]
  852.         cmp     dx,8000h
  853.         je      zero_version
  854.         mov     eax,[esi+4]
  855.         cmp     dx,7
  856.         jg      invalid_value
  857.         mov     cx,7
  858.         sub     cx,dx
  859.         mov     eax,[esi+4]
  860.         shr     eax,cl
  861.         mov     ebx,eax
  862.         shr     ebx,24
  863.         cmp     bl,100
  864.         jae     invalid_value
  865.         and     eax,0FFFFFFh
  866.         mov     ecx,100
  867.         mul     ecx
  868.         shrd    eax,edx,24
  869.         jnc     version_value_ok
  870.         inc     eax
  871.       version_value_ok:
  872.         shl     eax,16
  873.         mov     ax,bx
  874.         jmp     subsystem_version_ok
  875.       zero_version:
  876.         xor     eax,eax
  877.       subsystem_version_ok:
  878.         pop     edx
  879.         add     esi,13
  880.         mov     [subsystem_version],eax
  881.         jmp     pe_settings
  882.       get_pe_base:
  883.         bts     [format_flags],10
  884.         jc      setting_already_specified
  885.         lods    word [esi]
  886.         cmp     ah,'('
  887.         jne     invalid_argument
  888.         cmp     byte [esi],'.'
  889.         je      invalid_value
  890.         push    edx edi
  891.         add     edi,[stub_size]
  892.         test    [format_flags],8
  893.         jnz     get_pe64_base
  894.         call    get_dword_value
  895.         mov     [image_base],eax
  896.         jmp     pe_base_ok
  897.       get_pe64_base:
  898.         call    get_qword_value
  899.         mov     [image_base],eax
  900.         mov     [image_base_high],edx
  901.       pe_base_ok:
  902.         pop     edi edx
  903.         cmp     [value_type],0
  904.         jne     invalid_use_of_symbol
  905.         cmp     byte [esi],84h
  906.         jne     pe_settings_ok
  907.       get_stub_name:
  908.         lods    byte [esi]
  909.         lods    word [esi]
  910.         cmp     ax,'('
  911.         jne     invalid_argument
  912.         lods    dword [esi]
  913.         mov     edx,esi
  914.         add     esi,eax
  915.         inc     esi
  916.       pe_settings_ok:
  917.         mov     ebp,[stub_size]
  918.         or      ebp,ebp
  919.         jz      make_pe_stub
  920.         cmp     edx,[stub_file]
  921.         je      pe_stub_ok
  922.         sub     edi,[stub_size]
  923.         mov     [code_start],edi
  924.       make_pe_stub:
  925.         call    make_stub
  926.         mov     eax,edi
  927.         sub     eax,[code_start]
  928.         mov     [stub_size],eax
  929.         mov     [code_start],edi
  930.         mov     ebp,eax
  931.       pe_stub_ok:
  932.         mov     edx,edi
  933.         mov     ecx,18h+0E0h
  934.         test    [format_flags],8
  935.         jz      zero_pe_header
  936.         add     ecx,10h
  937.       zero_pe_header:
  938.         add     ebp,ecx
  939.         shr     ecx,2
  940.         xor     eax,eax
  941.         rep     stos dword [edi]
  942.         mov     word [edx],'PE'         ; signature
  943.         mov     ax,[machine]
  944.         mov     word [edx+4],ax
  945.         mov     dword [edx+38h],1000h   ; section alignment
  946.         mov     dword [edx+3Ch],200h    ; file alignment
  947.         mov     word [edx+40h],1        ; OS version
  948.         mov     eax,[subsystem_version]
  949.         mov     [edx+48h],eax
  950.         mov     ax,[subsystem]
  951.         mov     [edx+5Ch],ax
  952.         cmp     ax,1
  953.         jne     pe_alignment_ok
  954.         mov     eax,20h
  955.         mov     dword [edx+38h],eax
  956.         mov     dword [edx+3Ch],eax
  957.       pe_alignment_ok:
  958.         mov     word [edx+1Ah],VERSION_MAJOR + VERSION_MINOR shl 8
  959.         test    [format_flags],8
  960.         jnz     init_pe64_specific
  961.         mov     dword [edx+14h],0E0h    ; size of optional header
  962.         mov     dword [edx+16h],10B010Eh; flags and magic value
  963.         mov     eax,[image_base]
  964.         mov     dword [edx+34h],eax
  965.         mov     dword [edx+60h],1000h   ; stack reserve
  966.         mov     dword [edx+64h],1000h   ; stack commit
  967.         mov     dword [edx+68h],10000h  ; heap reserve
  968.         mov     dword [edx+6Ch],0       ; heap commit
  969.         mov     dword [edx+74h],16      ; number of directories
  970.         jmp     pe_header_ok
  971.       init_pe64_specific:
  972.         mov     dword [edx+14h],0F0h    ; size of optional header
  973.         mov     dword [edx+16h],20B002Eh; flags and magic value
  974.         mov     eax,[image_base]
  975.         mov     dword [edx+30h],eax
  976.         mov     eax,[image_base_high]
  977.         mov     dword [edx+34h],eax
  978.         mov     dword [edx+60h],1000h   ; stack reserve
  979.         mov     dword [edx+68h],1000h   ; stack commit
  980.         mov     dword [edx+70h],10000h  ; heap reserve
  981.         mov     dword [edx+78h],0       ; heap commit
  982.         mov     dword [edx+84h],16      ; number of directories
  983.       pe_header_ok:
  984.         bsf     ecx,[edx+3Ch]
  985.         imul    ebx,[number_of_sections],28h
  986.         or      ebx,ebx
  987.         jnz     reserve_space_for_section_headers
  988.         mov     ebx,28h
  989.       reserve_space_for_section_headers:
  990.         add     ebx,ebp
  991.         dec     ebx
  992.         shr     ebx,cl
  993.         inc     ebx
  994.         shl     ebx,cl
  995.         sub     ebx,ebp
  996.         mov     ecx,ebx
  997.         mov     eax,[display_buffer]
  998.         sub     eax,ecx
  999.         cmp     edi,eax
  1000.         jae     out_of_memory
  1001.         shr     ecx,2
  1002.         xor     eax,eax
  1003.         rep     stos dword [edi]
  1004.         mov     eax,edi
  1005.         sub     eax,[code_start]
  1006.         add     eax,[stub_size]
  1007.         mov     [edx+54h],eax           ; size of headers
  1008.         mov     ecx,[edx+38h]
  1009.         dec     ecx
  1010.         add     eax,ecx
  1011.         not     ecx
  1012.         and     eax,ecx
  1013.         bt      [format_flags],8
  1014.         jc      pe_entry_init_ok
  1015.         mov     [edx+28h],eax           ; entry point rva
  1016.       pe_entry_init_ok:
  1017.         mov     [number_of_sections],0
  1018.         movzx   ebx,word [edx+14h]
  1019.         lea     ebx,[edx+18h+ebx]
  1020.         mov     [current_section],ebx
  1021.         mov     dword [ebx],'.fla'
  1022.         mov     dword [ebx+4],'t'
  1023.         mov     [ebx+14h],edi
  1024.         mov     [ebx+0Ch],eax
  1025.         mov     dword [ebx+24h],0E0000060h
  1026.         xor     ecx,ecx
  1027.         not     eax
  1028.         not     ecx
  1029.         add     eax,1
  1030.         adc     ecx,0
  1031.         add     eax,edi
  1032.         adc     ecx,0
  1033.         test    [format_flags],8
  1034.         jnz     pe64_org
  1035.         sub     eax,[edx+34h]
  1036.         sbb     ecx,0
  1037.         mov     bl,2
  1038.         mov     [code_type],32
  1039.         jmp     pe_org_ok
  1040.       pe64_org:
  1041.         sub     eax,[edx+30h]
  1042.         sbb     ecx,[edx+34h]
  1043.         mov     bl,4
  1044.         mov     [code_type],64
  1045.       pe_org_ok:
  1046.         bt      [resolver_flags],0
  1047.         jc      pe_labels_type_ok
  1048.         xor     bl,bl
  1049.       pe_labels_type_ok:
  1050.         mov     [labels_type],bl
  1051.         mov     dword [org_origin],eax
  1052.         mov     dword [org_origin+4],ecx
  1053.         mov     [org_registers],0
  1054.         mov     [org_start],edi
  1055.         bt      [format_flags],8
  1056.         jnc     dll_flag_ok
  1057.         or      dword [edx+16h],2000h
  1058.       dll_flag_ok:
  1059.         bt      [format_flags],9
  1060.         jnc     wdm_flag_ok
  1061.         or      word [edx+5Eh],2000h
  1062.       wdm_flag_ok:
  1063.         jmp     format_defined
  1064. pe_section:
  1065.         call    close_pe_section
  1066.         bts     [format_flags],5
  1067.         lea     ecx,[ebx+28h]
  1068.         add     edx,[edx+54h]
  1069.         sub     edx,[stub_size]
  1070.         cmp     ecx,edx
  1071.         jbe     new_section
  1072.         lea     ebx,[edx-28h]
  1073.         or      [next_pass_needed],-1
  1074.         push    edi
  1075.         mov     edi,ebx
  1076.         mov     ecx,28h shr 4
  1077.         xor     eax,eax
  1078.         rep     stos dword [edi]
  1079.         pop     edi
  1080.       new_section:
  1081.         mov     [ebx+0Ch],eax
  1082.         lods    word [esi]
  1083.         cmp     ax,'('
  1084.         jne     invalid_argument
  1085.         lea     edx,[esi+4]
  1086.         mov     ecx,[esi]
  1087.         lea     esi,[esi+4+ecx+1]
  1088.         cmp     ecx,8
  1089.         ja      name_too_long
  1090.         xor     eax,eax
  1091.         mov     [ebx],eax
  1092.         mov     [ebx+4],eax
  1093.         push    esi edi
  1094.         mov     edi,ebx
  1095.         mov     esi,edx
  1096.         rep     movs byte [edi],[esi]
  1097.         pop     edi esi
  1098.         mov     dword [ebx+24h],0
  1099.         mov     [ebx+14h],edi
  1100.         mov     edx,[code_start]
  1101.         mov     eax,edi
  1102.         xor     ecx,ecx
  1103.         sub     eax,[ebx+0Ch]
  1104.         sbb     ecx,0
  1105.         test    [format_flags],8
  1106.         jnz     pe64_section_org
  1107.         sub     eax,[edx+34h]
  1108.         sbb     ecx,0
  1109.         mov     [labels_type],2
  1110.         mov     [code_type],32
  1111.         bt      [resolver_flags],0
  1112.         jc      pe_section_org_ok
  1113.         mov     [labels_type],0
  1114.         jmp     pe_section_org_ok
  1115.       pe64_section_org:
  1116.         sub     eax,[edx+30h]
  1117.         sbb     ecx,[edx+34h]
  1118.         mov     [labels_type],4
  1119.         mov     [code_type],64
  1120.         bt      [resolver_flags],0
  1121.         jc      pe_section_org_ok
  1122.         mov     [labels_type],0
  1123.       pe_section_org_ok:
  1124.         mov     dword [org_origin],eax
  1125.         mov     dword [org_origin+4],ecx
  1126.         mov     [org_registers],0
  1127.         mov     [org_start],edi
  1128.       get_section_flags:
  1129.         lods    byte [esi]
  1130.         cmp     al,1Ah
  1131.         je      set_directory
  1132.         cmp     al,19h
  1133.         je      section_flag
  1134.         dec     esi
  1135.         jmp     instruction_assembled
  1136.       set_directory:
  1137.         movzx   eax,byte [esi]
  1138.         inc     esi
  1139.         mov     ecx,ebx
  1140.         test    [format_flags],8
  1141.         jnz     pe64_directory
  1142.         xchg    ecx,[edx+78h+eax*8]
  1143.         mov     dword [edx+78h+eax*8+4],-1
  1144.         jmp     pe_directory_set
  1145.       pe64_directory:
  1146.         xchg    ecx,[edx+88h+eax*8]
  1147.         mov     dword [edx+88h+eax*8+4],-1
  1148.       pe_directory_set:
  1149.         or      ecx,ecx
  1150.         jnz     data_already_defined
  1151.         push    ebx edx
  1152.         call    generate_pe_data
  1153.         pop     edx ebx
  1154.         jmp     get_section_flags
  1155.       section_flag:
  1156.         lods    byte [esi]
  1157.         cmp     al,9
  1158.         je      invalid_argument
  1159.         cmp     al,11
  1160.         je      invalid_argument
  1161.         mov     cl,al
  1162.         mov     eax,1
  1163.         shl     eax,cl
  1164.         test    dword [ebx+24h],eax
  1165.         jnz     setting_already_specified
  1166.         or      dword [ebx+24h],eax
  1167.         jmp     get_section_flags
  1168.       close_pe_section:
  1169.         mov     ebx,[current_section]
  1170.         mov     edx,[code_start]
  1171.         mov     eax,edi
  1172.         sub     eax,[ebx+14h]
  1173.         jnz     finish_section
  1174.         bt      [format_flags],5
  1175.         jc      finish_section
  1176.         mov     eax,[ebx+0Ch]
  1177.         ret
  1178.       finish_section:
  1179.         mov     [ebx+8],eax
  1180.         cmp     edi,[undefined_data_end]
  1181.         jne     align_section
  1182.         cmp     dword [edx+38h],1000h
  1183.         jb      align_section
  1184.         mov     edi,[undefined_data_start]
  1185.       align_section:
  1186.         mov     [undefined_data_end],0
  1187.         mov     ebp,edi
  1188.         sub     ebp,[ebx+14h]
  1189.         mov     ecx,[edx+3Ch]
  1190.         dec     ecx
  1191.         lea     eax,[ebp+ecx]
  1192.         not     ecx
  1193.         and     eax,ecx
  1194.         mov     [ebx+10h],eax
  1195.         sub     eax,ebp
  1196.         mov     ecx,eax
  1197.         xor     al,al
  1198.         rep     stos byte [edi]
  1199.         mov     eax,[code_start]
  1200.         sub     eax,[stub_size]
  1201.         sub     [ebx+14h],eax
  1202.         mov     eax,[ebx+8]
  1203.         or      eax,eax
  1204.         jz      udata_ok
  1205.         cmp     dword [ebx+10h],0
  1206.         jne     udata_ok
  1207.         or      byte [ebx+24h],80h
  1208.       udata_ok:
  1209.         mov     ecx,[edx+38h]
  1210.         dec     ecx
  1211.         add     eax,ecx
  1212.         not     ecx
  1213.         and     eax,ecx
  1214.         add     eax,[ebx+0Ch]
  1215.         add     ebx,28h
  1216.         mov     [current_section],ebx
  1217.         inc     word [number_of_sections]
  1218.         jz      format_limitations_exceeded
  1219.         ret
  1220. data_directive:
  1221.         cmp     [output_format],3
  1222.         jne     illegal_instruction
  1223.         lods    byte [esi]
  1224.         cmp     al,1Ah
  1225.         je      predefined_data_type
  1226.         cmp     al,'('
  1227.         jne     invalid_argument
  1228.         call    get_byte_value
  1229.         cmp     al,16
  1230.         jb      data_type_ok
  1231.         jmp     invalid_value
  1232.       predefined_data_type:
  1233.         movzx   eax,byte [esi]
  1234.         inc     esi
  1235.       data_type_ok:
  1236.         mov     ebx,[current_section]
  1237.         mov     ecx,edi
  1238.         sub     ecx,[ebx+14h]
  1239.         add     ecx,[ebx+0Ch]
  1240.         mov     edx,[code_start]
  1241.         test    [format_flags],8
  1242.         jnz     pe64_data
  1243.         xchg    ecx,[edx+78h+eax*8]
  1244.         jmp     init_pe_data
  1245.       pe64_data:
  1246.         xchg    ecx,[edx+88h+eax*8]
  1247.       init_pe_data:
  1248.         or      ecx,ecx
  1249.         jnz     data_already_defined
  1250.         call    allocate_structure_data
  1251.         mov     word [ebx],data_directive-assembler
  1252.         mov     [ebx+2],al
  1253.         mov     edx,[current_line]
  1254.         mov     [ebx+4],edx
  1255.         call    generate_pe_data
  1256.         jmp     instruction_assembled
  1257.       end_data:
  1258.         cmp     [output_format],3
  1259.         jne     illegal_instruction
  1260.         call    find_structure_data
  1261.         jc      unexpected_instruction
  1262.         movzx   eax,byte [ebx+2]
  1263.         mov     edx,[current_section]
  1264.         mov     ecx,edi
  1265.         sub     ecx,[edx+14h]
  1266.         add     ecx,[edx+0Ch]
  1267.         mov     edx,[code_start]
  1268.         test    [format_flags],8
  1269.         jnz     end_pe64_data
  1270.         sub     ecx,[edx+78h+eax*8]
  1271.         mov     [edx+78h+eax*8+4],ecx
  1272.         jmp     remove_structure_data
  1273.       end_pe64_data:
  1274.         sub     ecx,[edx+88h+eax*8]
  1275.         mov     [edx+88h+eax*8+4],ecx
  1276.         jmp     remove_structure_data
  1277. pe_entry:
  1278.         lods    byte [esi]
  1279.         cmp     al,'('
  1280.         jne     invalid_argument
  1281.         cmp     byte [esi],'.'
  1282.         je      invalid_value
  1283.         test    [format_flags],8
  1284.         jnz     pe64_entry
  1285.         call    get_dword_value
  1286.         mov     bl,2
  1287.         bt      [resolver_flags],0
  1288.         jc      check_pe_entry_label_type
  1289.         xor     bl,bl
  1290.       check_pe_entry_label_type:
  1291.         cmp     [value_type],bl
  1292.         je      pe_entry_ok
  1293.         cmp     [error_line],0
  1294.         jne     pe_entry_ok
  1295.         mov     edx,[current_line]
  1296.         mov     [error_line],edx
  1297.         mov     [error],invalid_address
  1298.       pe_entry_ok:
  1299.         mov     edx,[code_start]
  1300.         sub     eax,[edx+34h]
  1301.         mov     [edx+28h],eax
  1302.         jmp     instruction_assembled
  1303.       pe64_entry:
  1304.         call    get_qword_value
  1305.         mov     bl,4
  1306.         bt      [resolver_flags],0
  1307.         jc      check_pe64_entry_label_type
  1308.         xor     bl,bl
  1309.       check_pe64_entry_label_type:
  1310.         cmp     [value_type],bl
  1311.         je      pe64_entry_type_ok
  1312.         cmp     [error_line],0
  1313.         jne     pe64_entry_type_ok
  1314.         mov     edx,[current_line]
  1315.         mov     [error_line],edx
  1316.         mov     [error],invalid_address
  1317.       pe64_entry_type_ok:
  1318.         mov     ecx,[code_start]
  1319.         sub     eax,[ecx+30h]
  1320.         sbb     edx,[ecx+34h]
  1321.         jz      pe64_entry_range_ok
  1322.         mov     edx,[current_line]
  1323.         mov     [error_line],edx
  1324.         mov     [error],value_out_of_range
  1325.       pe64_entry_range_ok:
  1326.         mov     [ecx+28h],eax
  1327.         jmp     instruction_assembled
  1328. pe_stack:
  1329.         lods    byte [esi]
  1330.         cmp     al,'('
  1331.         jne     invalid_argument
  1332.         cmp     byte [esi],'.'
  1333.         je      invalid_value
  1334.         test    [format_flags],8
  1335.         jnz     pe64_stack
  1336.         call    get_dword_value
  1337.         cmp     [value_type],0
  1338.         jne     invalid_use_of_symbol
  1339.         mov     edx,[code_start]
  1340.         mov     [edx+60h],eax
  1341.         cmp     byte [esi],','
  1342.         jne     default_stack_commit
  1343.         lods    byte [esi]
  1344.         lods    byte [esi]
  1345.         cmp     al,'('
  1346.         jne     invalid_argument
  1347.         cmp     byte [esi],'.'
  1348.         je      invalid_value
  1349.         call    get_dword_value
  1350.         cmp     [value_type],0
  1351.         jne     invalid_use_of_symbol
  1352.         mov     edx,[code_start]
  1353.         mov     [edx+64h],eax
  1354.         cmp     eax,[edx+60h]
  1355.         ja      value_out_of_range
  1356.         jmp     instruction_assembled
  1357.       default_stack_commit:
  1358.         mov     dword [edx+64h],1000h
  1359.         mov     eax,[edx+60h]
  1360.         cmp     eax,1000h
  1361.         ja      instruction_assembled
  1362.         mov     dword [edx+64h],eax
  1363.         jmp     instruction_assembled
  1364.       pe64_stack:
  1365.         call    get_qword_value
  1366.         cmp     [value_type],0
  1367.         jne     invalid_use_of_symbol
  1368.         mov     ecx,[code_start]
  1369.         mov     [ecx+60h],eax
  1370.         mov     [ecx+64h],edx
  1371.         cmp     byte [esi],','
  1372.         jne     default_pe64_stack_commit
  1373.         lods    byte [esi]
  1374.         lods    byte [esi]
  1375.         cmp     al,'('
  1376.         jne     invalid_argument
  1377.         cmp     byte [esi],'.'
  1378.         je      invalid_value
  1379.         call    get_qword_value
  1380.         cmp     [value_type],0
  1381.         jne     invalid_use_of_symbol
  1382.         mov     ecx,[code_start]
  1383.         mov     [ecx+68h],eax
  1384.         mov     [ecx+6Ch],edx
  1385.         cmp     edx,[ecx+64h]
  1386.         ja      value_out_of_range
  1387.         jb      instruction_assembled
  1388.         cmp     eax,[ecx+60h]
  1389.         ja      value_out_of_range
  1390.         jmp     instruction_assembled
  1391.       default_pe64_stack_commit:
  1392.         mov     dword [ecx+68h],1000h
  1393.         cmp     dword [ecx+64h],0
  1394.         jne     instruction_assembled
  1395.         mov     eax,[ecx+60h]
  1396.         cmp     eax,1000h
  1397.         ja      instruction_assembled
  1398.         mov     dword [ecx+68h],eax
  1399.         jmp     instruction_assembled
  1400. pe_heap:
  1401.         lods    byte [esi]
  1402.         cmp     al,'('
  1403.         jne     invalid_argument
  1404.         cmp     byte [esi],'.'
  1405.         je      invalid_value
  1406.         test    [format_flags],8
  1407.         jnz     pe64_heap
  1408.         call    get_dword_value
  1409.         cmp     [value_type],0
  1410.         jne     invalid_use_of_symbol
  1411.         mov     edx,[code_start]
  1412.         mov     [edx+68h],eax
  1413.         cmp     byte [esi],','
  1414.         jne     instruction_assembled
  1415.         lods    byte [esi]
  1416.         lods    byte [esi]
  1417.         cmp     al,'('
  1418.         jne     invalid_argument
  1419.         cmp     byte [esi],'.'
  1420.         je      invalid_value
  1421.         call    get_dword_value
  1422.         cmp     [value_type],0
  1423.         jne     invalid_use_of_symbol
  1424.         mov     edx,[code_start]
  1425.         mov     [edx+6Ch],eax
  1426.         cmp     eax,[edx+68h]
  1427.         ja      value_out_of_range
  1428.         jmp     instruction_assembled
  1429.       pe64_heap:
  1430.         call    get_qword_value
  1431.         cmp     [value_type],0
  1432.         jne     invalid_use_of_symbol
  1433.         mov     ecx,[code_start]
  1434.         mov     [ecx+70h],eax
  1435.         mov     [ecx+74h],edx
  1436.         cmp     byte [esi],','
  1437.         jne     instruction_assembled
  1438.         lods    byte [esi]
  1439.         lods    byte [esi]
  1440.         cmp     al,'('
  1441.         jne     invalid_argument
  1442.         cmp     byte [esi],'.'
  1443.         je      invalid_value
  1444.         call    get_qword_value
  1445.         cmp     [value_type],0
  1446.         jne     invalid_use_of_symbol
  1447.         mov     ecx,[code_start]
  1448.         mov     [ecx+78h],eax
  1449.         mov     [ecx+7Ch],edx
  1450.         cmp     edx,[ecx+74h]
  1451.         ja      value_out_of_range
  1452.         jb      instruction_assembled
  1453.         cmp     eax,[edx+70h]
  1454.         ja      value_out_of_range
  1455.         jmp     instruction_assembled
  1456. mark_pe_relocation:
  1457.         push    eax ebx
  1458.         test    [format_flags],8
  1459.         jz      check_pe32_relocation_type
  1460.         cmp     [value_type],4
  1461.         je      pe_relocation_type_ok
  1462.       check_pe32_relocation_type:
  1463.         cmp     [value_type],2
  1464.         je      pe_relocation_type_ok
  1465.         cmp     [error_line],0
  1466.         jne     pe_relocation_type_ok
  1467.         mov     eax,[current_line]
  1468.         mov     [error_line],eax
  1469.         mov     [error],invalid_use_of_symbol
  1470.       pe_relocation_type_ok:
  1471.         mov     ebx,[current_section]
  1472.         mov     eax,edi
  1473.         sub     eax,[ebx+14h]
  1474.         add     eax,[ebx+0Ch]
  1475.         mov     ebx,[free_additional_memory]
  1476.         inc     [number_of_relocations]
  1477.         jz      invalid_use_of_symbol
  1478.         add     ebx,5
  1479.         cmp     ebx,[structures_buffer]
  1480.         jae     out_of_memory
  1481.         mov     [free_additional_memory],ebx
  1482.         mov     [ebx-5],eax
  1483.         cmp     [value_type],2
  1484.         je      fixup_32bit
  1485.         mov     byte [ebx-1],0Ah
  1486.         jmp     fixup_ok
  1487.       fixup_32bit:
  1488.         mov     byte [ebx-1],3
  1489.       fixup_ok:
  1490.         pop     ebx eax
  1491.         ret
  1492. generate_pe_data:
  1493.         cmp     al,2
  1494.         je      make_pe_resource
  1495.         cmp     al,5
  1496.         je      make_pe_fixups
  1497.         ret
  1498. make_pe_fixups:
  1499.         bts     [resolver_flags],0
  1500.         jc      pe_relocatable_ok
  1501.         or      [next_pass_needed],-1
  1502.      pe_relocatable_ok:
  1503.         push    esi
  1504.         mov     ecx,[number_of_relocations]
  1505.         mov     esi,[free_additional_memory]
  1506.         lea     eax,[ecx*5]
  1507.         sub     esi,eax
  1508.         mov     [free_additional_memory],esi
  1509.         or      [number_of_relocations],-1
  1510.         xor     edx,edx
  1511.         mov     ebp,edi
  1512.       make_fixups:
  1513.         cmp     [esi],edx
  1514.         jb      store_fixup
  1515.         mov     eax,edi
  1516.         sub     eax,ebp
  1517.         test    eax,11b
  1518.         jz      fixups_block
  1519.         xor     ax,ax
  1520.         stos    word [edi]
  1521.         add     dword [ebx],2
  1522.       fixups_block:
  1523.         mov     eax,edx
  1524.         add     edx,1000h
  1525.         cmp     [esi],edx
  1526.         jae     fixups_block
  1527.         stos    dword [edi]
  1528.         mov     ebx,edi
  1529.         mov     eax,8
  1530.         stos    dword [edi]
  1531.       store_fixup:
  1532.         jecxz   fixups_done
  1533.         add     dword [ebx],2
  1534.         mov     ah,[esi+1]
  1535.         and     ah,0Fh
  1536.         mov     al,[esi+4]
  1537.         shl     al,4
  1538.         or      ah,al
  1539.         mov     al,[esi]
  1540.         stos    word [edi]
  1541.         add     esi,5
  1542.         loop    make_fixups
  1543.       fixups_done:
  1544.         pop     esi
  1545.         ret
  1546. make_pe_resource:
  1547.         cmp     byte [esi],82h
  1548.         jne     resource_done
  1549.         inc     esi
  1550.         lods    word [esi]
  1551.         cmp     ax,'('
  1552.         jne     invalid_argument
  1553.         lods    dword [esi]
  1554.         mov     edx,esi
  1555.         lea     esi,[esi+eax+1]
  1556.         cmp     [next_pass_needed],0
  1557.         je      resource_from_file
  1558.         cmp     [current_pass],0
  1559.         jne     reserve_space_for_resource
  1560.         mov     [resource_size],0
  1561.       reserve_space_for_resource:
  1562.         add     edi,[resource_size]
  1563.         cmp     edi,[display_buffer]
  1564.         ja      out_of_memory
  1565.         jmp     resource_done
  1566.       resource_from_file:
  1567.         push    esi
  1568.         mov     esi,edx
  1569.         call    open_binary_file
  1570.         push    ebx
  1571.         mov     esi,[free_additional_memory]
  1572.         lea     eax,[esi+20h]
  1573.         cmp     eax,[structures_buffer]
  1574.         ja      out_of_memory
  1575.         mov     edx,esi
  1576.         mov     ecx,20h
  1577.         call    read
  1578.         jc      invalid_file_format
  1579.         xor     eax,eax
  1580.         cmp     [esi],eax
  1581.         jne     invalid_file_format
  1582.         mov     ax,0FFFFh
  1583.         cmp     [esi+8],eax
  1584.         jne     invalid_file_format
  1585.         cmp     [esi+12],eax
  1586.         jne     invalid_file_format
  1587.         mov     eax,20h
  1588.         cmp     [esi+4],eax
  1589.         jne     invalid_file_format
  1590.       read_resource_headers:
  1591.         test    eax,11b
  1592.         jz      resource_file_alignment_ok
  1593.         mov     edx,4
  1594.         and     eax,11b
  1595.         sub     edx,eax
  1596.         mov     al,1
  1597.         call    lseek
  1598.       resource_file_alignment_ok:
  1599.         mov     [esi],eax
  1600.         lea     edx,[esi+12]
  1601.         mov     ecx,8
  1602.         call    read
  1603.         jc      resource_headers_ok
  1604.         mov     ecx,[esi+16]
  1605.         add     [esi],ecx
  1606.         lea     edx,[esi+20]
  1607.         sub     ecx,8
  1608.         mov     [esi+16],ecx
  1609.         lea     eax,[edx+ecx]
  1610.         cmp     eax,[structures_buffer]
  1611.         ja      out_of_memory
  1612.         call    read
  1613.         jc      invalid_file_format
  1614.         mov     edx,[esi]
  1615.         add     edx,[esi+12]
  1616.         mov     eax,[esi+16]
  1617.         lea     ecx,[esi+20]
  1618.         lea     esi,[ecx+eax]
  1619.         add     ecx,2
  1620.         cmp     word [ecx-2],0FFFFh
  1621.         je      resource_header_type_ok
  1622.       check_resource_header_type:
  1623.         cmp     ecx,esi
  1624.         jae     invalid_file_format
  1625.         cmp     word [ecx],0
  1626.         je      resource_header_type_ok
  1627.         add     ecx,2
  1628.         jmp     check_resource_header_type
  1629.       resource_header_type_ok:
  1630.         add     ecx,2
  1631.         cmp     word [ecx],0FFFFh
  1632.         je      resource_header_name_ok
  1633.       check_resource_header_name:
  1634.         cmp     ecx,esi
  1635.         jae     invalid_file_format
  1636.         cmp     word [ecx],0
  1637.         je      resource_header_name_ok
  1638.         add     ecx,2
  1639.         jmp     check_resource_header_name
  1640.       resource_header_name_ok:
  1641.         xor     al,al
  1642.         call    lseek
  1643.         jmp     read_resource_headers
  1644.       resource_headers_ok:
  1645.         xor     eax,eax
  1646.         mov     [esi],eax
  1647.         mov     [resource_data],edi
  1648.         lea     eax,[edi+16]
  1649.         cmp     eax,[display_buffer]
  1650.         jae     out_of_memory
  1651.         xor     eax,eax
  1652.         stos    dword [edi]
  1653.         call    make_timestamp
  1654.         stos    dword [edi]
  1655.         xor     eax,eax
  1656.         stos    dword [edi]
  1657.         stos    dword [edi]
  1658.         xor     ebx,ebx
  1659.       make_type_name_directory:
  1660.         mov     esi,[free_additional_memory]
  1661.         xor     edx,edx
  1662.       find_type_name:
  1663.         cmp     dword [esi],0
  1664.         je      type_name_ok
  1665.         add     esi,20
  1666.         cmp     word [esi],0FFFFh
  1667.         je      check_next_type_name
  1668.         or      ebx,ebx
  1669.         jz      check_this_type_name
  1670.         xor     ecx,ecx
  1671.       compare_with_previous_type_name:
  1672.         mov     ax,[esi+ecx]
  1673.         cmp     ax,[ebx+ecx]
  1674.         ja      check_this_type_name
  1675.         jb      check_next_type_name
  1676.         add     ecx,2
  1677.         mov     ax,[esi+ecx]
  1678.         or      ax,[ebx+ecx]
  1679.         jnz     compare_with_previous_type_name
  1680.         jmp     check_next_type_name
  1681.       check_this_type_name:
  1682.         or      edx,edx
  1683.         jz      type_name_found
  1684.         xor     ecx,ecx
  1685.       compare_with_current_type_name:
  1686.         mov     ax,[esi+ecx]
  1687.         cmp     ax,[edx+ecx]
  1688.         ja      check_next_type_name
  1689.         jb      type_name_found
  1690.         add     ecx,2
  1691.         mov     ax,[esi+ecx]
  1692.         or      ax,[edx+ecx]
  1693.         jnz     compare_with_current_type_name
  1694.         jmp     same_type_name
  1695.       type_name_found:
  1696.         mov     edx,esi
  1697.       same_type_name:
  1698.         mov     [esi-16],edi
  1699.       check_next_type_name:
  1700.         mov     eax,[esi-4]
  1701.         add     esi,eax
  1702.         jmp     find_type_name
  1703.       type_name_ok:
  1704.         or      edx,edx
  1705.         jz      type_name_directory_done
  1706.         mov     ebx,edx
  1707.       make_type_name_entry:
  1708.         mov     eax,[resource_data]
  1709.         inc     word [eax+12]
  1710.         lea     eax,[edi+8]
  1711.         cmp     eax,[display_buffer]
  1712.         jae     out_of_memory
  1713.         mov     eax,ebx
  1714.         stos    dword [edi]
  1715.         xor     eax,eax
  1716.         stos    dword [edi]
  1717.         jmp     make_type_name_directory
  1718.       type_name_directory_done:
  1719.         mov     ebx,-1
  1720.       make_type_id_directory:
  1721.         mov     esi,[free_additional_memory]
  1722.         mov     edx,10000h
  1723.       find_type_id:
  1724.         cmp     dword [esi],0
  1725.         je      type_id_ok
  1726.         add     esi,20
  1727.         cmp     word [esi],0FFFFh
  1728.         jne     check_next_type_id
  1729.         movzx   eax,word [esi+2]
  1730.         cmp     eax,ebx
  1731.         jle     check_next_type_id
  1732.         cmp     eax,edx
  1733.         jg      check_next_type_id
  1734.         mov     edx,eax
  1735.         mov     [esi-16],edi
  1736.       check_next_type_id:
  1737.         mov     eax,[esi-4]
  1738.         add     esi,eax
  1739.         jmp     find_type_id
  1740.       type_id_ok:
  1741.         cmp     edx,10000h
  1742.         je      type_id_directory_done
  1743.         mov     ebx,edx
  1744.       make_type_id_entry:
  1745.         mov     eax,[resource_data]
  1746.         inc     word [eax+14]
  1747.         lea     eax,[edi+8]
  1748.         cmp     eax,[display_buffer]
  1749.         jae     out_of_memory
  1750.         mov     eax,ebx
  1751.         stos    dword [edi]
  1752.         xor     eax,eax
  1753.         stos    dword [edi]
  1754.         jmp     make_type_id_directory
  1755.       type_id_directory_done:
  1756.         mov     esi,[resource_data]
  1757.         add     esi,10h
  1758.         mov     ecx,[esi-4]
  1759.         or      cx,cx
  1760.         jz      resource_directories_ok
  1761.       make_resource_directories:
  1762.         push    ecx
  1763.         push    edi
  1764.         mov     edx,edi
  1765.         sub     edx,[resource_data]
  1766.         bts     edx,31
  1767.         mov     [esi+4],edx
  1768.         lea     eax,[edi+16]
  1769.         cmp     eax,[display_buffer]
  1770.         jae     out_of_memory
  1771.         xor     eax,eax
  1772.         stos    dword [edi]
  1773.         call    make_timestamp
  1774.         stos    dword [edi]
  1775.         xor     eax,eax
  1776.         stos    dword [edi]
  1777.         stos    dword [edi]
  1778.         mov     ebp,esi
  1779.         xor     ebx,ebx
  1780.       make_resource_name_directory:
  1781.         mov     esi,[free_additional_memory]
  1782.         xor     edx,edx
  1783.       find_resource_name:
  1784.         cmp     dword [esi],0
  1785.         je      resource_name_ok
  1786.         push    esi
  1787.         cmp     [esi+4],ebp
  1788.         jne     check_next_resource_name
  1789.         add     esi,20
  1790.         call    skip_resource_name
  1791.         cmp     word [esi],0FFFFh
  1792.         je      check_next_resource_name
  1793.         or      ebx,ebx
  1794.         jz      check_this_resource_name
  1795.         xor     ecx,ecx
  1796.       compare_with_previous_resource_name:
  1797.         mov     ax,[esi+ecx]
  1798.         cmp     ax,[ebx+ecx]
  1799.         ja      check_this_resource_name
  1800.         jb      check_next_resource_name
  1801.         add     ecx,2
  1802.         mov     ax,[esi+ecx]
  1803.         or      ax,[ebx+ecx]
  1804.         jnz     compare_with_previous_resource_name
  1805.         jmp     check_next_resource_name
  1806.       skip_resource_name:
  1807.         cmp     word [esi],0FFFFh
  1808.         jne     skip_unicode_string
  1809.         add     esi,4
  1810.         ret
  1811.       skip_unicode_string:
  1812.         add     esi,2
  1813.         cmp     word [esi-2],0
  1814.         jne     skip_unicode_string
  1815.         ret
  1816.       check_this_resource_name:
  1817.         or      edx,edx
  1818.         jz      resource_name_found
  1819.         xor     ecx,ecx
  1820.       compare_with_current_resource_name:
  1821.         mov     ax,[esi+ecx]
  1822.         cmp     ax,[edx+ecx]
  1823.         ja      check_next_resource_name
  1824.         jb      resource_name_found
  1825.         add     ecx,2
  1826.         mov     ax,[esi+ecx]
  1827.         or      ax,[edx+ecx]
  1828.         jnz     compare_with_current_resource_name
  1829.         jmp     same_resource_name
  1830.       resource_name_found:
  1831.         mov     edx,esi
  1832.       same_resource_name:
  1833.         mov     eax,[esp]
  1834.         mov     [eax+8],edi
  1835.       check_next_resource_name:
  1836.         pop     esi
  1837.         mov     eax,[esi+16]
  1838.         lea     esi,[esi+20+eax]
  1839.         jmp     find_resource_name
  1840.       resource_name_ok:
  1841.         or      edx,edx
  1842.         jz      resource_name_directory_done
  1843.         mov     ebx,edx
  1844.       make_resource_name_entry:
  1845.         mov     eax,[esp]
  1846.         inc     word [eax+12]
  1847.         lea     eax,[edi+8]
  1848.         cmp     eax,[display_buffer]
  1849.         jae     out_of_memory
  1850.         mov     eax,ebx
  1851.         stos    dword [edi]
  1852.         xor     eax,eax
  1853.         stos    dword [edi]
  1854.         jmp     make_resource_name_directory
  1855.       resource_name_directory_done:
  1856.         mov     ebx,-1
  1857.       make_resource_id_directory:
  1858.         mov     esi,[free_additional_memory]
  1859.         mov     edx,10000h
  1860.       find_resource_id:
  1861.         cmp     dword [esi],0
  1862.         je      resource_id_ok
  1863.         push    esi
  1864.         cmp     [esi+4],ebp
  1865.         jne     check_next_resource_id
  1866.         add     esi,20
  1867.         call    skip_resource_name
  1868.         cmp     word [esi],0FFFFh
  1869.         jne     check_next_resource_id
  1870.         movzx   eax,word [esi+2]
  1871.         cmp     eax,ebx
  1872.         jle     check_next_resource_id
  1873.         cmp     eax,edx
  1874.         jg      check_next_resource_id
  1875.         mov     edx,eax
  1876.         mov     eax,[esp]
  1877.         mov     [eax+8],edi
  1878.       check_next_resource_id:
  1879.         pop     esi
  1880.         mov     eax,[esi+16]
  1881.         lea     esi,[esi+20+eax]
  1882.         jmp     find_resource_id
  1883.       resource_id_ok:
  1884.         cmp     edx,10000h
  1885.         je      resource_id_directory_done
  1886.         mov     ebx,edx
  1887.       make_resource_id_entry:
  1888.         mov     eax,[esp]
  1889.         inc     word [eax+14]
  1890.         lea     eax,[edi+8]
  1891.         cmp     eax,[display_buffer]
  1892.         jae     out_of_memory
  1893.         mov     eax,ebx
  1894.         stos    dword [edi]
  1895.         xor     eax,eax
  1896.         stos    dword [edi]
  1897.         jmp     make_resource_id_directory
  1898.       resource_id_directory_done:
  1899.         pop     eax
  1900.         mov     esi,ebp
  1901.         pop     ecx
  1902.         add     esi,8
  1903.         dec     cx
  1904.         jnz     make_resource_directories
  1905.       resource_directories_ok:
  1906.         shr     ecx,16
  1907.         jnz     make_resource_directories
  1908.         mov     esi,[resource_data]
  1909.         add     esi,10h
  1910.         movzx   eax,word [esi-4]
  1911.         movzx   edx,word [esi-2]
  1912.         add     eax,edx
  1913.         lea     esi,[esi+eax*8]
  1914.         push    edi                     ; address of language directories
  1915.       update_resource_directories:
  1916.         cmp     esi,[esp]
  1917.         je      resource_directories_updated
  1918.         add     esi,10h
  1919.         mov     ecx,[esi-4]
  1920.         or      cx,cx
  1921.         jz      language_directories_ok
  1922.       make_language_directories:
  1923.         push    ecx
  1924.         push    edi
  1925.         mov     edx,edi
  1926.         sub     edx,[resource_data]
  1927.         bts     edx,31
  1928.         mov     [esi+4],edx
  1929.         lea     eax,[edi+16]
  1930.         cmp     eax,[display_buffer]
  1931.         jae     out_of_memory
  1932.         xor     eax,eax
  1933.         stos    dword [edi]
  1934.         call    make_timestamp
  1935.         stos    dword [edi]
  1936.         xor     eax,eax
  1937.         stos    dword [edi]
  1938.         stos    dword [edi]
  1939.         mov     ebp,esi
  1940.         mov     ebx,-1
  1941.       make_language_id_directory:
  1942.         mov     esi,[free_additional_memory]
  1943.         mov     edx,10000h
  1944.       find_language_id:
  1945.         cmp     dword [esi],0
  1946.         je      language_id_ok
  1947.         push    esi
  1948.         cmp     [esi+8],ebp
  1949.         jne     check_next_language_id
  1950.         add     esi,20
  1951.         mov     eax,esi
  1952.         call    skip_resource_name
  1953.         call    skip_resource_name
  1954.         neg     eax
  1955.         add     eax,esi
  1956.         and     eax,11b
  1957.         add     esi,eax
  1958.       get_language_id:
  1959.         movzx   eax,word [esi+6]
  1960.         cmp     eax,ebx
  1961.         jle     check_next_language_id
  1962.         cmp     eax,edx
  1963.         jge     check_next_language_id
  1964.         mov     edx,eax
  1965.         mov     eax,[esp]
  1966.         mov     [current_offset],eax
  1967.       check_next_language_id:
  1968.         pop     esi
  1969.         mov     eax,[esi+16]
  1970.         lea     esi,[esi+20+eax]
  1971.         jmp     find_language_id
  1972.       language_id_ok:
  1973.         cmp     edx,10000h
  1974.         je      language_id_directory_done
  1975.         mov     ebx,edx
  1976.       make_language_id_entry:
  1977.         mov     eax,[esp]
  1978.         inc     word [eax+14]
  1979.         lea     eax,[edi+8]
  1980.         cmp     eax,[display_buffer]
  1981.         jae     out_of_memory
  1982.         mov     eax,ebx
  1983.         stos    dword [edi]
  1984.         mov     eax,[current_offset]
  1985.         stos    dword [edi]
  1986.         jmp     make_language_id_directory
  1987.       language_id_directory_done:
  1988.         pop     eax
  1989.         mov     esi,ebp
  1990.         pop     ecx
  1991.         add     esi,8
  1992.         dec     cx
  1993.         jnz     make_language_directories
  1994.       language_directories_ok:
  1995.         shr     ecx,16
  1996.         jnz     make_language_directories
  1997.         jmp     update_resource_directories
  1998.       resource_directories_updated:
  1999.         mov     esi,[resource_data]
  2000.         push    edi
  2001.       make_name_strings:
  2002.         add     esi,10h
  2003.         movzx   eax,word [esi-2]
  2004.         movzx   ecx,word [esi-4]
  2005.         add     eax,ecx
  2006.         lea     eax,[esi+eax*8]
  2007.         push    eax
  2008.         or      ecx,ecx
  2009.         jz      string_entries_processed
  2010.       process_string_entries:
  2011.         push    ecx
  2012.         mov     edx,edi
  2013.         sub     edx,[resource_data]
  2014.         bts     edx,31
  2015.         xchg    [esi],edx
  2016.         mov     ebx,edi
  2017.         xor     ax,ax
  2018.         stos    word [edi]
  2019.       copy_string_data:
  2020.         lea     eax,[edi+2]
  2021.         cmp     eax,[display_buffer]
  2022.         jae     out_of_memory
  2023.         mov     ax,[edx]
  2024.         or      ax,ax
  2025.         jz      string_data_copied
  2026.         stos    word [edi]
  2027.         inc     word [ebx]
  2028.         add     edx,2
  2029.         jmp     copy_string_data
  2030.       string_data_copied:
  2031.         add     esi,8
  2032.         pop     ecx
  2033.         loop    process_string_entries
  2034.       string_entries_processed:
  2035.         pop     esi
  2036.         cmp     esi,[esp]
  2037.         jb      make_name_strings
  2038.         mov     eax,edi
  2039.         sub     eax,[resource_data]
  2040.         test    al,11b
  2041.         jz      resource_strings_alignment_ok
  2042.         xor     ax,ax
  2043.         stos    word [edi]
  2044.       resource_strings_alignment_ok:
  2045.         pop     edx
  2046.         pop     ebx                     ; address of language directories
  2047.         mov     ebp,edi
  2048.       update_language_directories:
  2049.         add     ebx,10h
  2050.         movzx   eax,word [ebx-2]
  2051.         movzx   ecx,word [ebx-4]
  2052.         add     ecx,eax
  2053.       make_data_records:
  2054.         push    ecx
  2055.         mov     esi,edi
  2056.         sub     esi,[resource_data]
  2057.         xchg    esi,[ebx+4]
  2058.         lea     eax,[edi+16]
  2059.         cmp     eax,[display_buffer]
  2060.         jae     out_of_memory
  2061.         mov     eax,esi
  2062.         stos    dword [edi]
  2063.         mov     eax,[esi+12]
  2064.         stos    dword [edi]
  2065.         xor     eax,eax
  2066.         stos    dword [edi]
  2067.         stos    dword [edi]
  2068.         pop     ecx
  2069.         add     ebx,8
  2070.         loop    make_data_records
  2071.         cmp     ebx,edx
  2072.         jb      update_language_directories
  2073.         pop     ebx                     ; file handle
  2074.         mov     esi,ebp
  2075.         mov     ebp,edi
  2076.       update_data_records:
  2077.         push    ebp
  2078.         mov     ecx,edi
  2079.         mov     eax,[current_section]
  2080.         sub     ecx,[eax+14h]
  2081.         add     ecx,[eax+0Ch]
  2082.         xchg    ecx,[esi]
  2083.         mov     edx,[ecx]
  2084.         xor     al,al
  2085.         call    lseek
  2086.         mov     edx,edi
  2087.         mov     ecx,[esi+4]
  2088.         add     edi,ecx
  2089.         cmp     edi,[display_buffer]
  2090.         ja      out_of_memory
  2091.         call    read
  2092.         mov     eax,edi
  2093.         sub     eax,[resource_data]
  2094.         and     eax,11b
  2095.         jz      resource_data_alignment_ok
  2096.         mov     ecx,4
  2097.         sub     ecx,eax
  2098.         xor     al,al
  2099.         rep     stos byte [edi]
  2100.       resource_data_alignment_ok:
  2101.         pop     ebp
  2102.         add     esi,16
  2103.         cmp     esi,ebp
  2104.         jb      update_data_records
  2105.         pop     esi
  2106.         call    close
  2107.         mov     eax,edi
  2108.         sub     eax,[resource_data]
  2109.         mov     [resource_size],eax
  2110.       resource_done:
  2111.         ret
  2112. close_pe:
  2113.         call    close_pe_section
  2114.         mov     edx,[code_start]
  2115.         mov     [edx+50h],eax
  2116.         call    make_timestamp
  2117.         mov     edx,[code_start]
  2118.         mov     [edx+8],eax
  2119.         mov     eax,[number_of_relocations]
  2120.         cmp     eax,-1
  2121.         je      pe_relocations_ok
  2122.         shl     eax,2
  2123.         sub     [free_additional_memory],eax
  2124.         btr     [resolver_flags],0
  2125.         jnc     pe_relocations_ok
  2126.         or      [next_pass_needed],-1
  2127.       pe_relocations_ok:
  2128.         mov     eax,[number_of_sections]
  2129.         mov     [edx+6],ax
  2130.         imul    eax,28h
  2131.         movzx   ecx,word [edx+14h]
  2132.         lea     eax,[eax+18h+ecx]
  2133.         add     eax,[stub_size]
  2134.         mov     ecx,[edx+3Ch]
  2135.         dec     ecx
  2136.         add     eax,ecx
  2137.         not     ecx
  2138.         and     eax,ecx
  2139.         cmp     eax,[edx+54h]
  2140.         je      pe_sections_ok
  2141.         or      [next_pass_needed],-1
  2142.       pe_sections_ok:
  2143.         xor     ecx,ecx
  2144.         add     edx,78h
  2145.         test    [format_flags],8
  2146.         jz      process_directories
  2147.         add     edx,10h
  2148.       process_directories:
  2149.         mov     eax,[edx+ecx*8]
  2150.         or      eax,eax
  2151.         jz      directory_ok
  2152.         cmp     dword [edx+ecx*8+4],-1
  2153.         jne     directory_ok
  2154.       section_data:
  2155.         mov     ebx,[edx+ecx*8]
  2156.         mov     eax,[ebx+0Ch]
  2157.         mov     [edx+ecx*8],eax         ; directory rva
  2158.         mov     eax,[ebx+8]
  2159.         mov     [edx+ecx*8+4],eax       ; directory size
  2160.       directory_ok:
  2161.         inc     cl
  2162.         cmp     cl,10h
  2163.         jb      process_directories
  2164.         mov     ebx,[code_start]
  2165.         sub     ebx,[stub_size]
  2166.         mov     ecx,edi
  2167.         sub     ecx,ebx
  2168.         mov     ebp,ecx
  2169.         shr     ecx,1
  2170.         xor     eax,eax
  2171.         cdq
  2172.       calculate_checksum:
  2173.         mov     dx,[ebx]
  2174.         add     eax,edx
  2175.         mov     dx,ax
  2176.         shr     eax,16
  2177.         add     eax,edx
  2178.         add     ebx,2
  2179.         loop    calculate_checksum
  2180.         add     eax,ebp
  2181.         mov     ebx,[code_start]
  2182.         mov     [ebx+58h],eax
  2183.         ret
  2184.  
  2185. format_coff:
  2186.         mov     eax,[additional_memory]
  2187.         mov     [symbols_stream],eax
  2188.         mov     ebx,eax
  2189.         add     eax,20h
  2190.         cmp     eax,[structures_buffer]
  2191.         jae     out_of_memory
  2192.         mov     [free_additional_memory],eax
  2193.         xor     eax,eax
  2194.         mov     [ebx],al
  2195.         mov     [ebx+4],eax
  2196.         mov     [ebx+8],edi
  2197.         mov     al,4
  2198.         mov     [ebx+10h],eax
  2199.         mov     al,60h
  2200.         bt      [format_flags],0
  2201.         jnc     flat_section_flags_ok
  2202.         or      eax,0E0000000h
  2203.       flat_section_flags_ok:
  2204.         mov     dword [ebx+14h],eax
  2205.         mov     [current_section],ebx
  2206.         mov     [number_of_sections],0
  2207.         mov     dword [org_origin],edi
  2208.         mov     dword [org_origin+4],0
  2209.         mov     [org_registers],0
  2210.         mov     [org_start],edi
  2211.         mov     [org_symbol],ebx
  2212.         mov     [labels_type],2
  2213.         mov     [code_type],32
  2214.         test    [format_flags],8
  2215.         jz      format_defined
  2216.         mov     [labels_type],4
  2217.         mov     [code_type],64
  2218.         jmp     format_defined
  2219. coff_section:
  2220.         call    close_coff_section
  2221.         mov     ebx,[free_additional_memory]
  2222.         lea     eax,[ebx+20h]
  2223.         cmp     eax,[structures_buffer]
  2224.         jae     out_of_memory
  2225.         mov     [free_additional_memory],eax
  2226.         mov     [current_section],ebx
  2227.         inc     [number_of_sections]
  2228.         xor     eax,eax
  2229.         mov     [ebx],al
  2230.         mov     [ebx+8],edi
  2231.         mov     dword [org_origin],edi
  2232.         mov     dword [org_origin+4],0
  2233.         mov     [org_registers],0
  2234.         mov     [org_start],edi
  2235.         mov     [org_symbol],ebx
  2236.         mov     [labels_type],2
  2237.         test    [format_flags],8
  2238.         jz      coff_labels_type_ok
  2239.         mov     [labels_type],4
  2240.       coff_labels_type_ok:
  2241.         mov     [ebx+10h],eax
  2242.         mov     [ebx+14h],eax
  2243.         lods    word [esi]
  2244.         cmp     ax,'('
  2245.         jne     invalid_argument
  2246.         mov     [ebx+4],esi
  2247.         mov     ecx,[esi]
  2248.         lea     esi,[esi+4+ecx+1]
  2249.         cmp     ecx,8
  2250.         ja      name_too_long
  2251.       coff_section_flags:
  2252.         cmp     byte [esi],1Ch
  2253.         je      coff_section_alignment
  2254.         cmp     byte [esi],19h
  2255.         jne     coff_section_settings_ok
  2256.         inc     esi
  2257.         lods    byte [esi]
  2258.         bt      [format_flags],0
  2259.         jc      coff_section_flag_ok
  2260.         cmp     al,7
  2261.         ja      invalid_argument
  2262.       coff_section_flag_ok:
  2263.         mov     cl,al
  2264.         mov     eax,1
  2265.         shl     eax,cl
  2266.         test    dword [ebx+14h],eax
  2267.         jnz     setting_already_specified
  2268.         or      dword [ebx+14h],eax
  2269.         jmp     coff_section_flags
  2270.       coff_section_alignment:
  2271.         bt      [format_flags],0
  2272.         jnc     invalid_argument
  2273.         inc     esi
  2274.         lods    byte [esi]
  2275.         or      al,al
  2276.         jnz     invalid_argument
  2277.         lods    byte [esi]
  2278.         cmp     al,'('
  2279.         jne     invalid_argument
  2280.         cmp     byte [esi],'.'
  2281.         je      invalid_value
  2282.         push    ebx
  2283.         call    get_dword_value
  2284.         pop     ebx
  2285.         cmp     [value_type],0
  2286.         jne     invalid_use_of_symbol
  2287.         mov     edx,eax
  2288.         dec     edx
  2289.         test    eax,edx
  2290.         jnz     invalid_value
  2291.         or      eax,eax
  2292.         jz      invalid_value
  2293.         cmp     eax,2000h
  2294.         ja      invalid_value
  2295.         bsf     edx,eax
  2296.         inc     edx
  2297.         shl     edx,20
  2298.         or      [ebx+14h],edx
  2299.         xchg    [ebx+10h],eax
  2300.         or      eax,eax
  2301.         jnz     setting_already_specified
  2302.         jmp     coff_section_flags
  2303.       coff_section_settings_ok:
  2304.         cmp     dword [ebx+10h],0
  2305.         jne     instruction_assembled
  2306.         mov     dword [ebx+10h],4
  2307.         bt      [format_flags],0
  2308.         jnc     instruction_assembled
  2309.         or      dword [ebx+14h],300000h
  2310.         jmp     instruction_assembled
  2311.       close_coff_section:
  2312.         mov     ebx,[current_section]
  2313.         mov     eax,edi
  2314.         mov     edx,[ebx+8]
  2315.         sub     eax,edx
  2316.         mov     [ebx+0Ch],eax
  2317.         xor     eax,eax
  2318.         xchg    [undefined_data_end],eax
  2319.         cmp     eax,edi
  2320.         jne     coff_section_ok
  2321.         cmp     edx,[undefined_data_start]
  2322.         jne     coff_section_ok
  2323.         mov     edi,edx
  2324.         or      byte [ebx+14h],80h
  2325.       coff_section_ok:
  2326.         ret
  2327. mark_coff_relocation:
  2328.         cmp     [value_type],3
  2329.         je      coff_relocation_relative
  2330.         push    ebx eax
  2331.         test    [format_flags],8
  2332.         jnz     coff_64bit_relocation
  2333.         mov     al,6
  2334.         jmp     coff_relocation
  2335.       coff_64bit_relocation:
  2336.         mov     al,1
  2337.         cmp     [value_type],4
  2338.         je      coff_relocation
  2339.         mov     al,2
  2340.         jmp     coff_relocation
  2341.       coff_relocation_relative:
  2342.         push    ebx
  2343.         bt      [format_flags],0
  2344.         jnc     relative_ok
  2345.         mov     ebx,[current_section]
  2346.         mov     ebx,[ebx+8]
  2347.         sub     ebx,edi
  2348.         sub     eax,ebx
  2349.         add     eax,4
  2350.       relative_ok:
  2351.         push    eax
  2352.         mov     al,20
  2353.         test    [format_flags],8
  2354.         jnz     relative_coff_64bit_relocation
  2355.         cmp     [labels_type],2
  2356.         jne     invalid_use_of_symbol
  2357.         jmp     coff_relocation
  2358.       relative_coff_64bit_relocation:
  2359.         mov     al,4
  2360.         cmp     [labels_type],4
  2361.         jne     invalid_use_of_symbol
  2362.       coff_relocation:
  2363.         mov     ebx,[free_additional_memory]
  2364.         add     ebx,0Ch
  2365.         cmp     ebx,[structures_buffer]
  2366.         jae     out_of_memory
  2367.         mov     [free_additional_memory],ebx
  2368.         mov     byte [ebx-0Ch],al
  2369.         mov     eax,[current_section]
  2370.         mov     eax,[eax+8]
  2371.         neg     eax
  2372.         add     eax,edi
  2373.         mov     [ebx-0Ch+4],eax
  2374.         mov     eax,[symbol_identifier]
  2375.         mov     [ebx-0Ch+8],eax
  2376.         pop     eax ebx
  2377.         ret
  2378. close_coff:
  2379.         call    close_coff_section
  2380.         cmp     [next_pass_needed],0
  2381.         je      coff_closed
  2382.         mov     eax,[symbols_stream]
  2383.         mov     [free_additional_memory],eax
  2384.       coff_closed:
  2385.         ret
  2386. coff_formatter:
  2387.         sub     edi,[code_start]
  2388.         mov     [code_size],edi
  2389.         call    prepare_default_section
  2390.         mov     edi,[free_additional_memory]
  2391.         mov     ebx,edi
  2392.         mov     ecx,28h shr 2
  2393.         imul    ecx,[number_of_sections]
  2394.         add     ecx,14h shr 2
  2395.         lea     eax,[edi+ecx*4]
  2396.         cmp     eax,[structures_buffer]
  2397.         jae     out_of_memory
  2398.         xor     eax,eax
  2399.         rep     stos dword [edi]
  2400.         mov     word [ebx],14Ch
  2401.         test    [format_flags],8
  2402.         jz      coff_magic_ok
  2403.         mov     word [ebx],8664h
  2404.       coff_magic_ok:
  2405.         mov     word [ebx+12h],104h
  2406.         bt      [format_flags],0
  2407.         jnc     coff_flags_ok
  2408.         or      byte [ebx+12h],80h
  2409.       coff_flags_ok:
  2410.         push    ebx
  2411.         call    make_timestamp
  2412.         pop     ebx
  2413.         mov     [ebx+4],eax
  2414.         mov     eax,[number_of_sections]
  2415.         mov     [ebx+2],ax
  2416.         mov     esi,[symbols_stream]
  2417.         xor     eax,eax
  2418.         xor     ecx,ecx
  2419.       enumerate_symbols:
  2420.         cmp     esi,[free_additional_memory]
  2421.         je      symbols_enumerated
  2422.         mov     dl,[esi]
  2423.         or      dl,dl
  2424.         jz      enumerate_section
  2425.         cmp     dl,0C0h
  2426.         jae     enumerate_public
  2427.         cmp     dl,80h
  2428.         jae     enumerate_extrn
  2429.         add     esi,0Ch
  2430.         jmp     enumerate_symbols
  2431.       enumerate_section:
  2432.         mov     edx,eax
  2433.         shl     edx,8
  2434.         mov     [esi],edx
  2435.         inc     eax
  2436.         inc     ecx
  2437.         mov     [esi+1Eh],cx
  2438.         add     esi,20h
  2439.         jmp     enumerate_symbols
  2440.       enumerate_public:
  2441.         mov     edx,eax
  2442.         shl     edx,8
  2443.         mov     dl,[esi]
  2444.         mov     [esi],edx
  2445.         mov     edx,[esi+8]
  2446.         add     esi,10h
  2447.         inc     eax
  2448.         cmp     byte [edx+11],2
  2449.         jne     enumerate_symbols
  2450.         mov     edx,[edx+20]
  2451.         cmp     byte [edx],0C0h
  2452.         jae     enumerate_symbols
  2453.         cmp     byte [edx],80h
  2454.         jb      enumerate_symbols
  2455.         inc     eax
  2456.         jmp     enumerate_symbols
  2457.       enumerate_extrn:
  2458.         mov     edx,eax
  2459.         shl     edx,8
  2460.         mov     dl,[esi]
  2461.         mov     [esi],edx
  2462.         add     esi,0Ch
  2463.         inc     eax
  2464.         jmp     enumerate_symbols
  2465.       prepare_default_section:
  2466.         mov     ebx,[symbols_stream]
  2467.         cmp     dword [ebx+0Ch],0
  2468.         jne     default_section_ok
  2469.         cmp     [number_of_sections],0
  2470.         je      default_section_ok
  2471.         mov     edx,ebx
  2472.       find_references_to_default_section:
  2473.         cmp     ebx,[free_additional_memory]
  2474.         jne     check_reference
  2475.         add     [symbols_stream],20h
  2476.         ret
  2477.       check_reference:
  2478.         mov     al,[ebx]
  2479.         or      al,al
  2480.         jz      skip_other_section
  2481.         cmp     al,0C0h
  2482.         jae     check_public_reference
  2483.         cmp     al,80h
  2484.         jae     next_reference
  2485.         cmp     edx,[ebx+8]
  2486.         je      default_section_ok
  2487.       next_reference:
  2488.         add     ebx,0Ch
  2489.         jmp     find_references_to_default_section
  2490.       check_public_reference:
  2491.         mov     eax,[ebx+8]
  2492.         add     ebx,10h
  2493.         test    byte [eax+8],1
  2494.         jz      find_references_to_default_section
  2495.         mov     cx,[current_pass]
  2496.         cmp     cx,[eax+16]
  2497.         jne     find_references_to_default_section
  2498.         cmp     edx,[eax+20]
  2499.         je      default_section_ok
  2500.         jmp     find_references_to_default_section
  2501.       skip_other_section:
  2502.         add     ebx,20h
  2503.         jmp     find_references_to_default_section
  2504.       default_section_ok:
  2505.         inc     [number_of_sections]
  2506.         ret
  2507.       symbols_enumerated:
  2508.         mov     [ebx+0Ch],eax
  2509.         mov     ebp,edi
  2510.         sub     ebp,ebx
  2511.         push    ebp
  2512.         lea     edi,[ebx+14h]
  2513.         mov     esi,[symbols_stream]
  2514.       find_section:
  2515.         cmp     esi,[free_additional_memory]
  2516.         je      sections_finished
  2517.         mov     al,[esi]
  2518.         or      al,al
  2519.         jz      section_found
  2520.         add     esi,0Ch
  2521.         cmp     al,0C0h
  2522.         jb      find_section
  2523.         add     esi,4
  2524.         jmp     find_section
  2525.       section_found:
  2526.         push    esi edi
  2527.         mov     esi,[esi+4]
  2528.         or      esi,esi
  2529.         jz      default_section
  2530.         mov     ecx,[esi]
  2531.         add     esi,4
  2532.         rep     movs byte [edi],[esi]
  2533.         jmp     section_name_ok
  2534.       default_section:
  2535.         mov     al,'.'
  2536.         stos    byte [edi]
  2537.         mov     eax,'flat'
  2538.         stos    dword [edi]
  2539.       section_name_ok:
  2540.         pop     edi esi
  2541.         mov     eax,[esi+0Ch]
  2542.         mov     [edi+10h],eax
  2543.         mov     eax,[esi+14h]
  2544.         mov     [edi+24h],eax
  2545.         test    al,80h
  2546.         jnz     section_ptr_ok
  2547.         mov     eax,[esi+8]
  2548.         sub     eax,[code_start]
  2549.         add     eax,ebp
  2550.         mov     [edi+14h],eax
  2551.       section_ptr_ok:
  2552.         mov     ebx,[code_start]
  2553.         mov     edx,[code_size]
  2554.         add     ebx,edx
  2555.         add     edx,ebp
  2556.         xor     ecx,ecx
  2557.         add     esi,20h
  2558.       find_relocations:
  2559.         cmp     esi,[free_additional_memory]
  2560.         je      section_relocations_done
  2561.         mov     al,[esi]
  2562.         or      al,al
  2563.         jz      section_relocations_done
  2564.         cmp     al,80h
  2565.         jb      add_relocation
  2566.         cmp     al,0C0h
  2567.         jb      next_relocation
  2568.         add     esi,10h
  2569.         jmp     find_relocations
  2570.       add_relocation:
  2571.         lea     eax,[ebx+0Ah]
  2572.         cmp     eax,[display_buffer]
  2573.         ja      out_of_memory
  2574.         mov     eax,[esi+4]
  2575.         mov     [ebx],eax
  2576.         mov     eax,[esi+8]
  2577.         mov     eax,[eax]
  2578.         shr     eax,8
  2579.         mov     [ebx+4],eax
  2580.         movzx   ax,byte [esi]
  2581.         mov     [ebx+8],ax
  2582.         add     ebx,0Ah
  2583.         inc     ecx
  2584.       next_relocation:
  2585.         add     esi,0Ch
  2586.         jmp     find_relocations
  2587.       section_relocations_done:
  2588.         cmp     ecx,10000h
  2589.         jb      section_relocations_count_16bit
  2590.         bt      [format_flags],0
  2591.         jnc     format_limitations_exceeded
  2592.         mov     word [edi+20h],0FFFFh
  2593.         or      dword [edi+24h],1000000h
  2594.         mov     [edi+18h],edx
  2595.         push    esi edi
  2596.         push    ecx
  2597.         lea     esi,[ebx-1]
  2598.         add     ebx,0Ah
  2599.         lea     edi,[ebx-1]
  2600.         imul    ecx,0Ah
  2601.         std
  2602.         rep     movs byte [edi],[esi]
  2603.         cld
  2604.         pop     ecx
  2605.         inc     esi
  2606.         inc     ecx
  2607.         mov     [esi],ecx
  2608.         xor     eax,eax
  2609.         mov     [esi+4],eax
  2610.         mov     [esi+8],ax
  2611.         pop     edi esi
  2612.         jmp     section_relocations_ok
  2613.       section_relocations_count_16bit:
  2614.         mov     [edi+20h],cx
  2615.         jcxz    section_relocations_ok
  2616.         mov     [edi+18h],edx
  2617.       section_relocations_ok:
  2618.         sub     ebx,[code_start]
  2619.         mov     [code_size],ebx
  2620.         add     edi,28h
  2621.         jmp     find_section
  2622.       sections_finished:
  2623.         mov     edx,[free_additional_memory]
  2624.         mov     ebx,[code_size]
  2625.         add     ebp,ebx
  2626.         mov     [edx+8],ebp
  2627.         add     ebx,[code_start]
  2628.         mov     edi,ebx
  2629.         mov     ecx,[edx+0Ch]
  2630.         imul    ecx,12h shr 1
  2631.         xor     eax,eax
  2632.         shr     ecx,1
  2633.         jnc     zero_symbols_table
  2634.         stos    word [edi]
  2635.       zero_symbols_table:
  2636.         rep     stos dword [edi]
  2637.         mov     edx,edi
  2638.         stos    dword [edi]
  2639.         mov     esi,[symbols_stream]
  2640.       make_symbols_table:
  2641.         cmp     esi,[free_additional_memory]
  2642.         je      symbols_table_ok
  2643.         mov     al,[esi]
  2644.         cmp     al,0C0h
  2645.         jae     add_public_symbol
  2646.         cmp     al,80h
  2647.         jae     add_extrn_symbol
  2648.         or      al,al
  2649.         jz      add_section_symbol
  2650.         add     esi,0Ch
  2651.         jmp     make_symbols_table
  2652.       add_section_symbol:
  2653.         call    store_symbol_name
  2654.         movzx   eax,word [esi+1Eh]
  2655.         mov     [ebx+0Ch],ax
  2656.         mov     byte [ebx+10h],3
  2657.         add     esi,20h
  2658.         add     ebx,12h
  2659.         jmp     make_symbols_table
  2660.       add_extrn_symbol:
  2661.         call    store_symbol_name
  2662.         mov     byte [ebx+10h],2
  2663.         add     esi,0Ch
  2664.         add     ebx,12h
  2665.         jmp     make_symbols_table
  2666.       add_public_symbol:
  2667.         call    store_symbol_name
  2668.         mov     eax,[esi+0Ch]
  2669.         mov     [current_line],eax
  2670.         mov     eax,[esi+8]
  2671.         test    byte [eax+8],1
  2672.         jz      undefined_coff_public
  2673.         mov     cx,[current_pass]
  2674.         cmp     cx,[eax+16]
  2675.         jne     undefined_coff_public
  2676.         mov     cl,[eax+11]
  2677.         or      cl,cl
  2678.         jz      public_constant
  2679.         test    [format_flags],8
  2680.         jnz     check_64bit_public_symbol
  2681.         cmp     cl,2
  2682.         je      public_symbol_type_ok
  2683.         jmp     invalid_use_of_symbol
  2684.       undefined_coff_public:
  2685.         mov     eax,[eax+24]
  2686.         mov     [error_info],eax
  2687.         jmp     undefined_symbol
  2688.       check_64bit_public_symbol:
  2689.         cmp     cl,4
  2690.         jne     invalid_use_of_symbol
  2691.       public_symbol_type_ok:
  2692.         mov     ecx,[eax+20]
  2693.         cmp     byte [ecx],80h
  2694.         je      alias_symbol
  2695.         cmp     byte [ecx],0
  2696.         jne     invalid_use_of_symbol
  2697.         mov     cx,[ecx+1Eh]
  2698.         mov     [ebx+0Ch],cx
  2699.       public_symbol_section_ok:
  2700.         cmp     dword [eax+4],0
  2701.         je      store_public_symbol
  2702.         cmp     dword [eax+4],-1
  2703.         jne     value_out_of_range
  2704.         bt      dword [eax],31
  2705.         jnc     value_out_of_range
  2706.       store_public_symbol:
  2707.         mov     eax,[eax]
  2708.         mov     [ebx+8],eax
  2709.         mov     al,2
  2710.         cmp     byte [esi],0C0h
  2711.         je      store_symbol_class
  2712.         inc     al
  2713.         cmp     byte [esi],0C1h
  2714.         je      store_symbol_class
  2715.         mov     al,105
  2716.       store_symbol_class:
  2717.         mov     byte [ebx+10h],al
  2718.         add     esi,10h
  2719.         add     ebx,12h
  2720.         jmp     make_symbols_table
  2721.       alias_symbol:
  2722.         bt      [format_flags],0
  2723.         jnc     invalid_use_of_symbol
  2724.         mov     ecx,[eax]
  2725.         or      ecx,[eax+4]
  2726.         jnz     invalid_use_of_symbol
  2727.         mov     byte [ebx+10h],69h
  2728.         mov     byte [ebx+11h],1
  2729.         add     ebx,12h
  2730.         mov     ecx,[eax+20]
  2731.         mov     ecx,[ecx]
  2732.         shr     ecx,8
  2733.         mov     [ebx],ecx
  2734.         mov     byte [ebx+4],3
  2735.         add     esi,10h
  2736.         add     ebx,12h
  2737.         jmp     make_symbols_table
  2738.       public_constant:
  2739.         mov     word [ebx+0Ch],0FFFFh
  2740.         jmp     public_symbol_section_ok
  2741.       symbols_table_ok:
  2742.         mov     eax,edi
  2743.         sub     eax,edx
  2744.         mov     [edx],eax
  2745.         sub     edi,[code_start]
  2746.         mov     [code_size],edi
  2747.         mov     [written_size],0
  2748.         mov     edx,[output_file]
  2749.         call    create
  2750.         jc      write_failed
  2751.         mov     edx,[free_additional_memory]
  2752.         pop     ecx
  2753.         add     [written_size],ecx
  2754.         call    write
  2755.         jc      write_failed
  2756.         jmp     write_output
  2757.       store_symbol_name:
  2758.         push    esi
  2759.         mov     esi,[esi+4]
  2760.         or      esi,esi
  2761.         jz      default_name
  2762.         lods    dword [esi]
  2763.         mov     ecx,eax
  2764.         cmp     ecx,8
  2765.         ja      add_string
  2766.         push    edi
  2767.         mov     edi,ebx
  2768.         rep     movs byte [edi],[esi]
  2769.         pop     edi esi
  2770.         ret
  2771.       default_name:
  2772.         mov     dword [ebx],'.fla'
  2773.         mov     dword [ebx+4],'t'
  2774.         pop     esi
  2775.         ret
  2776.       add_string:
  2777.         mov     eax,edi
  2778.         sub     eax,edx
  2779.         mov     [ebx+4],eax
  2780.         inc     ecx
  2781.         rep     movs byte [edi],[esi]
  2782.         pop     esi
  2783.         ret
  2784.  
  2785. format_elf:
  2786.         test    [format_flags],8
  2787.         jnz     format_elf64
  2788.         mov     edx,edi
  2789.         mov     ecx,34h shr 2
  2790.         lea     eax,[edi+ecx*4]
  2791.         cmp     eax,[display_buffer]
  2792.         jae     out_of_memory
  2793.         xor     eax,eax
  2794.         rep     stos dword [edi]
  2795.         mov     dword [edx],7Fh + 'ELF' shl 8
  2796.         mov     al,1
  2797.         mov     [edx+4],al
  2798.         mov     [edx+5],al
  2799.         mov     [edx+6],al
  2800.         mov     [edx+14h],al
  2801.         mov     byte [edx+12h],3
  2802.         mov     byte [edx+28h],34h
  2803.         mov     byte [edx+2Eh],28h
  2804.         mov     [code_type],32
  2805.         cmp     word [esi],1D19h
  2806.         je      format_elf_exe
  2807.         mov     [labels_type],2
  2808.       elf_header_ok:
  2809.         mov     byte [edx+10h],1
  2810.         mov     eax,[additional_memory]
  2811.         mov     [symbols_stream],eax
  2812.         mov     ebx,eax
  2813.         add     eax,20h
  2814.         cmp     eax,[structures_buffer]
  2815.         jae     out_of_memory
  2816.         mov     [free_additional_memory],eax
  2817.         xor     eax,eax
  2818.         mov     [current_section],ebx
  2819.         mov     [number_of_sections],eax
  2820.         mov     dword [org_origin],edi
  2821.         mov     dword [org_origin+4],eax
  2822.         mov     [org_registers],eax
  2823.         mov     [org_start],edi
  2824.         mov     [org_symbol],ebx
  2825.         mov     [ebx],al
  2826.         mov     [ebx+4],eax
  2827.         mov     [ebx+8],edi
  2828.         mov     al,111b
  2829.         mov     [ebx+14h],eax
  2830.         mov     al,4
  2831.         mov     [ebx+10h],eax
  2832.         test    [format_flags],8
  2833.         jz      format_defined
  2834.         mov     byte [ebx+10h],8
  2835.         jmp     format_defined
  2836.       format_elf64:
  2837.         mov     edx,edi
  2838.         mov     ecx,40h shr 2
  2839.         lea     eax,[edi+ecx*4]
  2840.         cmp     eax,[display_buffer]
  2841.         jae     out_of_memory
  2842.         xor     eax,eax
  2843.         rep     stos dword [edi]
  2844.         mov     dword [edx],7Fh + 'ELF' shl 8
  2845.         mov     al,1
  2846.         mov     [edx+5],al
  2847.         mov     [edx+6],al
  2848.         mov     [edx+14h],al
  2849.         mov     byte [edx+4],2
  2850.         mov     byte [edx+12h],62
  2851.         mov     byte [edx+34h],40h
  2852.         mov     byte [edx+3Ah],40h
  2853.         mov     [code_type],64
  2854.         cmp     word [esi],1D19h
  2855.         je      format_elf64_exe
  2856.         mov     [labels_type],4
  2857.         jmp     elf_header_ok
  2858. elf_section:
  2859.         bt      [format_flags],0
  2860.         jc      illegal_instruction
  2861.         call    close_coff_section
  2862.         mov     ebx,[free_additional_memory]
  2863.         lea     eax,[ebx+20h]
  2864.         cmp     eax,[structures_buffer]
  2865.         jae     out_of_memory
  2866.         mov     [free_additional_memory],eax
  2867.         mov     [current_section],ebx
  2868.         inc     word [number_of_sections]
  2869.         jz      format_limitations_exceeded
  2870.         xor     eax,eax
  2871.         mov     [ebx],al
  2872.         mov     [ebx+8],edi
  2873.         mov     dword [org_origin],edi
  2874.         mov     dword [org_origin+4],0
  2875.         mov     [org_registers],0
  2876.         mov     [org_start],edi
  2877.         mov     [org_symbol],ebx
  2878.         test    [format_flags],8
  2879.         jnz     elf64_labels_type
  2880.         mov     [labels_type],2
  2881.         jmp     elf_labels_type_ok
  2882.       elf64_labels_type:
  2883.         mov     [labels_type],4
  2884.       elf_labels_type_ok:
  2885.         mov     [ebx+10h],eax
  2886.         mov     al,10b
  2887.         mov     [ebx+14h],eax
  2888.         lods    word [esi]
  2889.         cmp     ax,'('
  2890.         jne     invalid_argument
  2891.         mov     [ebx+4],esi
  2892.         mov     ecx,[esi]
  2893.         lea     esi,[esi+4+ecx+1]
  2894.       elf_section_flags:
  2895.         cmp     byte [esi],1Ch
  2896.         je      elf_section_alignment
  2897.         cmp     byte [esi],19h
  2898.         jne     elf_section_settings_ok
  2899.         inc     esi
  2900.         lods    byte [esi]
  2901.         sub     al,28
  2902.         xor     al,11b
  2903.         test    al,not 10b
  2904.         jnz     invalid_argument
  2905.         mov     cl,al
  2906.         mov     al,1
  2907.         shl     al,cl
  2908.         test    byte [ebx+14h],al
  2909.         jnz     setting_already_specified
  2910.         or      byte [ebx+14h],al
  2911.         jmp     elf_section_flags
  2912.       elf_section_alignment:
  2913.         inc     esi
  2914.         lods    byte [esi]
  2915.         or      al,al
  2916.         jnz     invalid_argument
  2917.         lods    byte [esi]
  2918.         cmp     al,'('
  2919.         jne     invalid_argument
  2920.         cmp     byte [esi],'.'
  2921.         je      invalid_value
  2922.         push    ebx
  2923.         call    get_dword_value
  2924.         pop     ebx
  2925.         cmp     [value_type],0
  2926.         jne     invalid_use_of_symbol
  2927.         mov     edx,eax
  2928.         dec     edx
  2929.         test    eax,edx
  2930.         jnz     invalid_value
  2931.         or      eax,eax
  2932.         jz      invalid_value
  2933.         xchg    [ebx+10h],eax
  2934.         or      eax,eax
  2935.         jnz     setting_already_specified
  2936.         jmp     elf_section_flags
  2937.       elf_section_settings_ok:
  2938.         cmp     dword [ebx+10h],0
  2939.         jne     instruction_assembled
  2940.         mov     dword [ebx+10h],4
  2941.         test    [format_flags],8
  2942.         jz      instruction_assembled
  2943.         mov     byte [ebx+10h],8
  2944.         jmp     instruction_assembled
  2945. mark_elf_relocation:
  2946.         cmp     [value_type],3
  2947.         je      elf_relocation_relative
  2948.         cmp     [value_type],7
  2949.         je      elf_relocation_relative
  2950.         push    ebx eax
  2951.         cmp     [value_type],5
  2952.         je      elf_gotoff_relocation
  2953.         ja      invalid_use_of_symbol
  2954.         mov     al,1                    ; R_386_32 / R_AMD64_64
  2955.         test    [format_flags],8
  2956.         jz      coff_relocation
  2957.         cmp     [value_type],4
  2958.         je      coff_relocation
  2959.         mov     al,11                   ; R_AMD64_32S
  2960.         jmp     coff_relocation
  2961.       elf_gotoff_relocation:
  2962.         test    [format_flags],8
  2963.         jnz     invalid_use_of_symbol
  2964.         mov     al,9                    ; R_386_GOTOFF
  2965.         jmp     coff_relocation
  2966.       elf_relocation_relative:
  2967.         cmp     [labels_type],0
  2968.         je      invalid_use_of_symbol
  2969.         push    ebx
  2970.         mov     ebx,[current_section]
  2971.         mov     ebx,[ebx+8]
  2972.         sub     ebx,edi
  2973.         sub     eax,ebx
  2974.         push    eax
  2975.         mov     al,2                    ; R_386_PC32 / R_AMD64_PC32
  2976.         cmp     [value_type],3
  2977.         je      coff_relocation
  2978.         mov     al,4                    ; R_386_PLT32 / R_AMD64_PLT32
  2979.         jmp     coff_relocation
  2980. close_elf:
  2981.         bt      [format_flags],0
  2982.         jc      close_elf_exe
  2983.         call    close_coff_section
  2984.         cmp     [next_pass_needed],0
  2985.         je      elf_closed
  2986.         mov     eax,[symbols_stream]
  2987.         mov     [free_additional_memory],eax
  2988.       elf_closed:
  2989.         ret
  2990. elf_formatter:
  2991.         push    edi
  2992.         call    prepare_default_section
  2993.         mov     esi,[symbols_stream]
  2994.         mov     edi,[free_additional_memory]
  2995.         xor     eax,eax
  2996.         mov     ecx,4
  2997.         rep     stos dword [edi]
  2998.         test    [format_flags],8
  2999.         jz      find_first_section
  3000.         mov     ecx,2
  3001.         rep     stos dword [edi]
  3002.       find_first_section:
  3003.         mov     al,[esi]
  3004.         or      al,al
  3005.         jz      first_section_found
  3006.         cmp     al,0C0h
  3007.         jb      skip_other_symbol
  3008.         add     esi,4
  3009.       skip_other_symbol:
  3010.         add     esi,0Ch
  3011.         jmp     find_first_section
  3012.       first_section_found:
  3013.         mov     ebx,esi
  3014.         mov     ebp,esi
  3015.         add     esi,20h
  3016.         xor     ecx,ecx
  3017.         xor     edx,edx
  3018.       find_next_section:
  3019.         cmp     esi,[free_additional_memory]
  3020.         je      make_section_symbol
  3021.         mov     al,[esi]
  3022.         or      al,al
  3023.         jz      make_section_symbol
  3024.         cmp     al,0C0h
  3025.         jae     skip_public
  3026.         cmp     al,80h
  3027.         jae     skip_extrn
  3028.         or      byte [ebx+14h],40h
  3029.       skip_extrn:
  3030.         add     esi,0Ch
  3031.         jmp     find_next_section
  3032.       skip_public:
  3033.         add     esi,10h
  3034.         jmp     find_next_section
  3035.       make_section_symbol:
  3036.         mov     eax,edi
  3037.         xchg    eax,[ebx+4]
  3038.         stos    dword [edi]
  3039.         test    [format_flags],8
  3040.         jnz     elf64_section_symbol
  3041.         xor     eax,eax
  3042.         stos    dword [edi]
  3043.         stos    dword [edi]
  3044.         call    store_section_index
  3045.         jmp     section_symbol_ok
  3046.       store_section_index:
  3047.         inc     ecx
  3048.         mov     eax,ecx
  3049.         shl     eax,8
  3050.         mov     [ebx],eax
  3051.         inc     dx
  3052.         jz      format_limitations_exceeded
  3053.         mov     eax,edx
  3054.         shl     eax,16
  3055.         mov     al,3
  3056.         test    byte [ebx+14h],40h
  3057.         jz      section_index_ok
  3058.         or      ah,-1
  3059.         inc     dx
  3060.         jz      format_limitations_exceeded
  3061.       section_index_ok:
  3062.         stos    dword [edi]
  3063.         ret
  3064.       elf64_section_symbol:
  3065.         call    store_section_index
  3066.         xor     eax,eax
  3067.         stos    dword [edi]
  3068.         stos    dword [edi]
  3069.         stos    dword [edi]
  3070.         stos    dword [edi]
  3071.       section_symbol_ok:
  3072.         mov     ebx,esi
  3073.         add     esi,20h
  3074.         cmp     ebx,[free_additional_memory]
  3075.         jne     find_next_section
  3076.         inc     dx
  3077.         jz      format_limitations_exceeded
  3078.         mov     [current_section],edx
  3079.         mov     esi,[symbols_stream]
  3080.       find_other_symbols:
  3081.         cmp     esi,[free_additional_memory]
  3082.         je      elf_symbol_table_ok
  3083.         mov     al,[esi]
  3084.         or      al,al
  3085.         jz      skip_section
  3086.         cmp     al,0C0h
  3087.         jae     make_public_symbol
  3088.         cmp     al,80h
  3089.         jae     make_extrn_symbol
  3090.         add     esi,0Ch
  3091.         jmp     find_other_symbols
  3092.       skip_section:
  3093.         add     esi,20h
  3094.         jmp     find_other_symbols
  3095.       make_public_symbol:
  3096.         mov     eax,[esi+0Ch]
  3097.         mov     [current_line],eax
  3098.         cmp     byte [esi],0C0h
  3099.         jne     invalid_argument
  3100.         mov     ebx,[esi+8]
  3101.         test    byte [ebx+8],1
  3102.         jz      undefined_public
  3103.         mov     ax,[current_pass]
  3104.         cmp     ax,[ebx+16]
  3105.         jne     undefined_public
  3106.         mov     dl,[ebx+11]
  3107.         or      dl,dl
  3108.         jz      public_absolute
  3109.         mov     eax,[ebx+20]
  3110.         cmp     byte [eax],0
  3111.         jne     invalid_use_of_symbol
  3112.         mov     eax,[eax+4]
  3113.         test    [format_flags],8
  3114.         jnz     elf64_public
  3115.         cmp     dl,2
  3116.         jne     invalid_use_of_symbol
  3117.         mov     dx,[eax+0Eh]
  3118.         jmp     section_for_public_ok
  3119.       undefined_public:
  3120.         mov     eax,[ebx+24]
  3121.         mov     [error_info],eax
  3122.         jmp     undefined_symbol
  3123.       elf64_public:
  3124.         cmp     dl,4
  3125.         jne     invalid_use_of_symbol
  3126.         mov     dx,[eax+6]
  3127.         jmp     section_for_public_ok
  3128.       public_absolute:
  3129.         mov     dx,0FFF1h
  3130.       section_for_public_ok:
  3131.         mov     eax,[esi+4]
  3132.         stos    dword [edi]
  3133.         test    [format_flags],8
  3134.         jnz     elf64_public_symbol
  3135.         call    get_public_value
  3136.         stos    dword [edi]
  3137.         xor     eax,eax
  3138.         mov     al,[ebx+10]
  3139.         stos    dword [edi]
  3140.         mov     eax,edx
  3141.         shl     eax,16
  3142.         mov     al,10h
  3143.         cmp     byte [ebx+10],0
  3144.         je      elf_public_function
  3145.         or      al,1
  3146.         jmp     store_elf_public_info
  3147.       elf_public_function:
  3148.         or      al,2
  3149.       store_elf_public_info:
  3150.         stos    dword [edi]
  3151.         jmp     public_symbol_ok
  3152.       elf64_public_symbol:
  3153.         mov     eax,edx
  3154.         shl     eax,16
  3155.         mov     al,10h
  3156.         cmp     byte [ebx+10],0
  3157.         je      elf64_public_function
  3158.         or      al,1
  3159.         jmp     store_elf64_public_info
  3160.       elf64_public_function:
  3161.         or      al,2
  3162.       store_elf64_public_info:
  3163.         stos    dword [edi]
  3164.         call    get_public_value
  3165.         stos    dword [edi]
  3166.         xor     eax,eax
  3167.         stos    dword [edi]
  3168.         mov     al,[ebx+10]
  3169.         stos    dword [edi]
  3170.         xor     al,al
  3171.         stos    dword [edi]
  3172.       public_symbol_ok:
  3173.         inc     ecx
  3174.         mov     eax,ecx
  3175.         shl     eax,8
  3176.         mov     al,0C0h
  3177.         mov     [esi],eax
  3178.         add     esi,10h
  3179.         jmp     find_other_symbols
  3180.       get_public_value:
  3181.         mov     eax,[ebx]
  3182.         cmp     dword [ebx+4],0
  3183.         je      public_value_ok
  3184.         cmp     dword [ebx+4],-1
  3185.         jne     value_out_of_range
  3186.         bt      eax,31
  3187.         jnc     value_out_of_range
  3188.       public_value_ok:
  3189.         ret
  3190.       make_extrn_symbol:
  3191.         mov     eax,[esi+4]
  3192.         stos    dword [edi]
  3193.         test    [format_flags],8
  3194.         jnz     elf64_extrn_symbol
  3195.         xor     eax,eax
  3196.         stos    dword [edi]
  3197.         mov     eax,[esi+8]
  3198.         stos    dword [edi]
  3199.         mov     eax,10h
  3200.         stos    dword [edi]
  3201.         jmp     extrn_symbol_ok
  3202.       elf64_extrn_symbol:
  3203.         mov     eax,10h
  3204.         stos    dword [edi]
  3205.         xor     al,al
  3206.         stos    dword [edi]
  3207.         stos    dword [edi]
  3208.         mov     eax,[esi+8]
  3209.         stos    dword [edi]
  3210.         xor     eax,eax
  3211.         stos    dword [edi]
  3212.       extrn_symbol_ok:
  3213.         inc     ecx
  3214.         mov     eax,ecx
  3215.         shl     eax,8
  3216.         mov     al,80h
  3217.         mov     [esi],eax
  3218.         add     esi,0Ch
  3219.         jmp     find_other_symbols
  3220.       elf_symbol_table_ok:
  3221.         mov     edx,edi
  3222.         mov     ebx,[free_additional_memory]
  3223.         xor     al,al
  3224.         stos    byte [edi]
  3225.         add     edi,16
  3226.         mov     [edx+1],edx
  3227.         add     ebx,10h
  3228.         test    [format_flags],8
  3229.         jz      make_string_table
  3230.         add     ebx,8
  3231.       make_string_table:
  3232.         cmp     ebx,edx
  3233.         je      elf_string_table_ok
  3234.         test    [format_flags],8
  3235.         jnz     make_elf64_string
  3236.         cmp     byte [ebx+0Dh],0
  3237.         je      rel_prefix_ok
  3238.         mov     byte [ebx+0Dh],0
  3239.         mov     eax,'.rel'
  3240.         stos    dword [edi]
  3241.       rel_prefix_ok:
  3242.         mov     esi,edi
  3243.         sub     esi,edx
  3244.         xchg    esi,[ebx]
  3245.         add     ebx,10h
  3246.       make_elf_string:
  3247.         or      esi,esi
  3248.         jz      default_string
  3249.         lods    dword [esi]
  3250.         mov     ecx,eax
  3251.         rep     movs byte [edi],[esi]
  3252.         xor     al,al
  3253.         stos    byte [edi]
  3254.         jmp     make_string_table
  3255.       make_elf64_string:
  3256.         cmp     byte [ebx+5],0
  3257.         je      elf64_rel_prefix_ok
  3258.         mov     byte [ebx+5],0
  3259.         mov     eax,'.rel'
  3260.         stos    dword [edi]
  3261.         mov     al,'a'
  3262.         stos    byte [edi]
  3263.       elf64_rel_prefix_ok:
  3264.         mov     esi,edi
  3265.         sub     esi,edx
  3266.         xchg    esi,[ebx]
  3267.         add     ebx,18h
  3268.         jmp     make_elf_string
  3269.       default_string:
  3270.         mov     eax,'.fla'
  3271.         stos    dword [edi]
  3272.         mov     ax,'t'
  3273.         stos    word [edi]
  3274.         jmp     make_string_table
  3275.       elf_string_table_ok:
  3276.         mov     [edx+1+8],edi
  3277.         mov     ebx,[code_start]
  3278.         mov     eax,edi
  3279.         sub     eax,[free_additional_memory]
  3280.         test    [format_flags],8
  3281.         jnz     finish_elf64_header
  3282.         mov     [ebx+20h],eax
  3283.         mov     eax,[current_section]
  3284.         inc     ax
  3285.         jz      format_limitations_exceeded
  3286.         mov     [ebx+32h],ax
  3287.         inc     ax
  3288.         jz      format_limitations_exceeded
  3289.         mov     [ebx+30h],ax
  3290.         jmp     elf_header_finished
  3291.       finish_elf64_header:
  3292.         mov     [ebx+28h],eax
  3293.         mov     eax,[current_section]
  3294.         inc     ax
  3295.         jz      format_limitations_exceeded
  3296.         mov     [ebx+3Eh],ax
  3297.         inc     ax
  3298.         jz      format_limitations_exceeded
  3299.         mov     [ebx+3Ch],ax
  3300.       elf_header_finished:
  3301.         xor     eax,eax
  3302.         mov     ecx,10
  3303.         rep     stos dword [edi]
  3304.         test    [format_flags],8
  3305.         jz      elf_null_section_ok
  3306.         mov     ecx,6
  3307.         rep     stos dword [edi]
  3308.       elf_null_section_ok:
  3309.         mov     esi,ebp
  3310.         xor     ecx,ecx
  3311.       make_section_entry:
  3312.         mov     ebx,edi
  3313.         mov     eax,[esi+4]
  3314.         mov     eax,[eax]
  3315.         stos    dword [edi]
  3316.         mov     eax,1
  3317.         cmp     dword [esi+0Ch],0
  3318.         je      bss_section
  3319.         test    byte [esi+14h],80h
  3320.         jz      section_type_ok
  3321.       bss_section:
  3322.         mov     al,8
  3323.       section_type_ok:
  3324.         stos    dword [edi]
  3325.         mov     eax,[esi+14h]
  3326.         and     al,3Fh
  3327.         call    store_elf_machine_word
  3328.         xor     eax,eax
  3329.         call    store_elf_machine_word
  3330.         mov     eax,[esi+8]
  3331.         mov     [image_base],eax
  3332.         sub     eax,[code_start]
  3333.         call    store_elf_machine_word
  3334.         mov     eax,[esi+0Ch]
  3335.         call    store_elf_machine_word
  3336.         xor     eax,eax
  3337.         stos    dword [edi]
  3338.         stos    dword [edi]
  3339.         mov     eax,[esi+10h]
  3340.         call    store_elf_machine_word
  3341.         xor     eax,eax
  3342.         call    store_elf_machine_word
  3343.         inc     ecx
  3344.         add     esi,20h
  3345.         xchg    edi,[esp]
  3346.         mov     ebp,edi
  3347.       convert_relocations:
  3348.         cmp     esi,[free_additional_memory]
  3349.         je      relocations_converted
  3350.         mov     al,[esi]
  3351.         or      al,al
  3352.         jz      relocations_converted
  3353.         cmp     al,80h
  3354.         jb      make_relocation_entry
  3355.         cmp     al,0C0h
  3356.         jb      relocation_entry_ok
  3357.         add     esi,10h
  3358.         jmp     convert_relocations
  3359.       make_relocation_entry:
  3360.         test    [format_flags],8
  3361.         jnz     make_elf64_relocation_entry
  3362.         mov     eax,[esi+4]
  3363.         stos    dword [edi]
  3364.         mov     eax,[esi+8]
  3365.         mov     eax,[eax]
  3366.         mov     al,[esi]
  3367.         stos    dword [edi]
  3368.         jmp     relocation_entry_ok
  3369.       make_elf64_relocation_entry:
  3370.         mov     eax,[esi+4]
  3371.         stos    dword [edi]
  3372.         xor     eax,eax
  3373.         stos    dword [edi]
  3374.         movzx   eax,byte [esi]
  3375.         stos    dword [edi]
  3376.         mov     eax,[esi+8]
  3377.         mov     eax,[eax]
  3378.         shr     eax,8
  3379.         stos    dword [edi]
  3380.         xor     eax,eax
  3381.         stos    dword [edi]
  3382.         stos    dword [edi]
  3383.       relocation_entry_ok:
  3384.         add     esi,0Ch
  3385.         jmp     convert_relocations
  3386.       store_elf_machine_word:
  3387.         stos    dword [edi]
  3388.         test    [format_flags],8
  3389.         jz      elf_machine_word_ok
  3390.         mov     dword [edi],0
  3391.         add     edi,4
  3392.       elf_machine_word_ok:
  3393.         ret
  3394.       relocations_converted:
  3395.         cmp     edi,ebp
  3396.         xchg    edi,[esp]
  3397.         je      rel_section_ok
  3398.         mov     eax,[ebx]
  3399.         sub     eax,4
  3400.         test    [format_flags],8
  3401.         jz      store_relocations_name_offset
  3402.         dec     eax
  3403.       store_relocations_name_offset:
  3404.         stos    dword [edi]
  3405.         test    [format_flags],8
  3406.         jnz     rela_section
  3407.         mov     eax,9
  3408.         jmp     store_relocations_type
  3409.       rela_section:
  3410.         mov     eax,4
  3411.       store_relocations_type:
  3412.         stos    dword [edi]
  3413.         xor     al,al
  3414.         call    store_elf_machine_word
  3415.         call    store_elf_machine_word
  3416.         mov     eax,ebp
  3417.         sub     eax,[code_start]
  3418.         call    store_elf_machine_word
  3419.         mov     eax,[esp]
  3420.         sub     eax,ebp
  3421.         call    store_elf_machine_word
  3422.         mov     eax,[current_section]
  3423.         stos    dword [edi]
  3424.         mov     eax,ecx
  3425.         stos    dword [edi]
  3426.         inc     ecx
  3427.         test    [format_flags],8
  3428.         jnz     finish_elf64_rela_section
  3429.         mov     eax,4
  3430.         stos    dword [edi]
  3431.         mov     al,8
  3432.         stos    dword [edi]
  3433.         jmp     rel_section_ok
  3434.       finish_elf64_rela_section:
  3435.         mov     eax,8
  3436.         stos    dword [edi]
  3437.         xor     al,al
  3438.         stos    dword [edi]
  3439.         mov     al,24
  3440.         stos    dword [edi]
  3441.         xor     al,al
  3442.         stos    dword [edi]
  3443.       rel_section_ok:
  3444.         cmp     esi,[free_additional_memory]
  3445.         jne     make_section_entry
  3446.         pop     eax
  3447.         mov     ebx,[code_start]
  3448.         sub     eax,ebx
  3449.         mov     [code_size],eax
  3450.         mov     ecx,20h
  3451.         test    [format_flags],8
  3452.         jz      adjust_elf_section_headers_offset
  3453.         mov     ecx,28h
  3454.       adjust_elf_section_headers_offset:
  3455.         add     [ebx+ecx],eax
  3456.         mov     eax,1
  3457.         stos    dword [edi]
  3458.         mov     al,2
  3459.         stos    dword [edi]
  3460.         xor     al,al
  3461.         call    store_elf_machine_word
  3462.         call    store_elf_machine_word
  3463.         mov     eax,[code_size]
  3464.         call    store_elf_machine_word
  3465.         mov     eax,[edx+1]
  3466.         sub     eax,[free_additional_memory]
  3467.         call    store_elf_machine_word
  3468.         mov     eax,[current_section]
  3469.         inc     eax
  3470.         stos    dword [edi]
  3471.         mov     eax,[number_of_sections]
  3472.         inc     eax
  3473.         stos    dword [edi]
  3474.         test    [format_flags],8
  3475.         jnz     finish_elf64_sym_section
  3476.         mov     eax,4
  3477.         stos    dword [edi]
  3478.         mov     al,10h
  3479.         stos    dword [edi]
  3480.         jmp     sym_section_ok
  3481.       finish_elf64_sym_section:
  3482.         mov     eax,8
  3483.         stos    dword [edi]
  3484.         xor     al,al
  3485.         stos    dword [edi]
  3486.         mov     al,18h
  3487.         stos    dword [edi]
  3488.         xor     al,al
  3489.         stos    dword [edi]
  3490.       sym_section_ok:
  3491.         mov     al,1+8
  3492.         stos    dword [edi]
  3493.         mov     al,3
  3494.         stos    dword [edi]
  3495.         xor     al,al
  3496.         call    store_elf_machine_word
  3497.         call    store_elf_machine_word
  3498.         mov     eax,[edx+1]
  3499.         sub     eax,[free_additional_memory]
  3500.         add     eax,[code_size]
  3501.         call    store_elf_machine_word
  3502.         mov     eax,[edx+1+8]
  3503.         sub     eax,[edx+1]
  3504.         call    store_elf_machine_word
  3505.         xor     eax,eax
  3506.         stos    dword [edi]
  3507.         stos    dword [edi]
  3508.         mov     al,1
  3509.         call    store_elf_machine_word
  3510.         xor     eax,eax
  3511.         call    store_elf_machine_word
  3512.         mov     eax,'tab'
  3513.         mov     dword [edx+1],'.sym'
  3514.         mov     [edx+1+4],eax
  3515.         mov     dword [edx+1+8],'.str'
  3516.         mov     [edx+1+8+4],eax
  3517.         mov     [written_size],0
  3518.         mov     edx,[output_file]
  3519.         call    create
  3520.         jc      write_failed
  3521.         call    write_code
  3522.         mov     ecx,edi
  3523.         mov     edx,[free_additional_memory]
  3524.         sub     ecx,edx
  3525.         add     [written_size],ecx
  3526.         call    write
  3527.         jc      write_failed
  3528.         jmp     output_written
  3529.  
  3530. format_elf_exe:
  3531.         add     esi,2
  3532.         or      [format_flags],1
  3533.         mov     [image_base],8048000h
  3534.         cmp     byte [esi],80h
  3535.         jne     elf_exe_base_ok
  3536.         lods    word [esi]
  3537.         cmp     ah,'('
  3538.         jne     invalid_argument
  3539.         cmp     byte [esi],'.'
  3540.         je      invalid_value
  3541.         push    edx
  3542.         call    get_dword_value
  3543.         cmp     [value_type],0
  3544.         jne     invalid_use_of_symbol
  3545.         mov     [image_base],eax
  3546.         pop     edx
  3547.       elf_exe_base_ok:
  3548.         mov     byte [edx+10h],2
  3549.         mov     byte [edx+2Ah],20h
  3550.         mov     ebx,edi
  3551.         mov     ecx,20h shr 2
  3552.         cmp     [current_pass],0
  3553.         je      init_elf_segments
  3554.         imul    ecx,[number_of_sections]
  3555.       init_elf_segments:
  3556.         xor     eax,eax
  3557.         rep     stos dword [edi]
  3558.         mov     [number_of_sections],0
  3559.         mov     byte [ebx],1
  3560.         mov     word [ebx+1Ch],1000h
  3561.         mov     byte [ebx+18h],111b
  3562.         mov     eax,edi
  3563.         sub     eax,[code_start]
  3564.         mov     [ebx+4],eax
  3565.         add     eax,[image_base]
  3566.         mov     [ebx+8],eax
  3567.         mov     [ebx+0Ch],eax
  3568.         mov     [edx+18h],eax
  3569.         xor     edx,edx
  3570.         not     eax
  3571.         not     edx
  3572.         add     eax,1
  3573.         adc     edx,0
  3574.         add     eax,edi
  3575.         adc     edx,0
  3576.         mov     dword [org_origin],eax
  3577.         mov     dword [org_origin+4],edx
  3578.         mov     [org_registers],0
  3579.         mov     [org_start],edi
  3580.         mov     [symbols_stream],edi
  3581.         jmp     format_defined
  3582.       format_elf64_exe:
  3583.         add     esi,2
  3584.         or      [format_flags],1
  3585.         mov     [image_base],400000h
  3586.         mov     [image_base_high],0
  3587.         cmp     byte [esi],80h
  3588.         jne     elf64_exe_base_ok
  3589.         lods    word [esi]
  3590.         cmp     ah,'('
  3591.         jne     invalid_argument
  3592.         cmp     byte [esi],'.'
  3593.         je      invalid_value
  3594.         push    edx
  3595.         call    get_qword_value
  3596.         cmp     [value_type],0
  3597.         jne     invalid_use_of_symbol
  3598.         mov     [image_base],eax
  3599.         mov     [image_base_high],edx
  3600.         pop     edx
  3601.       elf64_exe_base_ok:
  3602.         mov     byte [edx+10h],2
  3603.         mov     byte [edx+36h],38h
  3604.         mov     ebx,edi
  3605.         mov     ecx,38h shr 2
  3606.         cmp     [current_pass],0
  3607.         je      init_elf64_segments
  3608.         imul    ecx,[number_of_sections]
  3609.       init_elf64_segments:
  3610.         xor     eax,eax
  3611.         rep     stos dword [edi]
  3612.         mov     [number_of_sections],0
  3613.         mov     byte [ebx],1
  3614.         mov     word [ebx+30h],1000h
  3615.         mov     byte [ebx+4],111b
  3616.         push    edx
  3617.         mov     eax,edi
  3618.         sub     eax,[code_start]
  3619.         mov     [ebx+8],eax
  3620.         xor     edx,edx
  3621.         add     eax,[image_base]
  3622.         adc     edx,[image_base_high]
  3623.         mov     [ebx+10h],eax
  3624.         mov     [ebx+10h+4],edx
  3625.         mov     [ebx+18h],eax
  3626.         mov     [ebx+18h+4],edx
  3627.         pop     ebx
  3628.         mov     [ebx+18h],eax
  3629.         mov     [ebx+18h+4],edx
  3630.         not     eax
  3631.         not     edx
  3632.         add     eax,1
  3633.         adc     edx,0
  3634.         add     eax,edi
  3635.         adc     edx,0
  3636.         mov     dword [org_origin],eax
  3637.         mov     dword [org_origin+4],edx
  3638.         mov     [org_registers],0
  3639.         mov     [org_start],edi
  3640.         mov     [symbols_stream],edi
  3641.         jmp     format_defined
  3642. elf_entry:
  3643.         lods    byte [esi]
  3644.         cmp     al,'('
  3645.         jne     invalid_argument
  3646.         cmp     byte [esi],'.'
  3647.         je      invalid_value
  3648.         test    [format_flags],8
  3649.         jnz     elf64_entry
  3650.         call    get_dword_value
  3651.         cmp     [value_type],0
  3652.         jne     invalid_use_of_symbol
  3653.         mov     edx,[code_start]
  3654.         mov     [edx+18h],eax
  3655.         jmp     instruction_assembled
  3656.       elf64_entry:
  3657.         call    get_qword_value
  3658.         cmp     [value_type],0
  3659.         jne     invalid_use_of_symbol
  3660.         mov     ebx,[code_start]
  3661.         mov     [ebx+18h],eax
  3662.         mov     [ebx+1Ch],edx
  3663.         jmp     instruction_assembled
  3664. elf_segment:
  3665.         bt      [format_flags],0
  3666.         jnc     illegal_instruction
  3667.         test    [format_flags],8
  3668.         jnz     elf64_segment
  3669.         call    close_elf_segment
  3670.         push    eax
  3671.         mov     ebx,[number_of_sections]
  3672.         shl     ebx,5
  3673.         add     ebx,[code_start]
  3674.         add     ebx,34h
  3675.         cmp     ebx,[symbols_stream]
  3676.         jb      new_elf_segment
  3677.         mov     ebx,[symbols_stream]
  3678.         sub     ebx,20h
  3679.         push    edi
  3680.         mov     edi,ebx
  3681.         mov     ecx,20h shr 2
  3682.         xor     eax,eax
  3683.         rep     stos dword [edi]
  3684.         pop     edi
  3685.         or      [next_pass_needed],-1
  3686.       new_elf_segment:
  3687.         mov     byte [ebx],1
  3688.         mov     word [ebx+1Ch],1000h
  3689.       elf_segment_flags:
  3690.         cmp     byte [esi],19h
  3691.         jne     elf_segment_flags_ok
  3692.         lods    word [esi]
  3693.         sub     ah,28
  3694.         jbe     invalid_argument
  3695.         cmp     ah,1
  3696.         je      mark_elf_segment_flag
  3697.         cmp     ah,3
  3698.         ja      invalid_argument
  3699.         xor     ah,1
  3700.         cmp     ah,2
  3701.         je      mark_elf_segment_flag
  3702.         inc     ah
  3703.       mark_elf_segment_flag:
  3704.         test    [ebx+18h],ah
  3705.         jnz     setting_already_specified
  3706.         or      [ebx+18h],ah
  3707.         jmp     elf_segment_flags
  3708.       elf_segment_flags_ok:
  3709.         mov     eax,edi
  3710.         sub     eax,[code_start]
  3711.         mov     [ebx+4],eax
  3712.         pop     edx
  3713.         and     eax,0FFFh
  3714.         add     edx,eax
  3715.         mov     [ebx+8],edx
  3716.         mov     [ebx+0Ch],edx
  3717.         mov     eax,edx
  3718.         xor     edx,edx
  3719.         not     eax
  3720.         not     edx
  3721.         add     eax,1
  3722.         adc     edx,0
  3723.         add     eax,edi
  3724.         adc     edx,0
  3725.         mov     dword [org_origin],eax
  3726.         mov     dword [org_origin+4],edx
  3727.         mov     [org_registers],0
  3728.         mov     [org_start],edi
  3729.         inc     [number_of_sections]
  3730.         jmp     instruction_assembled
  3731.       close_elf_segment:
  3732.         cmp     [number_of_sections],0
  3733.         jne     finish_elf_segment
  3734.         cmp     edi,[symbols_stream]
  3735.         jne     first_elf_segment_ok
  3736.         push    edi
  3737.         mov     edi,[code_start]
  3738.         add     edi,34h
  3739.         mov     ecx,20h shr 2
  3740.         xor     eax,eax
  3741.         rep     stos dword [edi]
  3742.         pop     edi
  3743.         mov     eax,[image_base]
  3744.         ret
  3745.       first_elf_segment_ok:
  3746.         inc     [number_of_sections]
  3747.       finish_elf_segment:
  3748.         mov     ebx,[number_of_sections]
  3749.         dec     ebx
  3750.         shl     ebx,5
  3751.         add     ebx,[code_start]
  3752.         add     ebx,34h
  3753.         mov     eax,edi
  3754.         sub     eax,[code_start]
  3755.         sub     eax,[ebx+4]
  3756.         mov     edx,edi
  3757.         cmp     edi,[undefined_data_end]
  3758.         jne     elf_segment_size_ok
  3759.         mov     edi,[undefined_data_start]
  3760.       elf_segment_size_ok:
  3761.         mov     [ebx+14h],eax
  3762.         add     eax,edi
  3763.         sub     eax,edx
  3764.         mov     [ebx+10h],eax
  3765.         mov     eax,[ebx+8]
  3766.         add     eax,[ebx+14h]
  3767.         add     eax,0FFFh
  3768.         and     eax,not 0FFFh
  3769.         ret
  3770.       elf64_segment:
  3771.         call    close_elf64_segment
  3772.         push    eax edx
  3773.         mov     ebx,[number_of_sections]
  3774.         imul    ebx,38h
  3775.         add     ebx,[code_start]
  3776.         add     ebx,40h
  3777.         cmp     ebx,[symbols_stream]
  3778.         jb      new_elf64_segment
  3779.         mov     ebx,[symbols_stream]
  3780.         sub     ebx,38h
  3781.         push    edi
  3782.         mov     edi,ebx
  3783.         mov     ecx,38h shr 2
  3784.         xor     eax,eax
  3785.         rep     stos dword [edi]
  3786.         pop     edi
  3787.         or      [next_pass_needed],-1
  3788.       new_elf64_segment:
  3789.         mov     byte [ebx],1
  3790.         mov     word [ebx+30h],1000h
  3791.       elf64_segment_flags:
  3792.         cmp     byte [esi],19h
  3793.         jne     elf64_segment_flags_ok
  3794.         lods    word [esi]
  3795.         sub     ah,28
  3796.         jbe     invalid_argument
  3797.         cmp     ah,1
  3798.         je      mark_elf64_segment_flag
  3799.         cmp     ah,3
  3800.         ja      invalid_argument
  3801.         xor     ah,1
  3802.         cmp     ah,2
  3803.         je      mark_elf64_segment_flag
  3804.         inc     ah
  3805.       mark_elf64_segment_flag:
  3806.         test    [ebx+4],ah
  3807.         jnz     setting_already_specified
  3808.         or      [ebx+4],ah
  3809.         jmp     elf64_segment_flags
  3810.       elf64_segment_flags_ok:
  3811.         mov     ecx,edi
  3812.         sub     ecx,[code_start]
  3813.         mov     [ebx+8],ecx
  3814.         pop     edx eax
  3815.         and     ecx,0FFFh
  3816.         add     eax,ecx
  3817.         adc     edx,0
  3818.         mov     [ebx+10h],eax
  3819.         mov     [ebx+10h+4],edx
  3820.         mov     [ebx+18h],eax
  3821.         mov     [ebx+18h+4],edx
  3822.         not     eax
  3823.         not     edx
  3824.         add     eax,1
  3825.         adc     edx,0
  3826.         add     eax,edi
  3827.         adc     edx,0
  3828.         mov     dword [org_origin],eax
  3829.         mov     dword [org_origin+4],edx
  3830.         mov     [org_registers],0
  3831.         mov     [org_start],edi
  3832.         inc     [number_of_sections]
  3833.         jmp     instruction_assembled
  3834.       close_elf64_segment:
  3835.         cmp     [number_of_sections],0
  3836.         jne     finish_elf64_segment
  3837.         cmp     edi,[symbols_stream]
  3838.         jne     first_elf64_segment_ok
  3839.         push    edi
  3840.         mov     edi,[code_start]
  3841.         add     edi,40h
  3842.         mov     ecx,38h shr 2
  3843.         xor     eax,eax
  3844.         rep     stos dword [edi]
  3845.         pop     edi
  3846.         mov     eax,[image_base]
  3847.         mov     edx,[image_base_high]
  3848.         ret
  3849.       first_elf64_segment_ok:
  3850.         inc     [number_of_sections]
  3851.       finish_elf64_segment:
  3852.         mov     ebx,[number_of_sections]
  3853.         dec     ebx
  3854.         imul    ebx,38h
  3855.         add     ebx,[code_start]
  3856.         add     ebx,40h
  3857.         mov     eax,edi
  3858.         sub     eax,[code_start]
  3859.         sub     eax,[ebx+8]
  3860.         mov     edx,edi
  3861.         cmp     edi,[undefined_data_end]
  3862.         jne     elf64_segment_size_ok
  3863.         mov     edi,[undefined_data_start]
  3864.       elf64_segment_size_ok:
  3865.         mov     [ebx+28h],eax
  3866.         add     eax,edi
  3867.         sub     eax,edx
  3868.         mov     [ebx+20h],eax
  3869.         mov     eax,[ebx+10h]
  3870.         mov     edx,[ebx+10h+4]
  3871.         add     eax,[ebx+28h]
  3872.         adc     edx,0
  3873.         sub     eax,1
  3874.         sbb     edx,0
  3875.         shrd    eax,edx,12
  3876.         shr     edx,12
  3877.         add     eax,1
  3878.         adc     edx,0
  3879.         shld    edx,eax,12
  3880.         shl     eax,12
  3881.         ret
  3882. close_elf_exe:
  3883.         test    [format_flags],8
  3884.         jnz     close_elf64_exe
  3885.         call    close_elf_segment
  3886.         mov     edx,[code_start]
  3887.         mov     eax,[number_of_sections]
  3888.         mov     byte [edx+1Ch],34h
  3889.         mov     [edx+2Ch],ax
  3890.         shl     eax,5
  3891.         add     eax,edx
  3892.         add     eax,34h
  3893.         cmp     eax,[symbols_stream]
  3894.         je      elf_exe_ok
  3895.         or      [next_pass_needed],-1
  3896.       elf_exe_ok:
  3897.         ret
  3898.       close_elf64_exe:
  3899.         call    close_elf64_segment
  3900.         mov     edx,[code_start]
  3901.         mov     eax,[number_of_sections]
  3902.         mov     byte [edx+20h],40h
  3903.         mov     [edx+38h],ax
  3904.         imul    eax,38h
  3905.         add     eax,edx
  3906.         add     eax,40h
  3907.         cmp     eax,[symbols_stream]
  3908.         je      elf64_exe_ok
  3909.         or      [next_pass_needed],-1
  3910.       elf64_exe_ok:
  3911.         ret
  3912.