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