"Fossies" - the Fresh Open Source Software Archive

Member "fasm/source/libc/system.inc" (21 Feb 2022, 6687 Bytes) of package /linux/misc/fasm-1.73.30.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) fasm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "system.inc": 1.73.29_vs_1.73.30.

    1 
    2 ; flat assembler interface for Unix/libc
    3 ; Copyright (c) 1999-2022, Tomasz Grysztar.
    4 ; All rights reserved.
    5 
    6 extrn malloc
    7 extrn free
    8 extrn getenv
    9 extrn fopen
   10 extrn fclose
   11 extrn fread
   12 extrn fwrite
   13 extrn fseek
   14 extrn ftell
   15 extrn time
   16 extrn exit
   17 
   18 extrn 'write' as libc_write
   19 
   20 init_memory:
   21     mov eax,esp
   22     and eax,not 0FFFh
   23     add eax,1000h-10000h
   24     mov [stack_limit],eax
   25     mov ecx,[memory_setting]
   26     shl ecx,10
   27     jnz allocate_memory
   28     mov ecx,1000000h
   29       allocate_memory:
   30     mov [memory_setting],ecx
   31     ccall   malloc,ecx
   32     or  eax,eax
   33     jz  out_of_memory
   34     mov [additional_memory],eax
   35     add eax,[memory_setting]
   36     mov [memory_end],eax
   37     mov eax,[memory_setting]
   38     shr eax,2
   39     add eax,[additional_memory]
   40     mov [additional_memory_end],eax
   41     mov [memory_start],eax
   42     ret
   43 
   44 exit_program:
   45     movzx   eax,al
   46     push    eax
   47     ccall   free,[additional_memory]
   48     pop eax
   49     ccall   exit,eax
   50     mov esp,[stack_frame]
   51     pop ebp
   52     ret
   53 
   54 get_environment_variable:
   55     ccall   getenv,esi
   56     mov esi,eax
   57     or  eax,eax
   58     jz  no_environment_variable
   59       copy_variable_value:
   60     lodsb
   61     cmp edi,[memory_end]
   62     jae out_of_memory
   63     stosb
   64     or  al,al
   65     jnz copy_variable_value
   66     dec edi
   67     ret
   68       no_environment_variable:
   69     stosb
   70     dec edi
   71     ret
   72 
   73 open:
   74     push    esi edi ebp
   75     call    adapt_path
   76     ccall   fopen,buffer,open_mode
   77     pop ebp edi esi
   78     or  eax,eax
   79     jz  file_error
   80     mov ebx,eax
   81     clc
   82     ret
   83     adapt_path:
   84     mov esi,edx
   85     mov edi,buffer
   86       copy_path:
   87     lods    byte [esi]
   88     cmp al,'\'
   89     jne path_char_ok
   90     mov al,'/'
   91       path_char_ok:
   92     stos    byte [edi]
   93     or  al,al
   94     jnz copy_path
   95     cmp edi,buffer+1000h
   96     ja  out_of_memory
   97     ret
   98 create:
   99     push    esi edi ebp
  100     call    adapt_path
  101     ccall   fopen,buffer,create_mode
  102     pop ebp edi esi
  103     or  eax,eax
  104     jz  file_error
  105     mov ebx,eax
  106     clc
  107     ret
  108 close:
  109     ccall   fclose,ebx
  110     ret
  111 read:
  112     push    ebx ecx edx esi edi ebp
  113     ccall   fread,edx,1,ecx,ebx
  114     pop ebp edi esi edx ecx ebx
  115     cmp eax,ecx
  116     jne file_error
  117     clc
  118     ret
  119     file_error:
  120     stc
  121     ret
  122 write:
  123     push    ebx ecx edx esi edi ebp
  124     ccall   fwrite,edx,1,ecx,ebx
  125     pop ebp edi esi edx ecx ebx
  126     cmp eax,ecx
  127     jne file_error
  128     clc
  129     ret
  130 lseek:
  131     push    ebx
  132     movzx   eax,al
  133     ccall   fseek,ebx,edx,eax
  134     test    eax,eax
  135     jnz lseek_error
  136     mov ebx,[esp]
  137     ccall   ftell,ebx
  138     pop ebx
  139     clc
  140     ret
  141     lseek_error:
  142     pop ebx
  143     stc
  144     ret
  145 
  146 display_string:
  147     lodsb
  148     or  al,al
  149     jz  string_displayed
  150     mov dl,al
  151     call    display_character
  152     jmp display_string
  153       string_displayed:
  154     ret
  155 display_character:
  156     mov [character],dl
  157     ccall   libc_write,[con_handle],character,1
  158     ret
  159 display_number:
  160     push    ebx
  161     mov ecx,1000000000
  162     xor edx,edx
  163     xor bl,bl
  164       display_loop:
  165     div ecx
  166     push    edx
  167     cmp ecx,1
  168     je  display_digit
  169     or  bl,bl
  170     jnz display_digit
  171     or  al,al
  172     jz  digit_ok
  173     not bl
  174       display_digit:
  175     mov dl,al
  176     add dl,30h
  177     push    ebx ecx
  178     call    display_character
  179     pop ecx ebx
  180       digit_ok:
  181     mov eax,ecx
  182     xor edx,edx
  183     mov ecx,10
  184     div ecx
  185     mov ecx,eax
  186     pop eax
  187     or  ecx,ecx
  188     jnz display_loop
  189     pop ebx
  190     ret
  191 
  192 display_user_messages:
  193     mov [displayed_count],0
  194     call    show_display_buffer
  195     cmp [displayed_count],0
  196     je  line_break_ok
  197     cmp [last_displayed],0Ah
  198     je  line_break_ok
  199     mov dl,0Ah
  200     call    display_character
  201       line_break_ok:
  202     ret
  203 display_block:
  204     jecxz   block_displayed
  205     add [displayed_count],ecx
  206     mov al,[esi+ecx-1]
  207     mov [last_displayed],al
  208       display_characters:
  209     lodsb
  210     mov dl,al
  211     push    ecx esi
  212     call    display_character
  213     pop esi ecx
  214     loop    display_characters
  215       block_displayed:
  216     ret
  217 
  218 fatal_error:
  219     mov [con_handle],2
  220     mov esi,error_prefix
  221     call    display_string
  222     pop esi
  223     call    display_string
  224     mov esi,error_suffix
  225     call    display_string
  226     mov al,0FFh
  227     jmp exit_program
  228 assembler_error:
  229     mov [con_handle],2
  230     call    display_user_messages
  231     mov ebx,[current_line]
  232     test    ebx,ebx
  233     jz  display_error_message
  234     push    dword 0
  235       get_error_lines:
  236     mov eax,[ebx]
  237     cmp byte [eax],0
  238     je  get_next_error_line
  239     push    ebx
  240     test    byte [ebx+7],80h
  241     jz  display_error_line
  242     mov edx,ebx
  243       find_definition_origin:
  244     mov edx,[edx+12]
  245     test    byte [edx+7],80h
  246     jnz find_definition_origin
  247     push    edx
  248       get_next_error_line:
  249     mov ebx,[ebx+8]
  250     jmp get_error_lines
  251       display_error_line:
  252     mov esi,[ebx]
  253     call    display_string
  254     mov esi,line_number_start
  255     call    display_string
  256     mov eax,[ebx+4]
  257     and eax,7FFFFFFFh
  258     call    display_number
  259     mov dl,']'
  260     call    display_character
  261     pop esi
  262     cmp ebx,esi
  263     je  line_number_ok
  264     mov dl,20h
  265     call    display_character
  266     push    esi
  267     mov esi,[esi]
  268     movzx   ecx,byte [esi]
  269     inc esi
  270     call    display_block
  271     mov esi,line_number_start
  272     call    display_string
  273     pop esi
  274     mov eax,[esi+4]
  275     and eax,7FFFFFFFh
  276     call    display_number
  277     mov dl,']'
  278     call    display_character
  279       line_number_ok:
  280     mov esi,line_data_start
  281     call    display_string
  282     mov esi,ebx
  283     mov edx,[esi]
  284     call    open
  285     mov al,2
  286     xor edx,edx
  287     call    lseek
  288     mov edx,[esi+8]
  289     sub eax,edx
  290     jz  line_data_displayed
  291     push    eax
  292     xor al,al
  293     call    lseek
  294     mov ecx,[esp]
  295     mov edx,[additional_memory]
  296     lea eax,[edx+ecx]
  297     cmp eax,[additional_memory_end]
  298     ja  out_of_memory
  299     call    read
  300     call    close
  301     pop ecx
  302     mov esi,[additional_memory]
  303       get_line_data:
  304     mov al,[esi]
  305     cmp al,0Ah
  306     je  display_line_data
  307     cmp al,0Dh
  308     je  display_line_data
  309     cmp al,1Ah
  310     je  display_line_data
  311     or  al,al
  312     jz  display_line_data
  313     inc esi
  314     loop    get_line_data
  315       display_line_data:
  316     mov ecx,esi
  317     mov esi,[additional_memory]
  318     sub ecx,esi
  319     call    display_block
  320       line_data_displayed:
  321     mov esi,lf
  322     call    display_string
  323     pop ebx
  324     or  ebx,ebx
  325     jnz display_error_line
  326     cmp [preprocessing_done],0
  327     je  display_error_message
  328     mov esi,preprocessed_instruction_prefix
  329     call    display_string
  330     mov esi,[current_line]
  331     add esi,16
  332     mov edi,[additional_memory]
  333     xor dl,dl
  334       convert_instruction:
  335     lodsb
  336     cmp al,1Ah
  337     je  copy_symbol
  338     cmp al,22h
  339     je  copy_symbol
  340     cmp al,3Bh
  341     je  instruction_converted
  342     stosb
  343     or  al,al
  344     jz  instruction_converted
  345     xor dl,dl
  346     jmp convert_instruction
  347       copy_symbol:
  348     or  dl,dl
  349     jz  space_ok
  350     mov byte [edi],20h
  351     inc edi
  352       space_ok:
  353     cmp al,22h
  354     je  quoted
  355     lodsb
  356     movzx   ecx,al
  357     rep movsb
  358     or  dl,-1
  359     jmp convert_instruction
  360       quoted:
  361     mov al,27h
  362     stosb
  363     lodsd
  364     mov ecx,eax
  365     jecxz   quoted_copied
  366       copy_quoted:
  367     lodsb
  368     stosb
  369     cmp al,27h
  370     jne quote_ok
  371     stosb
  372       quote_ok:
  373     loop    copy_quoted
  374       quoted_copied:
  375     mov al,27h
  376     stosb
  377     or  dl,-1
  378     jmp convert_instruction
  379       instruction_converted:
  380     xor al,al
  381     stosb
  382     mov esi,[additional_memory]
  383     call    display_string
  384     mov esi,lf
  385     call    display_string
  386       display_error_message:
  387     mov esi,error_prefix
  388     call    display_string
  389     pop esi
  390     call    display_string
  391     mov esi,error_suffix
  392     call    display_string
  393     mov al,2
  394     jmp exit_program
  395 
  396 make_timestamp:
  397     ccall   time,timestamp
  398     mov eax,dword [timestamp]
  399     mov edx,dword [timestamp+4]
  400     ret
  401 
  402 open_mode db 'r',0
  403 create_mode db 'w',0
  404 
  405 error_prefix db 'error: ',0
  406 error_suffix db '.'
  407 lf db 0xA,0
  408 line_number_start db ' [',0
  409 line_data_start db ':',0xA,0
  410 preprocessed_instruction_prefix db 'processed: ',0