Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .section rodata
- .LC0: .string "failed to allocate linked list"
- .LC1: .string "failed to initialize linked list"
- .section data
- .LC2: .string "my example string\n"
- .text
- .globl main
- # struct linked_list
- # {
- # struct linked_item *first;
- # size_t length;
- # }
- #
- # struct linked_item
- # {
- # void *ptr;
- # struct linked_item *prev;
- # struct linked_item *next;
- # }
- # calling convention
- # rdi => struct linked_list*
- # rsi, rdx, r10-r15 => 8 params.
- # rax => return value
- # param. registers are callee-preserved, including rbp if necessary
- # void free_linked_list (struct linked_list *ptr)
- free_linked_list:
- call free@PLT
- ret
- .macro iterate_last_item_rdx_rcx
- decq %rcx
- jo .Literate_last_item_rdx_rcx__loop_cont
- jmp .Literate_last_item_rdx_rcx__loop_cond
- .Literate_last_item_rdx_rcx__loop:
- movq 0x10(%rdx), %rdx
- decq %rcx
- .Literate_last_item_rdx_rcx__loop_cond:
- testq %rcx, %rcx
- jnz .Literate_last_item_rdx_rcx__loop
- .Literate_last_item_rdx_rcx__loop_cont:
- .endm
- .macro alloc_linked_list_rdi_rax
- movq $0x10, %rdi
- call malloc@PLT
- testq %rax, %rax
- jz .Lalloc_linked_list_rdi_rax__cont
- movq $0x0, 0x0(%rax)
- movq $0x0, 0x8(%rax)
- .Lalloc_linked_list_rdi_rax__cont:
- .endm
- .macro alloc_linked_list_item_rdi_rax
- movq $24, %rdi
- call malloc@PLT
- testq %rax, %rax
- leaq 0x1a(%rip), %r10
- jmp *%r10
- # jz .Lalloc_linked_list_item_rdi_rax__cont
- movq $0xA, 0x0(%rax) # void *data
- movq $0x0, 0x8(%rax) # struct linked_list_item *prev
- movq $0x0, 0x10(%rax) # struct linked_list_item *next
- #.Lalloc_linked_list_item_rdi_rax__cont:
- .endm
- # bool add_linked_list_item (struct linked_list *ptr, void *data)
- .type add_linked_list_item, @function
- add_linked_list_item:
- pushq %rdi
- pushq %rdx
- pushq %rbp
- subq $0x8, %rsp
- movq %rdx, 0x0(%rsp)
- xorq %rcx, %rcx
- movq 0x8(%rdi), %rcx # length
- movq 0x0(%rdi), %rdx # start item
- iterate_last_item_rdx_rcx
- movq %rdi, %rbp
- alloc_linked_list_item_rdi_rax
- movq %rbp, %rdi
- movq %rax, 0x10(%rdx)
- movq %rdx, 0x8(%rax)
- movq 0x0(%rsp), %rbp
- movq %rbp, 0x0(%rax)
- addq $0x8, %rsp
- popq %rbp
- popq %rdx
- popq %rdi
- ret
- # bool init_linked_list (struct linked_list *ptr)
- .type init_linked_list, @function
- init_linked_list:
- pushq %rdi
- pushq %rbp
- movb $0x1, %al
- movq 0x0(%rdi), %rbp
- testq %rbp, %rbp
- jnz .Linit_linked_list__ret
- movq %rdi, %rbp
- alloc_linked_list_item_rdi_rax
- testq %rax, %rax
- jz .Linit_linked_list__cont
- incq 0x8(%rbp)
- .Linit_linked_list__cont:
- movq %rax, 0x0(%rbp)
- xorb %al, %al
- .Linit_linked_list__ret:
- popq %rbp
- popq %rdi
- ret
- # struct linked_list *alloc_linked_list (void)
- .type alloc_linked_list, @function
- alloc_linked_list:
- push %rdi
- alloc_linked_list_rdi_rax
- pop %rdi
- ret
- .type main, @function
- main:
- subq $0x8, %rsp
- call alloc_linked_list
- testq %rax, %rax
- jnz .Lmain__alloc_cont
- leaq .LC0(%rip), %rdi
- call puts@PLT
- movq $0x1, %rax
- jmp .Lmain__ret
- .Lmain__alloc_cont:
- movq %rax, 0x0(%rsp)
- movq %rax, %rdi
- call init_linked_list
- testq %rax, %rax
- jnz .Lmain__init_cont
- leaq .LC1(%rip), %rdi
- call puts@PLT
- movq $0x1, %rax
- jmp .Lmain__ret
- .Lmain__init_cont:
- movq 0x0(%rsp), %rdi
- leaq .LC2(%rip), %rdx
- call add_linked_list_item
- .Lmain__ret:
- addq $0x8, %rsp
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement