Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
166
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 "I %s %x %d%%%c%b", 10, 0
  6. string:     db "love", 0
  7. buf: resb 64d
  8.  
  9. section .text
  10.  
  11. %define NEXT_PARAM R8
  12. %define NEXT_FORMAT_SYMBOL RBP
  13. %define BUFFER_SIZE RBX
  14.  
  15. %macro printf 1-*           ;macro with undefined qty of params
  16.     %rep %0                 ;loop at all parameters
  17.         %rotate -1          ;%1 now is the last parameter
  18.         push %1             ;push last param into stack
  19.     %endrep
  20.     call handle
  21.  
  22. %endmacro
  23.  
  24. _start:
  25.         xor BUFFER_SIZE, BUFFER_SIZE
  26.         printf format, string, 3802, 100, '!', 127
  27.         ;printf format, 16, 16, 14
  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. ;adds next format string symbol to buffer
  74. ;===================================================|
  75. simple:        
  76.         mov RAX, [NEXT_FORMAT_SYMBOL]
  77.         call to_buf
  78.         jmp next
  79. ;===================================================/
  80.  
  81.  
  82.  
  83. ;===================================================\
  84. ;adds a char located in next param to buffer
  85. ;===================================================|
  86. print_char:
  87.         mov RAX, [NEXT_PARAM]
  88.         add NEXT_PARAM, 8
  89.         call to_buf
  90.         jmp next
  91. ;===================================================/
  92.  
  93.  
  94.  
  95.        
  96. ;===================================================\
  97. ;adds string to buffer
  98. ;===================================================|
  99. print_string:
  100.         mov RSI, [NEXT_PARAM]
  101.         add NEXT_PARAM, 8
  102. .str_loop:
  103.         mov AL, [RSI]
  104.         cmp AL, 0
  105.         je next
  106.  
  107.         call to_buf
  108.         inc RSI
  109.         jmp .str_loop
  110. ;===================================================/
  111.  
  112.  
  113. ;===================================================\
  114. ;puts next agr as binary number to buf
  115. ;ENTRY: RAX -- value
  116. ;       CL -- base (degree of 2)
  117. ;DESTR: R13, R14, CH
  118. ;===================================================|
  119. print_binary:
  120.         mov RAX, [NEXT_PARAM]
  121.         add NEXT_PARAM, 8
  122.         mov CL, 1
  123.         call print_degree
  124.         jmp next
  125. ;===================================================/
  126.  
  127.  
  128. ;===================================================\
  129. ;puts next agr as octal number to buf
  130. ;ENTRY: RAX -- value
  131. ;       CL -- base (degree of 2)
  132. ;DESTR: R13, R14, CH
  133. ;===================================================|      
  134. print_octal:
  135.         mov RAX, [NEXT_PARAM]
  136.         add NEXT_PARAM, 8
  137.         mov CL, 3
  138.         call print_degree
  139.         jmp next
  140. ;===================================================/
  141.  
  142. ;===================================================\
  143. ;puts next agr as hex number to buf
  144. ;ENTRY: RAX -- value
  145. ;       CL -- base (degree of 2)
  146. ;DESTR: R13, R14, CH
  147. ;===================================================|
  148. print_hex:
  149.         mov RAX, [NEXT_PARAM]
  150.         add NEXT_PARAM, 8
  151.         mov CL, 4
  152.         call print_degree
  153.         jmp next
  154. ;===================================================/
  155.  
  156. ;===================================================\
  157. ;ENTRY: RAX -- value
  158. ;       CL -- base (degree of 2)
  159. ;DESTR: R13, R14, CH
  160. ;===================================================|
  161. print_degree:
  162.         mov CH, CL
  163.         xor R13, R13            ;R13 -- corresponding base
  164. .prepare_base:
  165.         shl R13, 1
  166.         inc R13
  167.         dec CH
  168.         cmp CH, 0
  169.         ja .prepare_base
  170.  
  171.         xor R10, R10            ;R10 -- reversed value
  172.         xor CH, CH              ;counter
  173.  
  174. .reverse_loop:
  175.         mov R14, RAX
  176.         and R14, R13
  177.  
  178.         shl R10, cl
  179.         add R10, R14
  180.  
  181.         shr RAX, CL
  182.         inc CH
  183.  
  184.         cmp RAX, 0
  185.         jne .reverse_loop
  186.  
  187. .print_loop:
  188.         mov R14, R10
  189.         and R14, R13
  190.  
  191.         add R14, '0'
  192.  
  193.         cmp R14, '9'
  194.         jna .digit
  195.         add R14, 'A' - '0' - 10
  196.  
  197. .digit:
  198.         mov RAX, R14
  199.         call to_buf
  200.  
  201.         shr R10, CL
  202.         dec CH
  203.         cmp CH, 0
  204.         jne .print_loop
  205.  
  206.         ret
  207. ;===================================================/
  208.  
  209.  
  210. ;===================================================\
  211. ;adds decimal number to buffer as symbols array
  212. ;===================================================|
  213. print_decimal:
  214.         mov RAX, [NEXT_PARAM]
  215.         ;making the mask for last bit
  216.         xor R9, R9      
  217.         inc R9
  218.         shl R9, 31d
  219.  
  220.         ;looking for the sign of the number
  221.         mov R10, [NEXT_PARAM]  
  222.         and R10, R9
  223.         cmp R9, R10
  224.         jne .positive_decimal
  225.  
  226.         ;print minus
  227.         push RAX
  228.         mov RAX, '-'    
  229.         call to_buf
  230.         pop RAX
  231.  
  232.         ;get a positive value from negative
  233.         not RAX
  234.         inc RAX
  235.  
  236.        
  237. .positive_decimal:
  238.         mov R9, 10d
  239.         call print_number
  240.         add NEXT_PARAM, 8
  241.         jmp next
  242. ;===================================================/
  243.  
  244. ;===================================================\
  245. ;Prints a positive decimal number
  246. ;ENTRY: R9 -- base
  247. ;       RAX -- number to print
  248. ;DESTR: CH, R10, R11, RAX
  249. ;===================================================|
  250. print_number:
  251.         xor R10, R10            ;reversed value
  252.         xor CH, CH              ;number of digits
  253.         mov R11, RAX
  254.  
  255. .reverse_loop:
  256.  
  257.         ;reverse digits in number
  258.         mov RAX, R11
  259.         div R9d
  260.  
  261.         shl R10, 4              ;log 2 16
  262.  
  263.         mov R11, RAX
  264.         inc CH
  265.  
  266.         cmp R11, R9
  267.         jae .reverse_loop
  268.  
  269.         shl R10, 4
  270.         add R10, R11
  271.         inc CH
  272.  
  273. .print_loop:
  274.         mov RAX, R10
  275.         and RAX, 1111b
  276.         add RAX, '0'
  277.         call to_buf
  278.  
  279.         shr R10, 4
  280.         dec CH
  281.         cmp CH, 0
  282.         jne .print_loop
  283.  
  284.         ret
  285. ;===================================================/
  286.  
  287.  
  288. ;===================================================\
  289. ;Puts a symbol into buf
  290. ;ENTRY: RAX - char to put
  291. ;DESTR: RDI
  292. ;===================================================|
  293. to_buf:
  294.         cmp BUFFER_SIZE, 64d
  295.         jae from_buf
  296.  
  297.         mov RDI, buf
  298.         add RDI, BUFFER_SIZE
  299.         stosb
  300.         inc BUFFER_SIZE
  301.        
  302.         ret
  303. ;===================================================/
  304.  
  305. ;===================================================\
  306. ;Prints a buffer's content on a screen
  307. ;===================================================|
  308. from_buf:
  309.         push RDX
  310.         push RSI
  311.         cmp BUFFER_SIZE, 0
  312.         je .quit
  313.        
  314.         mov RDX, BUFFER_SIZE
  315.         mov RSI, buf
  316.         call printf_symbols
  317.         xor BUFFER_SIZE, BUFFER_SIZE
  318. .quit:
  319.         pop RSI
  320.         pop RDX
  321.         ret
  322. ;===================================================/
  323.  
  324. ;===================================================\
  325. ;prints RDX symbols from [RSI]
  326. ;ENTRY: RSI -- symbol address, RDX -- qty of symbols to print
  327. ;===================================================|
  328. printf_symbols:
  329.         push RAX
  330.         push RDI
  331.  
  332.         xor RAX, RAX
  333.         inc RAX
  334.  
  335.         xor RDI, RDI
  336.         inc RDI
  337.  
  338.         syscall
  339.        
  340.         pop RDI
  341.         pop RAX
  342.         ret
  343. ;===================================================/
  344.  
  345. ;===================================================\
  346. ;ends a program
  347. ;===================================================|
  348. end_program:
  349.         call from_buf
  350.         mov RAX, 60
  351.         xor RDI, RDI
  352.         syscall
  353. ;===================================================/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement