Ladies_Man

#ccc God have mercy pls

Jan 4th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==========================================
  2. #------------------data--------------------
  3. #==========================================
  4. .section .data
  5.  
  6. msg:
  7.     .ascii "hallo world"
  8.     .set len, .-msg
  9.  
  10. RTLWriteIntegerBuffer:
  11.     .byte 0x90,0x8d,0x40,0x00
  12.     .byte 0x90,0x8d,0x40,0x00
  13.     .byte 0x90,0x8d,0x40,0x00
  14.            
  15. IsEOF:
  16.     .byte 0    
  17.    
  18. #==========================================
  19. #-------------------bss--------------------
  20. #==========================================    
  21. .bss
  22.  
  23.    .lcomm ReadCharBuffer,   1
  24.    .lcomm ReadCharInited,   1
  25.    .lcomm ReadCharBytesRead,4
  26.    
  27. #==========================================
  28. #------------------text--------------------
  29. #==========================================
  30. .section .text
  31.  
  32. .globl _start
  33. _start:
  34.     jmp zaloop
  35.  
  36.  
  37. #------------------------------------------
  38. #----------------WriteChar-----------------
  39. #------------------------------------------
  40. RTLWriteChar:
  41.     pushq %rbp
  42.     movq %rsp,   %rbp    #better use base ptr to stack frame
  43.                         #instead of stack itself
  44.     movq $1,    %rax    #syscall №
  45.     movq $1,    %rdi    #param1, fd
  46.     movq %rsp,  %rsi    #p2, buf
  47.     addq $16,   %rsi    #stack:[0ret,8rbp,16arg]
  48.     movq $1,    %rdx    #p3, count
  49.  
  50.     syscall
  51.    
  52.     popq %rbp
  53.     ret
  54.    
  55. #------------------------------------------
  56. #--------------WriteInteger----------------
  57. #------------------------------------------    
  58. RTLWriteInteger:
  59.     pushq %rbp
  60.     movq %rsp,  %rbp
  61.    
  62.     movq 16(%rbp),  %rbx    #arg: count
  63.     movq 24(%rbp),  %rax    #arg: num
  64.    
  65.     cmpq $0,    %rax
  66.     jnl RTLWriteIntegerNotSigned
  67.        
  68.         negq %rax
  69.         decq %rbx
  70.         pushq $'-'              #dont forget to pop
  71.         call RTLWriteChar
  72.         addq $8, %rsp            #popped!
  73.    
  74.     RTLWriteIntegerNotSigned:
  75.     xorq %rcx,  %rcx
  76.     pushq %rax
  77.     pushq %rbx
  78.    
  79.     RTLWriteIntegerPreCheckLoop:
  80.         testq %rax, %rax
  81.         jz RTLWriteIntegerPreCheckLoopDone
  82.         incq %rcx
  83.         movq $10,   %rbx
  84.         xorq %rdx,  %rdx
  85.         idiv %rbx          #suffix here q?
  86.    
  87.     RTLWriteIntegerPreCheckLoopDone:
  88.     testq %rcx, %rcx
  89.     setz %dl
  90.     orb %dl,    %cl
  91.    
  92.     popq %rbx
  93.     popq %rax
  94.     subq %rcx,  %rbx
  95.    
  96.     cmpq $0,    %rbx
  97.     jle RTLWriteIntegerNotPadding
  98.         pushq %rcx
  99.    
  100.         RTLWriteIntegerPaddingLoop:
  101.             pushq $' '              #pop!
  102.             call RTLWriteChar
  103.             addq $8,    %rsp        #popped
  104.             decq %rbx
  105.         jnz RTLWriteIntegerPaddingLoop
  106.         popq %rcx
  107.    
  108.     RTLWriteIntegerNotPadding:
  109.     leaq RTLWriteIntegerBuffer-1(%rcx), %rdi
  110.     #LEA EDI,[OFFSET RTLWriteIntegerBuffer+ECX-1]
  111.     pushq %rcx
  112.    
  113.     RTLWriteIntegerLoop:
  114.         movq $10,   %rsi
  115.         xorq %rdx,  %rdx
  116.         idiv %rsi
  117.         leaq '0'(%rdx), %rbx
  118.         movb (%rdi), %bl
  119.         decq %rdi
  120.     loop RTLWriteIntegerLoop
  121.     popq %rcx
  122.    
  123.     #invoke pop ret
  124.    
  125. #------------------------------------------
  126. #-----------------WriteLn------------------
  127. #------------------------------------------    
  128. RTLWriteLn:
  129.     pushq %rbp
  130.     movq %rsp,  %rbp    
  131.    
  132.     pushq $13
  133.     call RTLWriteChar
  134.     addq $8,    %rsp
  135.    
  136.     pushq $10
  137.     call RTLWriteChar
  138.     addq $8,    %rsp
  139.    
  140.     popq %rbp
  141.     ret
  142.    
  143.      
  144. ReadCharEx:
  145.     pushq %rbp
  146.     movq %rsp,  %rbp
  147.    
  148.     movq $0,    %rax
  149.     movq $0,    %rdi
  150.     movq $ReadCharBuffer,  %rsi  
  151.     movq $1,    %rdx
  152.     syscall
  153.  
  154.     movq (ReadCharBuffer), %rax
  155.    
  156.     /*cmpq $'x', %rax
  157.     je a1
  158.     movq $1, %rax
  159.     movq $1, %rdi
  160.     movq $msg, %rsi
  161.     movq $1, %rdx
  162.     syscall
  163.     jmp end  
  164.     a1:
  165.     movq $1, %rax
  166.     movq $1, %rdi
  167.     movq $msg, %rsi
  168.     movq $2, %rdx
  169.     syscall
  170.     end:*/
  171.     popq %rbp
  172.     ret
  173.    
  174. ReadCharInit:
  175.     cmpb $0,    ReadCharInited
  176.     jnz ReadInitDone
  177.     call ReadCharEx
  178.     movq $1,    ReadCharInited
  179.     ReadInitDone:
  180.     ret
  181.  
  182. #------------------------------------------
  183. #----------------ReadChar------------------
  184. #------------------------------------------    
  185. RTLReadChar:
  186.     movzx ReadCharBuffer,   %rax    #also dont need. already x64
  187.     call ReadCharEx         #do we really need
  188.                             #that shit
  189.     ret
  190.  
  191. #------------------------------------------
  192. #--------------ReadInteger-----------------
  193. #------------------------------------------    
  194. RTLReadInteger:
  195.     call ReadCharInit
  196.    
  197.     pushq %rax
  198.     pushq %rbx
  199.     pushq %rcx
  200.     pushq %rdx
  201.     pushq %rsi
  202.     pushq %rdi
  203.     pushq %rbp
  204.     movq %rsp,  %rbp
  205.    
  206.     xorq %rax,  %rax
  207.     xorq %rbx,  %rbx
  208.     leaq 1(%rax,%rbx,1),  %rcx
  209.    
  210.     ReadIntegerSkipWhiteSpace:
  211.         cmpb $0,    IsEOF
  212.         jnz ReadIntegerDone
  213.         cmpb $0,    (ReadCharBuffer)
  214.         je ReadIntegerSkipWhiteSpaceDone
  215.         cmpb $32,   (ReadCharBuffer)
  216.         ja ReadIntegerSkipWhiteSpaceDone
  217.         call ReadCharEx
  218.         jmp ReadIntegerSkipWhiteSpace
  219.        
  220.     ReadIntegerSkipWhiteSpaceDone:
  221.     cmpb $'-',  (ReadCharBuffer)
  222.     jne ReadIntegerNotSigned
  223.        
  224.         negq %rcx
  225.         call ReadCharEx
  226.        
  227.     ReadIntegerNotSigned:
  228.     ReadIntegerLoop:
  229.         movzx ReadCharBuffer,  %rbx
  230.         cmpb $'0',  %bl
  231.         jb ReadIntegerDone
  232.         cmpb $'9',  %bl
  233.         ja ReadIntegerDone
  234.         imul $10,   %rax
  235.         lea -'0'(%rax,%rbx,1),  %rax
  236.         call ReadCharEx
  237.         jmp ReadIntegerLoop
  238.    
  239.     ReadIntegerDone:
  240.     imul %rcx
  241.    
  242.     popq %rbp
  243.     popq %rdi
  244.     popq %rsi
  245.     popq %rdx
  246.     popq %rcx
  247.     popq %rbx
  248.     popq %rax
  249.    
  250.     ret
  251.  
  252. #------------------------------------------
  253. #------------------ReadLn------------------
  254. #------------------------------------------        
  255. RTLReadLn:
  256.     call ReadCharInit
  257.     cmpb $0,    IsEOF
  258.     jne ReadLnDone
  259.     movb ReadCharBuffer,    %bl    
  260.     cmpb $10,   %bl
  261.     je ReadLnDone
  262.     call ReadCharEx
  263.     jmp RTLReadLn
  264.     ReadLnDone:
  265.    
  266.     ret
  267.  
  268. #------------------------------------------
  269. #-------------------EOF--------------------
  270. #------------------------------------------        
  271. RTLEOF:
  272.     movzx IsEOF,    %rax
  273.     ret
  274.  
  275. #------------------------------------------
  276. #------------------EOLN--------------------
  277. #------------------------------------------    
  278. RTLEOLN:
  279.     cmpb $10,   ReadCharBuffer
  280.     seteb %bl
  281.     ret
  282.  
  283. #------------------------------------------
  284. #------------------Halt--------------------
  285. #------------------------------------------        
  286. RTLHalt:
  287.     movq $60,   %rax
  288.     movq $0,    %rdi
  289.     syscall
  290.    
  291. zaloop:
  292.     call RTLWriteLn
  293.    
  294.     call ReadCharEx
  295.    
  296.     pushq %rax
  297.     call RTLWriteChar
  298.     addq $8,    %rsp
  299.        
  300.     movq $'D',  %rax
  301.     pushq %rax
  302.     call RTLWriteChar
  303.     popq %rax       #clear stack
  304.    
  305.     call RTLHalt
Advertisement
Add Comment
Please, Sign In to add comment