Advertisement
Guest User

NASM CLI Flags

a guest
Jan 25th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bits 64
  2.  
  3. ; streams:
  4. %define STDIN       0
  5. %define STDOUT      1
  6. %define STDERR      2
  7.  
  8. ; syscalls:
  9. %define INPUT       0
  10. %define OPEN        2
  11. %define CLOSE       3
  12. %define WRITE       1
  13. %define EXIT        60
  14.  
  15. ; fopen modes:
  16. %define O_RDONLY    0
  17. %define O_WRONLY    1
  18. %define O_CREAT     64
  19.  
  20. section .text
  21.     global _start
  22.  
  23. _start:
  24.     pop     rax
  25.     mov     [argc], rax
  26.     cmp     rax, 2
  27.     jl      _argc_error
  28.  
  29.     xor     rdx, rdx
  30.     pop     rax
  31.     inc     rdx
  32.  
  33. _args_loop:
  34.     inc     rdx
  35.     cmp     rdx, [argc]
  36.     jg      _exit
  37.  
  38.     pop     rax
  39.     mov     bl, byte [rax]
  40.     cmp     bl, byte 45 ; flag start: -
  41.     je      _args_flag
  42.     jmp     _args_loop
  43.  
  44. _args_flag:
  45.     push    rax
  46.     push    rdx
  47.    
  48.     push    rax
  49.     mov     rdi, out_flag
  50.     mov     rsi, rax
  51.     call    _strcmp
  52.     cmp     rax, 0
  53.     jne     _args_loop
  54.     pop     rax
  55.  
  56.     push    rax
  57.     mov     rdi, rax
  58.     call    _strlen
  59.  
  60.     ; print the name of the flag if it is equal to out_flag
  61.     mov     rdx, rax
  62.     mov     rax, WRITE
  63.     mov     rdi, STDOUT
  64.     pop     rsi
  65.     syscall
  66.  
  67.     mov     rax, WRITE
  68.     mov     rdi, STDOUT
  69.     mov     rsi, endl
  70.     mov     rdx, 1
  71.     syscall
  72.  
  73.     pop     rdx
  74.     pop     rax
  75.  
  76.     jmp     _args_loop
  77.  
  78. _exit:
  79.     mov     rax, EXIT
  80.     mov     rdi, 0
  81.     syscall
  82.  
  83.     ret
  84.  
  85. ; print errors:
  86. _argc_error:
  87.     mov     rdi, argc_error
  88.     call    _strlen
  89.  
  90.     mov     rdx, rax
  91.     mov     rax, WRITE
  92.     mov     rdi, STDERR
  93.     mov     rsi, argc_error
  94.     syscall
  95.    
  96.     jmp     _exit_err
  97.  
  98. _flag_error:
  99.     mov     rdi, flag_error
  100.     call    _strlen
  101.  
  102.     mov     rdx, rax
  103.     mov     rax, WRITE
  104.     mov     rdi, STDERR
  105.     mov     rsi, flag_error
  106.     syscall
  107.    
  108.     jmp     _exit_err
  109.  
  110. _exit_err:
  111.     mov     rax, EXIT
  112.     mov     rdi, 1
  113.     syscall
  114.     ret
  115.  
  116. ; Functions
  117.  
  118. ; function _strlen (string)
  119. ; returns value to rax (the length of the string)
  120. _strlen:
  121.     push    rcx
  122.     xor     rcx, rcx
  123.    
  124.     _strlen_next:
  125.         cmp [rdi], byte 0
  126.         jz  _strlen_null
  127.  
  128.         inc rdi
  129.         inc rcx
  130.         jmp _strlen_next
  131.  
  132.     _strlen_null:
  133.         mov     rax, rcx
  134.         pop     rcx
  135.  
  136.     ret
  137.  
  138. ; function _strcmp (string, string)
  139. ; returns value to rax (1 if equal, 0 if does not)
  140. _strcmp:
  141.     push    rdi
  142.     push    rsi
  143.  
  144.     push    rsi
  145.     push    rdi
  146.     mov     rdi, rsi
  147.     call    _strlen
  148.     mov     rbx, rax
  149.     pop     rdi
  150.     call    _strlen
  151.     pop     rsi
  152.     cmp     rbx, rax
  153.     jne     _streq_not_eq
  154.    
  155.     _strcmp_loop:
  156.         mov rax, [rdi]
  157.         sub rax, [rsi]
  158.         cmp rax, 0
  159.         jne _streq_not_eq
  160.  
  161.         cmp byte [rsi], 0
  162.         je  _streq_eq
  163.  
  164.         inc rdi
  165.         inc rsi
  166.         jmp _streq_loop
  167.  
  168.     _strcmp_not_eq:
  169.         mov     rax, 0
  170.         jmp     _streq_end
  171.  
  172.     _strcmp_eq:
  173.         mov     rax, 1
  174.         jmp     _streq_end
  175.  
  176.     _strcmp_end:
  177.         pop     rsi
  178.         pop     rdi
  179.    
  180.     ret
  181.  
  182. section .data
  183.     argc_error: db "Small count of CLI arguments passed!", 10, 0
  184.     flag_error: db "Unexpected flag passed", 10, 0
  185.     output_file: db "out.asm"
  186.     out_flag:   db "-o", 0
  187.     endl:       db 10
  188.    
  189. section .bss
  190.     input_file: resb 255
  191.     argc:       resb 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement