Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .section        rodata
  2. .LC0: .string "failed to allocate linked list"
  3. .LC1: .string "failed to initialize linked list"
  4.  
  5. .section        data
  6. .LC2: .string "my example string\n"
  7.  
  8.         .text
  9.         .globl  main
  10.  
  11.  
  12. # struct linked_list
  13. # {
  14. #   struct linked_item *first;
  15. #   size_t length;
  16. # }
  17. #
  18. # struct linked_item
  19. # {
  20. #   void *ptr;
  21. #   struct linked_item *prev;
  22. #   struct linked_item *next;
  23. # }
  24.  
  25. # calling convention
  26. # rdi                => struct linked_list*
  27. # rsi, rdx, r10-r15  => 8 params.
  28. # rax                => return value
  29. # param. registers are callee-preserved, including rbp if necessary
  30.  
  31. # void free_linked_list (struct linked_list *ptr)
  32. free_linked_list:
  33.         call free@PLT
  34.         ret
  35.  
  36. .macro iterate_last_item_rdx_rcx
  37.         decq %rcx
  38.         jo .Literate_last_item_rdx_rcx__loop_cont
  39.         jmp .Literate_last_item_rdx_rcx__loop_cond
  40. .Literate_last_item_rdx_rcx__loop:
  41.         movq 0x10(%rdx), %rdx
  42.         decq %rcx
  43. .Literate_last_item_rdx_rcx__loop_cond:
  44.         testq %rcx, %rcx
  45.         jnz .Literate_last_item_rdx_rcx__loop
  46. .Literate_last_item_rdx_rcx__loop_cont:
  47. .endm
  48.  
  49. .macro alloc_linked_list_rdi_rax
  50.         movq $0x10, %rdi
  51.         call malloc@PLT
  52.         testq %rax, %rax
  53.         jz .Lalloc_linked_list_rdi_rax__cont
  54.         movq $0x0, 0x0(%rax)
  55.         movq $0x0, 0x8(%rax)
  56. .Lalloc_linked_list_rdi_rax__cont:
  57. .endm
  58.  
  59. .macro alloc_linked_list_item_rdi_rax
  60.         movq $24, %rdi
  61.         call malloc@PLT
  62.         testq %rax, %rax
  63.         leaq 0x1a(%rip), %r10
  64.         jmp *%r10
  65. #       jz .Lalloc_linked_list_item_rdi_rax__cont
  66.         movq $0xA, 0x0(%rax)  # void *data
  67.         movq $0x0, 0x8(%rax)  # struct linked_list_item *prev
  68.         movq $0x0, 0x10(%rax) # struct linked_list_item *next
  69. #.Lalloc_linked_list_item_rdi_rax__cont:
  70. .endm
  71.  
  72.  
  73. # bool add_linked_list_item (struct linked_list *ptr, void *data)
  74.         .type add_linked_list_item, @function
  75. add_linked_list_item:
  76.         pushq %rdi
  77.         pushq %rdx
  78.         pushq %rbp
  79.         subq $0x8, %rsp
  80.  
  81.         movq %rdx, 0x0(%rsp)
  82.         xorq %rcx, %rcx
  83.         movq 0x8(%rdi), %rcx  # length
  84.         movq 0x0(%rdi), %rdx  # start item
  85.         iterate_last_item_rdx_rcx
  86.  
  87.         movq %rdi, %rbp
  88.         alloc_linked_list_item_rdi_rax
  89.         movq %rbp, %rdi
  90.  
  91.         movq %rax, 0x10(%rdx)
  92.         movq %rdx, 0x8(%rax)
  93.         movq 0x0(%rsp), %rbp
  94.         movq %rbp, 0x0(%rax)
  95.  
  96.         addq $0x8, %rsp
  97.         popq %rbp
  98.         popq %rdx
  99.         popq %rdi
  100.         ret
  101.  
  102. # bool init_linked_list (struct linked_list *ptr)
  103.         .type init_linked_list, @function
  104. init_linked_list:
  105.         pushq %rdi
  106.         pushq %rbp
  107.         movb $0x1, %al
  108.         movq 0x0(%rdi), %rbp
  109.         testq %rbp, %rbp
  110.         jnz .Linit_linked_list__ret
  111.         movq %rdi, %rbp
  112.         alloc_linked_list_item_rdi_rax
  113.         testq %rax, %rax
  114.         jz .Linit_linked_list__cont
  115.         incq 0x8(%rbp)
  116. .Linit_linked_list__cont:
  117.         movq %rax, 0x0(%rbp)
  118.         xorb %al, %al
  119. .Linit_linked_list__ret:
  120.         popq %rbp
  121.         popq %rdi
  122.         ret
  123.  
  124. # struct linked_list *alloc_linked_list (void)
  125.         .type alloc_linked_list, @function
  126. alloc_linked_list:
  127.         push %rdi
  128.         alloc_linked_list_rdi_rax
  129.         pop %rdi
  130.         ret
  131.  
  132.         .type main, @function
  133. main:
  134.         subq $0x8, %rsp
  135.         call alloc_linked_list
  136.         testq %rax, %rax
  137.         jnz .Lmain__alloc_cont
  138.         leaq .LC0(%rip), %rdi
  139.         call puts@PLT
  140.         movq $0x1, %rax
  141.         jmp .Lmain__ret
  142. .Lmain__alloc_cont:
  143.         movq %rax, 0x0(%rsp)
  144.         movq %rax, %rdi
  145.         call init_linked_list
  146.         testq %rax, %rax
  147.         jnz .Lmain__init_cont
  148.         leaq .LC1(%rip), %rdi
  149.         call puts@PLT
  150.         movq $0x1, %rax
  151.         jmp .Lmain__ret
  152. .Lmain__init_cont:
  153.         movq 0x0(%rsp), %rdi
  154.         leaq .LC2(%rip), %rdx
  155.         call add_linked_list_item
  156.  
  157. .Lmain__ret:
  158.         addq $0x8, %rsp
  159.         ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement