"Fossies" - the Fresh Open Source Software Archive

Member "fasm/source/Linux/x64/system.inc" (21 Feb 2022, 8827 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 Linux x64
    3 ; Copyright (c) 1999-2022, 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 ecx,esi
  115     mov rbx,[environment]
  116       next_variable:
  117     mov rsi,[rbx]
  118     test    rsi,rsi
  119     jz  no_environment_variable
  120     add rbx,8
  121       compare_variable_names:
  122     mov edx,ecx
  123       compare_character:
  124     lodsb
  125     mov ah,[edx]
  126     inc edx
  127     cmp al,'='
  128     je  end_of_variable_name
  129     or  ah,ah
  130     jz  next_variable
  131     sub ah,al
  132     jz  compare_character
  133     cmp ah,20h
  134     jne next_variable
  135     cmp al,41h
  136     jb  next_variable
  137     cmp al,5Ah
  138     jna compare_character
  139     jmp next_variable
  140       no_environment_variable:
  141     ret
  142       end_of_variable_name:
  143     or  ah,ah
  144     jnz next_variable
  145       copy_variable_value:
  146     lodsb
  147     cmp edi,[memory_end]
  148     jae out_of_memory
  149     stosb
  150     or  al,al
  151     jnz copy_variable_value
  152     dec edi
  153     ret
  154 
  155 open:
  156     mov r12d,esi
  157     mov r13d,edi
  158     call    adapt_path
  159     mov eax,2
  160     mov edi,buffer
  161     mov esi,O_RDONLY
  162     xor edx,edx
  163     syscall
  164     mov esi,r12d
  165     mov edi,r13d
  166     test    eax,eax
  167     js  file_error
  168     mov ebx,eax
  169     clc
  170     ret
  171     adapt_path:
  172     mov esi,edx
  173     mov edi,buffer
  174       copy_path:
  175     lods    byte [esi]
  176     cmp al,'\'
  177     jne path_char_ok
  178     mov al,'/'
  179       path_char_ok:
  180     stos    byte [edi]
  181     or  al,al
  182     jnz copy_path
  183     cmp edi,buffer+1000h
  184     ja  out_of_memory
  185     ret
  186 create:
  187     mov r12d,esi
  188     mov r13d,edi
  189     mov r15d,edx
  190     call    adapt_path
  191     mov edi,buffer
  192     mov esi,O_CREAT+O_TRUNC+O_WRONLY
  193     mov edx,S_IRUSR+S_IWUSR+S_IRGRP+S_IROTH
  194     cmp r15d,[output_file]
  195     jne do_create
  196     cmp [output_format],5
  197     jne do_create
  198     bt  [format_flags],0
  199     jnc do_create
  200     or  edx,S_IXUSR+S_IXGRP+S_IXOTH
  201       do_create:
  202     mov eax,2
  203     syscall
  204     mov esi,r12d
  205     mov edi,r13d
  206     test    eax,eax
  207     js  file_error
  208     mov ebx,eax
  209     clc
  210     ret
  211 close:
  212     mov r13d,edi
  213     mov edi,ebx
  214     mov eax,3
  215     syscall
  216     mov edi,r13d
  217     ret
  218 read:
  219     mov r12d,esi
  220     mov r13d,edi
  221     mov eax,0
  222     mov edi,ebx
  223     mov esi,edx
  224     mov edx,ecx
  225     syscall
  226     mov ecx,edx
  227     mov edx,esi
  228     mov esi,r12d
  229     mov edi,r13d
  230     test    eax,eax
  231     js  file_error
  232     cmp eax,ecx
  233     jne file_error
  234     clc
  235     ret
  236     file_error:
  237     stc
  238     ret
  239 write:
  240     mov r12d,esi
  241     mov r13d,edi
  242     mov eax,1
  243     mov edi,ebx
  244     mov esi,edx
  245     mov edx,ecx
  246     syscall
  247     mov ecx,edx
  248     mov edx,esi
  249     mov esi,r12d
  250     mov edi,r13d
  251     test    eax,eax
  252     js  file_error
  253     clc
  254     ret
  255 lseek:
  256     mov r12d,esi
  257     mov r13d,edi
  258     mov edi,ebx
  259     mov esi,edx
  260     xor edx,edx
  261     mov dl,al
  262     mov eax,8
  263     syscall
  264     mov esi,r12d
  265     mov edi,r13d
  266     cmp eax,-1
  267     je  file_error
  268     clc
  269     ret
  270 
  271 display_string:
  272     mov edi,esi
  273     mov edx,esi
  274     or  ecx,-1
  275     xor al,al
  276     repne   scasb
  277     neg ecx
  278     sub ecx,2
  279     mov eax,1
  280     mov edi,[con_handle]
  281     mov esi,edx
  282     mov edx,ecx
  283     syscall
  284     ret
  285 display_character:
  286     mov r12d,esi
  287     mov r13d,edi
  288     mov [character],dl
  289     mov eax,1
  290     mov edi,[con_handle]
  291     mov esi,character
  292     mov edx,1
  293     syscall
  294     mov esi,r12d
  295     mov edi,r13d
  296     ret
  297 display_number:
  298     mov r14d,ebx
  299     mov ecx,1000000000
  300     xor edx,edx
  301     xor bl,bl
  302       display_loop:
  303     div ecx
  304     mov r15d,edx
  305     cmp ecx,1
  306     je  display_digit
  307     or  bl,bl
  308     jnz display_digit
  309     or  al,al
  310     jz  digit_ok
  311     not bl
  312       display_digit:
  313     mov dl,al
  314     add dl,30h
  315     mov r10d,ecx
  316     call    display_character
  317     mov ecx,r10d
  318       digit_ok:
  319     mov eax,ecx
  320     xor edx,edx
  321     mov ecx,10
  322     div ecx
  323     mov ecx,eax
  324     mov eax,r15d
  325     or  ecx,ecx
  326     jnz display_loop
  327     mov ebx,r14d
  328     ret
  329 
  330 display_user_messages:
  331     mov [displayed_count],0
  332     call    show_display_buffer
  333     cmp [displayed_count],0
  334     je  line_break_ok
  335     cmp [last_displayed],0Ah
  336     je  line_break_ok
  337     mov dl,0Ah
  338     call    display_character
  339       line_break_ok:
  340     ret
  341 display_block:
  342     jecxz   block_displayed
  343     add [displayed_count],ecx
  344     mov al,[esi+ecx-1]
  345     mov [last_displayed],al
  346     mov r13d,edi
  347     mov eax,1
  348     mov edi,[con_handle]
  349     mov edx,ecx
  350     syscall
  351     mov edi,r13d
  352       block_displayed:
  353     ret
  354 
  355 fatal_error:
  356     mov [con_handle],2
  357     mov esi,error_prefix
  358     call    display_string
  359     pop esi
  360     call    display_string
  361     mov esi,error_suffix
  362     call    display_string
  363     mov al,0FFh
  364     jmp exit_program
  365 assembler_error:
  366     mov [con_handle],2
  367     call    display_user_messages
  368     mov ebx,[current_line]
  369     test    ebx,ebx
  370     jz  display_error_message
  371     push    dword 0
  372       get_error_lines:
  373     mov eax,[ebx]
  374     cmp byte [eax],0
  375     je  get_next_error_line
  376     push    ebx
  377     test    byte [ebx+7],80h
  378     jz  display_error_line
  379     mov edx,ebx
  380       find_definition_origin:
  381     mov edx,[edx+12]
  382     test    byte [edx+7],80h
  383     jnz find_definition_origin
  384     push    edx
  385       get_next_error_line:
  386     mov ebx,[ebx+8]
  387     jmp get_error_lines
  388       display_error_line:
  389     mov esi,[ebx]
  390     call    display_string
  391     mov esi,line_number_start
  392     call    display_string
  393     mov eax,[ebx+4]
  394     and eax,7FFFFFFFh
  395     call    display_number
  396     mov dl,']'
  397     call    display_character
  398     pop esi
  399     cmp ebx,esi
  400     je  line_number_ok
  401     mov dl,20h
  402     call    display_character
  403     push    esi
  404     mov esi,[esi]
  405     movzx   ecx,byte [esi]
  406     inc esi
  407     call    display_block
  408     mov esi,line_number_start
  409     call    display_string
  410     pop esi
  411     mov eax,[esi+4]
  412     and eax,7FFFFFFFh
  413     call    display_number
  414     mov dl,']'
  415     call    display_character
  416       line_number_ok:
  417     mov esi,line_data_start
  418     call    display_string
  419     mov esi,ebx
  420     mov edx,[esi]
  421     call    open
  422     mov al,2
  423     xor edx,edx
  424     call    lseek
  425     mov edx,[esi+8]
  426     sub eax,edx
  427     jz  line_data_displayed
  428     push    eax
  429     xor al,al
  430     call    lseek
  431     mov ecx,[esp]
  432     mov edx,[additional_memory]
  433     lea eax,[edx+ecx]
  434     cmp eax,[additional_memory_end]
  435     ja  out_of_memory
  436     call    read
  437     call    close
  438     pop ecx
  439     mov esi,[additional_memory]
  440       get_line_data:
  441     mov al,[esi]
  442     cmp al,0Ah
  443     je  display_line_data
  444     cmp al,0Dh
  445     je  display_line_data
  446     cmp al,1Ah
  447     je  display_line_data
  448     or  al,al
  449     jz  display_line_data
  450     inc esi
  451     loop    get_line_data
  452       display_line_data:
  453     mov ecx,esi
  454     mov esi,[additional_memory]
  455     sub ecx,esi
  456     call    display_block
  457       line_data_displayed:
  458     mov esi,lf
  459     call    display_string
  460     pop ebx
  461     or  ebx,ebx
  462     jnz display_error_line
  463     cmp [preprocessing_done],0
  464     je  display_error_message
  465     mov esi,preprocessed_instruction_prefix
  466     call    display_string
  467     mov esi,[current_line]
  468     add esi,16
  469     mov edi,[additional_memory]
  470     xor dl,dl
  471       convert_instruction:
  472     lodsb
  473     cmp al,1Ah
  474     je  copy_symbol
  475     cmp al,22h
  476     je  copy_symbol
  477     cmp al,3Bh
  478     je  instruction_converted
  479     stosb
  480     or  al,al
  481     jz  instruction_converted
  482     xor dl,dl
  483     jmp convert_instruction
  484       copy_symbol:
  485     or  dl,dl
  486     jz  space_ok
  487     mov byte [edi],20h
  488     inc edi
  489       space_ok:
  490     cmp al,22h
  491     je  quoted
  492     lodsb
  493     movzx   ecx,al
  494     rep movsb
  495     or  dl,-1
  496     jmp convert_instruction
  497       quoted:
  498     mov al,27h
  499     stosb
  500     lodsd
  501     mov ecx,eax
  502     jecxz   quoted_copied
  503       copy_quoted:
  504     lodsb
  505     stosb
  506     cmp al,27h
  507     jne quote_ok
  508     stosb
  509       quote_ok:
  510     loop    copy_quoted
  511       quoted_copied:
  512     mov al,27h
  513     stosb
  514     or  dl,-1
  515     jmp convert_instruction
  516       instruction_converted:
  517     xor al,al
  518     stosb
  519     mov esi,[additional_memory]
  520     call    display_string
  521     mov esi,lf
  522     call    display_string
  523       display_error_message:
  524     mov esi,error_prefix
  525     call    display_string
  526     pop esi
  527     call    display_string
  528     mov esi,error_suffix
  529     call    display_string
  530     mov al,2
  531     jmp exit_program
  532 
  533 make_timestamp:
  534     mov r13d,edi
  535     mov eax,201
  536     mov edi,timestamp
  537     syscall
  538     mov eax,dword [timestamp]
  539     mov edx,dword [timestamp+4]
  540     mov edi,r13d
  541     ret
  542 
  543 error_prefix db 'error: ',0
  544 error_suffix db '.'
  545 lf db 0xA,0
  546 line_number_start db ' [',0
  547 line_data_start db ':',0xA,0
  548 preprocessed_instruction_prefix db 'processed: ',0