Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global _start
  2.  
  3. section .data
  4.  
  5. format: db "%c %s", 10, 0
  6. string: db "love", 10, 0
  7. string_len: equ $ - string
  8. buf: resb 64d
  9.  
  10. section .text
  11.  
  12. %define NEXT_PARAM R8
  13. %define NEXT_FORMAT_SYMBOL RBP
  14. %define BUFFER_SIZE RBX
  15.  
  16. %macro printf 1-*           ;macro with undefined qty of params
  17.     %rep %0                 ;loop at all parameters
  18.         %rotate -1          ;%1 now is the last parameter
  19.         push %1             ;push last param into stack
  20.     %endrep
  21.     call handle
  22. %endmacro
  23.  
  24. _start:
  25.         xor BUFFER_SIZE, BUFFER_SIZE
  26.         ;printf format, string, 3802, 100, '!', 127
  27.         printf format, 'c', string
  28. ;------------------------------------------------------------\
  29. ;ENTRY:
  30. ;DESTR:
  31. ;------------------------------------------------------------|
  32. handle:
  33.         mov NEXT_PARAM, RSP
  34.         add NEXT_PARAM, 8              ;[NEXT_PARAM] now is the last pushed arg -- format address
  35.         mov NEXT_FORMAT_SYMBOL, [NEXT_PARAM]          ;NEXT_FORMAT_SYMBOL points to current format symbol
  36.         add NEXT_PARAM, 8              ;NEXT_PARAM is current arg
  37.  
  38.         dec NEXT_FORMAT_SYMBOL
  39. next:  
  40.         inc NEXT_FORMAT_SYMBOL
  41.  
  42.         cmp byte [NEXT_FORMAT_SYMBOL], 0       ;program ends by 0-symbol in format string
  43.         je end_program
  44.  
  45.         cmp byte [NEXT_FORMAT_SYMBOL], '%'     ;if not '%' print source format symbol
  46.         jne .simple
  47.        
  48.         inc NEXT_FORMAT_SYMBOL
  49.        
  50.  
  51.         ;switch case
  52.         cmp byte [NEXT_FORMAT_SYMBOL], '%'     ;case %%
  53.         je .simple
  54.  
  55.         cmp byte [NEXT_FORMAT_SYMBOL], 'c'
  56.         je .print_char
  57.  
  58.         cmp byte [NEXT_FORMAT_SYMBOL], 's'
  59.         je .print_string
  60.  
  61.         cmp byte [NEXT_FORMAT_SYMBOL], 'd'
  62.         je .print_decimal
  63.  
  64.         cmp byte [NEXT_FORMAT_SYMBOL], 'b'
  65.         je .print_binary
  66.  
  67.         cmp byte [NEXT_FORMAT_SYMBOL], 'o'
  68.         je .print_octal
  69.  
  70.         cmp byte [NEXT_FORMAT_SYMBOL], 'x'
  71.         je .print_hex
  72.  
  73. .simple:        
  74.         mov RAX, [NEXT_FORMAT_SYMBOL]
  75.         call to_buf
  76.         jmp next
  77.  
  78. .print_char:
  79.         mov RAX, [NEXT_PARAM]
  80.         add NEXT_PARAM, 8
  81.         call to_buf
  82.         jmp next
  83.        
  84.  
  85. .print_string:
  86.         mov RSI, [NEXT_PARAM]
  87.         add NEXT_PARAM, 8
  88. .str_loop:
  89.         mov RAX, [RSI]
  90.         cmp RAX, 0
  91.         je next
  92.  
  93.         call to_buf
  94.         inc RSI
  95.         jmp .str_loop
  96.        
  97.  
  98. .print_decimal:
  99.         mov RAX, [NEXT_PARAM]
  100.         ;making the mask for last bit
  101.         xor R9, R9      
  102.         inc R9
  103.         shl R9, 31d
  104.  
  105.         ;looking for the sign of the number
  106.         mov R10, [NEXT_PARAM]  
  107.         and R10, R9
  108.         cmp R9, R10
  109.         jne .positive_decimal
  110.  
  111.         ;print minus
  112.         push RAX
  113.         mov RAX, '-'    
  114.         call to_buf
  115.         pop RAX
  116.  
  117.         ;get a positive value from negative
  118.         not RAX        
  119.         inc RAX        
  120. .positive_decimal:
  121.         mov R9, 10d
  122.         call print_number
  123.         add NEXT_PARAM, 8
  124.         jmp next
  125.  
  126.  
  127. .print_binary:
  128.         call printf_symbols
  129.         jmp next
  130.        
  131. .print_octal:
  132.         call printf_symbols
  133.         jmp next
  134.  
  135. .print_hex:
  136.         call printf_symbols
  137.         jmp next
  138.        
  139. ;------------------------------------------------------------/
  140.  
  141.  
  142. ;------------------------------------------------------------\
  143. ;Prints a positive number
  144. ;ENTRY: R9 -- base
  145. ;       RAX -- number to print
  146. ;------------------------------------------------------------|
  147. print_number:
  148.         xor R10, R10            ;reversed value
  149.         xor RDX, RDX
  150.         xor RCX, RCX            ;number of digits
  151.         mov R11, RAX
  152.  
  153. .reverse_loop:
  154.         ;reverse digits in number
  155.         mov RAX, R11
  156.         div R9d
  157.  
  158.         shl R10, 4
  159.         add R10, RDX
  160.  
  161.         mov R11, RAX
  162.         inc RCX
  163.  
  164.         cmp R11, R9
  165.         jae .reverse_loop
  166.  
  167.         shl R10, 4
  168.         add R10, R11
  169.         inc RCX
  170.  
  171. .print_loop:
  172.         mov RAX, R10
  173.         and RAX, 1111b
  174.         add RAX, '0'
  175.         call to_buf
  176.  
  177.         shr R10, 4
  178.         dec RCX
  179.         cmp RCX, 0
  180.         jne .print_loop
  181.  
  182.         ret
  183. ;------------------------------------------------------------/
  184.  
  185.  
  186. ;------------------------------------------------------------\
  187. ;Puts a symbol into buf
  188. ;ENTRY: RAX - char to put
  189. ;------------------------------------------------------------|
  190. to_buf:
  191.         push RSI
  192.         cmp BUFFER_SIZE, 64d
  193.         jae from_buf
  194.  
  195.         mov RDI, buf
  196.         add RDI, BUFFER_SIZE
  197.         stosb
  198.         inc BUFFER_SIZE
  199.        
  200.         pop RSI
  201.         ret
  202.  
  203. ;------------------------------------------------------------/
  204.  
  205. ;------------------------------------------------------------\
  206. ;Prints a buffer's content on a screen
  207. ;------------------------------------------------------------|
  208. from_buf:
  209.         push RDX
  210.         push RSI
  211.         cmp BUFFER_SIZE, 0
  212.         je .quit
  213.        
  214.         mov RDX, BUFFER_SIZE
  215.         mov RSI, buf
  216.         call printf_symbols
  217.         xor BUFFER_SIZE, BUFFER_SIZE
  218. .quit:
  219.         pop RSI
  220.         pop RDX
  221.         ret
  222. ;------------------------------------------------------------/
  223.  
  224. ;------------------------------------------------------------\
  225. ;ENTRY: RSI -- symbol address, RDX -- qty of symbols to print
  226. ;------------------------------------------------------------|
  227. printf_symbols:
  228.         push RAX
  229.         push RDI
  230.  
  231.         xor RAX, RAX
  232.         inc RAX
  233.  
  234.         xor RDI, RDI
  235.         inc RDI
  236.  
  237.         syscall
  238.        
  239.         pop RDI
  240.         pop RAX
  241.         ret
  242. ;------------------------------------------------------------/
  243.  
  244. end_program:
  245.         call from_buf
  246.         mov RAX, 60
  247.         xor RDI, RDI
  248.         syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement