"Fossies" - the Fresh Open Source Software Archive

Member "fasm/source/Linux/x64/system.inc" (9 Feb 2020, 8791 Bytes) of package /linux/misc/fasm-1.73.22.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.21_vs_1.73.22.

    1 
    2 ; flat assembler interface for Linux x64
    3 ; Copyright (c) 1999-2020, Tomasz Grysztar.
    4 ; All rights reserved.
    5 
    6 O_ACCMODE  = 0003o
    7 O_RDONLY   = 0000o
    8 O_WRONLY   = 0001o
    9 O_RDWR     = 0002o
   10 O_CREAT    = 0100o
   11 O_EXCL     = 0200o
   12 O_NOCTTY   = 0400o
   13 O_TRUNC    = 1000o
   14 O_APPEND   = 2000o
   15 O_NONBLOCK = 4000o
   16 
   17 S_ISUID    = 4000o
   18 S_ISGID    = 2000o
   19 S_ISVTX    = 1000o
   20 S_IRUSR    = 0400o
   21 S_IWUSR    = 0200o
   22 S_IXUSR    = 0100o
   23 S_IRGRP    = 0040o
   24 S_IWGRP    = 0020o
   25 S_IXGRP    = 0010o
   26 S_IROTH    = 0004o
   27 S_IWOTH    = 0002o
   28 S_IXOTH    = 0001o
   29 
   30 init_memory:
   31     mov eax,esp
   32     and eax,not 0FFFh
   33     add eax,1000h-10000h
   34     mov [stack_limit],eax
   35     xor edi,edi
   36     mov eax,12
   37     syscall
   38     mov ecx,[memory_setting]
   39     shl ecx,10
   40     jnz allocate_memory
   41     mov ecx,1000000h
   42       allocate_memory:
   43     mov r9d,eax
   44     cmp rax,r9
   45     jne high_brk
   46     mov [additional_memory],eax
   47     lea edi,[eax+ecx]
   48     mov eax,12
   49     syscall
   50     mov r9d,eax
   51     cmp rax,r9
   52     jne no_low_memory
   53     mov [memory_end],eax
   54     sub eax,[additional_memory]
   55     shr eax,2
   56     add eax,[additional_memory]
   57     mov [additional_memory_end],eax
   58     mov [memory_start],eax
   59     ret
   60       high_brk:
   61     xor r9d,r9d
   62     or  r8,-1
   63     mov r10d,62h        ; MAP_PRIVATE + MAP_ANONYMOUS + MAP_32BIT
   64     mov edx,3           ; PROT_READ + PROT_WRITE
   65     mov esi,ecx
   66     xor edi,edi
   67     mov eax,9           ; sys_mmap
   68     syscall
   69     cmp eax,-1
   70     je  mmap_with_hint
   71     mov r9d,eax
   72     cmp rax,r9
   73     jne mmap_unusable
   74     add r9d,esi
   75     jnc mmap_ok
   76       mmap_unusable:
   77     mov rdi,rax
   78     mov eax,11          ; sys_munmap
   79     syscall
   80       mmap_with_hint:
   81     mov r10d,22h        ; MAP_PRIVATE + MAP_ANONYMOUS
   82     mov edx,3           ; PROT_READ + PROT_WRITE
   83     mov edi,480000h
   84     mov eax,9           ; sys_mmap
   85     syscall
   86     cmp eax,-1
   87     je  no_low_memory
   88     mov r9d,eax
   89     cmp rax,r9
   90     jne no_low_memory
   91     add r9d,esi
   92     jnc mmap_ok
   93       no_low_memory:
   94     mov esi,lf
   95     call    display_string
   96     push    _no_low_memory
   97     jmp fatal_error
   98       mmap_ok:
   99     mov [additional_memory],eax
  100     lea edi,[eax+esi]
  101     mov [memory_end],edi
  102     shr esi,2
  103     add eax,esi
  104     mov [additional_memory_end],eax
  105     mov [memory_start],eax
  106     ret
  107 
  108 exit_program:
  109     movzx   edi,al
  110     mov eax,60
  111     syscall
  112 
  113 get_environment_variable:
  114     mov ebx,esi
  115     mov rsi,[environment]
  116       compare_variable_names:
  117     mov edx,ebx
  118       compare_character:
  119     lodsb
  120     mov ah,[edx]
  121     inc edx
  122     cmp al,'='
  123     je  end_of_variable_name
  124     or  ah,ah
  125     jz  next_variable
  126     sub ah,al
  127     jz  compare_character
  128     cmp ah,20h
  129     jne next_variable
  130     cmp al,41h
  131     jb  next_variable
  132     cmp al,5Ah
  133     jna compare_character
  134       next_variable:
  135     lodsb
  136     or  al,al
  137     jnz next_variable
  138     cmp byte [rsi],0
  139     jne compare_variable_names
  140     ret
  141       end_of_variable_name:
  142     or  ah,ah
  143     jnz next_variable
  144       copy_variable_value:
  145     lodsb
  146     cmp edi,[memory_end]
  147     jae out_of_memory
  148     stosb
  149     or  al,al
  150     jnz copy_variable_value
  151     dec edi
  152     ret
  153 
  154 open:
  155     mov r12d,esi
  156     mov r13d,edi
  157     call    adapt_path
  158     mov eax,2
  159     mov edi,buffer
  160     mov esi,O_RDONLY
  161     xor edx,edx
  162     syscall
  163     mov esi,r12d
  164     mov edi,r13d
  165     test    eax,eax
  166     js  file_error
  167     mov ebx,eax
  168     clc
  169     ret
  170     adapt_path:
  171     mov esi,edx
  172     mov edi,buffer
  173       copy_path:
  174     lods    byte [esi]
  175     cmp al,'\'
  176     jne path_char_ok
  177     mov al,'/'
  178       path_char_ok:
  179     stos    byte [edi]
  180     or  al,al
  181     jnz copy_path
  182     cmp edi,buffer+1000h
  183     ja  out_of_memory
  184     ret
  185 create:
  186     mov r12d,esi
  187     mov r13d,edi
  188     mov r15d,edx
  189     call    adapt_path
  190     mov edi,buffer
  191     mov esi,O_CREAT+O_TRUNC+O_WRONLY
  192     mov edx,S_IRUSR+S_IWUSR+S_IRGRP+S_IROTH
  193     cmp r15d,[output_file]
  194     jne do_create
  195     cmp [output_format],5
  196     jne do_create
  197     bt  [format_flags],0
  198     jnc do_create
  199     or  edx,S_IXUSR+S_IXGRP+S_IXOTH
  200       do_create:
  201     mov eax,2
  202     syscall
  203     mov esi,r12d
  204     mov edi,r13d
  205     test    eax,eax
  206     js  file_error
  207     mov ebx,eax
  208     clc
  209     ret
  210 close:
  211     mov r13d,edi
  212     mov edi,ebx
  213     mov eax,3
  214     syscall
  215     mov edi,r13d
  216     ret
  217 read:
  218     mov r12d,esi
  219     mov r13d,edi
  220     mov eax,0
  221     mov edi,ebx
  222     mov esi,edx
  223     mov edx,ecx
  224     syscall
  225     mov ecx,edx
  226     mov edx,esi
  227     mov esi,r12d
  228     mov edi,r13d
  229     test    eax,eax
  230     js  file_error
  231     cmp eax,ecx
  232     jne file_error
  233     clc
  234     ret
  235     file_error:
  236     stc
  237     ret
  238 write:
  239     mov r12d,esi
  240     mov r13d,edi
  241     mov eax,1
  242     mov edi,ebx
  243     mov esi,edx
  244     mov edx,ecx
  245     syscall
  246     mov ecx,edx
  247     mov edx,esi
  248     mov esi,r12d
  249     mov edi,r13d
  250     test    eax,eax
  251     js  file_error
  252     clc
  253     ret
  254 lseek:
  255     mov r12d,esi
  256     mov r13d,edi
  257     mov edi,ebx
  258     mov esi,edx
  259     xor edx,edx
  260     mov dl,al
  261     mov eax,8
  262     syscall
  263     mov esi,r12d
  264     mov edi,r13d
  265     cmp eax,-1
  266     je  file_error
  267     clc
  268     ret
  269 
  270 display_string:
  271     mov edi,esi
  272     mov edx,esi
  273     or  ecx,-1
  274     xor al,al
  275     repne   scasb
  276     neg ecx
  277     sub ecx,2
  278     mov eax,1
  279     mov edi,[con_handle]
  280     mov esi,edx
  281     mov edx,ecx
  282     syscall
  283     ret
  284 display_character:
  285     mov r12d,esi
  286     mov r13d,edi
  287     mov [character],dl
  288     mov eax,1
  289     mov edi,[con_handle]
  290     mov esi,character
  291     mov edx,1
  292     syscall
  293     mov esi,r12d
  294     mov edi,r13d
  295     ret
  296 display_number:
  297     mov r14d,ebx
  298     mov ecx,1000000000
  299     xor edx,edx
  300     xor bl,bl
  301       display_loop:
  302     div ecx
  303     mov r15d,edx
  304     cmp ecx,1
  305     je  display_digit
  306     or  bl,bl
  307     jnz display_digit
  308     or  al,al
  309     jz  digit_ok
  310     not bl
  311       display_digit:
  312     mov dl,al
  313     add dl,30h
  314     mov r10d,ecx
  315     call    display_character
  316     mov ecx,r10d
  317       digit_ok:
  318     mov eax,ecx
  319     xor edx,edx
  320     mov ecx,10
  321     div ecx
  322     mov ecx,eax
  323     mov eax,r15d
  324     or  ecx,ecx
  325     jnz display_loop
  326     mov ebx,r14d
  327     ret
  328 
  329 display_user_messages:
  330     mov [displayed_count],0
  331     call    show_display_buffer
  332     cmp [displayed_count],0
  333     je  line_break_ok
  334     cmp [last_displayed],0Ah
  335     je  line_break_ok
  336     mov dl,0Ah
  337     call    display_character
  338       line_break_ok:
  339     ret
  340 display_block:
  341     jecxz   block_displayed
  342     add [displayed_count],ecx
  343     mov al,[esi+ecx-1]
  344     mov [last_displayed],al
  345     mov r13d,edi
  346     mov eax,1
  347     mov edi,[con_handle]
  348     mov edx,ecx
  349     syscall
  350     mov edi,r13d
  351       block_displayed:
  352     ret
  353 
  354 fatal_error:
  355     mov [con_handle],2
  356     mov esi,error_prefix
  357     call    display_string
  358     pop esi
  359     call    display_string
  360     mov esi,error_suffix
  361     call    display_string
  362     mov al,0FFh
  363     jmp exit_program
  364 assembler_error:
  365     mov [con_handle],2
  366     call    display_user_messages
  367     mov ebx,[current_line]
  368     test    ebx,ebx
  369     jz  display_error_message
  370     push    dword 0
  371       get_error_lines:
  372     mov eax,[ebx]
  373     cmp byte [eax],0
  374     je  get_next_error_line
  375     push    ebx
  376     test    byte [ebx+7],80h
  377     jz  display_error_line
  378     mov edx,ebx
  379       find_definition_origin:
  380     mov edx,[edx+12]
  381     test    byte [edx+7],80h
  382     jnz find_definition_origin
  383     push    edx
  384       get_next_error_line:
  385     mov ebx,[ebx+8]
  386     jmp get_error_lines
  387       display_error_line:
  388     mov esi,[ebx]
  389     call    display_string
  390     mov esi,line_number_start
  391     call    display_string
  392     mov eax,[ebx+4]
  393     and eax,7FFFFFFFh
  394     call    display_number
  395     mov dl,']'
  396     call    display_character
  397     pop esi
  398     cmp ebx,esi
  399     je  line_number_ok
  400     mov dl,20h
  401     call    display_character
  402     push    esi
  403     mov esi,[esi]
  404     movzx   ecx,byte [esi]
  405     inc esi
  406     call    display_block
  407     mov esi,line_number_start
  408     call    display_string
  409     pop esi
  410     mov eax,[esi+4]
  411     and eax,7FFFFFFFh
  412     call    display_number
  413     mov dl,']'
  414     call    display_character
  415       line_number_ok:
  416     mov esi,line_data_start
  417     call    display_string
  418     mov esi,ebx
  419     mov edx,[esi]
  420     call    open
  421     mov al,2
  422     xor edx,edx
  423     call    lseek
  424     mov edx,[esi+8]
  425     sub eax,edx
  426     jz  line_data_displayed
  427     push    eax
  428     xor al,al
  429     call    lseek
  430     mov ecx,[esp]
  431     mov edx,[additional_memory]
  432     lea eax,[edx+ecx]
  433     cmp eax,[additional_memory_end]
  434     ja  out_of_memory
  435     call    read
  436     call    close
  437     pop ecx
  438     mov esi,[additional_memory]
  439       get_line_data:
  440     mov al,[esi]
  441     cmp al,0Ah
  442     je  display_line_data
  443     cmp al,0Dh
  444     je  display_line_data
  445     cmp al,1Ah
  446     je  display_line_data
  447     or  al,al
  448     jz  display_line_data
  449     inc esi
  450     loop    get_line_data
  451       display_line_data:
  452     mov ecx,esi
  453     mov esi,[additional_memory]
  454     sub ecx,esi
  455     call    display_block
  456       line_data_displayed:
  457     mov esi,lf
  458     call    display_string
  459     pop ebx
  460     or  ebx,ebx
  461     jnz display_error_line
  462     cmp [preprocessing_done],0
  463     je  display_error_message
  464     mov esi,preprocessed_instruction_prefix
  465     call    display_string
  466     mov esi,[current_line]
  467     add esi,16
  468     mov edi,[additional_memory]
  469     xor dl,dl
  470       convert_instruction:
  471     lodsb
  472     cmp al,1Ah
  473     je  copy_symbol
  474     cmp al,22h
  475     je  copy_symbol
  476     cmp al,3Bh
  477     je  instruction_converted
  478     stosb
  479     or  al,al
  480     jz  instruction_converted
  481     xor dl,dl
  482     jmp convert_instruction
  483       copy_symbol:
  484     or  dl,dl
  485     jz  space_ok
  486     mov byte [edi],20h
  487     inc edi
  488       space_ok:
  489     cmp al,22h
  490     je  quoted
  491     lodsb
  492     movzx   ecx,al
  493     rep movsb
  494     or  dl,-1
  495     jmp convert_instruction
  496       quoted:
  497     mov al,27h
  498     stosb
  499     lodsd
  500     mov ecx,eax
  501     jecxz   quoted_copied
  502       copy_quoted:
  503     lodsb
  504     stosb
  505     cmp al,27h
  506     jne quote_ok
  507     stosb
  508       quote_ok:
  509     loop    copy_quoted
  510       quoted_copied:
  511     mov al,27h
  512     stosb
  513     or  dl,-1
  514     jmp convert_instruction
  515       instruction_converted:
  516     xor al,al
  517     stosb
  518     mov esi,[additional_memory]
  519     call    display_string
  520     mov esi,lf
  521     call    display_string
  522       display_error_message:
  523     mov esi,error_prefix
  524     call    display_string
  525     pop esi
  526     call    display_string
  527     mov esi,error_suffix
  528     call    display_string
  529     mov al,2
  530     jmp exit_program
  531 
  532 make_timestamp:
  533     mov r13d,edi
  534     mov eax,201
  535     mov edi,timestamp
  536     syscall
  537     mov eax,dword [timestamp]
  538     mov edx,dword [timestamp+4]
  539     mov edi,r13d
  540     ret
  541 
  542 error_prefix db 'error: ',0
  543 error_suffix db '.'
  544 lf db 0xA,0
  545 line_number_start db ' [',0
  546 line_data_start db ':',0xA,0
  547 preprocessed_instruction_prefix db 'processed: ',0