Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. .section .data
  2. .globl baseheap
  3. baseheap: .quad 0
  4. .section .text
  5.  
  6. .global meuMalloc
  7. .type meuMalloc, @function
  8.  
  9. meuMalloc:
  10.  
  11. #############################################################################################################################
  12. # Deve ocorrer uma checagem no começo se a base da heap for zero a instr. nunca executou e brk(0) indica o topo e a base da #
  13. # heap que deve ser o valor setado para baseheap. #
  14. #############################################################################################################################
  15.  
  16. pushq %rbp
  17. movq %rsp, %rbp
  18. subq $20, %rsp #-8(rbp) = aux, -16(rbp)= aux2, -20(rbp) = oldsize
  19. movq $12, %rax #syscall 12 obtem brk
  20. pushq %rdi #empilha size
  21. movq $0, %rdi
  22. syscall
  23. popq %rdi #recupera size
  24. cmpq $0, baseheap
  25. jne fimif
  26. movq %rax, baseheap
  27. fimif:
  28. cmpq $0, %rdi
  29. jg precondwhile
  30. movq $0, %rax
  31. addq $20, %rsp
  32. popq %rbp
  33. ret
  34. precondwhile:
  35. movq baseheap, %rax
  36. movq %rax, -8(%rbp) #aux = base da heap
  37. addq $1, -8(%rbp) #aux = base da heap + 1
  38. condwhile:
  39. movq $12, %rax #as proximas linhas obtem o ultimo endereco da heap
  40. pushq %rdi #empilha size
  41. movq $0, %rdi
  42. syscall
  43. popq %rdi #recupera size
  44. cmpq %rax, -8(%rbp) #se aux >= brk sai do loop
  45. jge endwhile
  46. movq -8(%rbp), %rbx #rbx = aux
  47. subq $1,%rbx #rbx = aux-1
  48. movb (%rbx), %cl #rcx = M[aux-1]
  49. cmp $1, %cl #rcx == OCUPADO?
  50. je endverif
  51. movq -8(%rbp), %rax #rax = aux
  52. movl (%rax), %eax #rax = *aux
  53. subq $5, %rax
  54. cmpl %eax, %edi #compara *(aux)-5 com size
  55. jle endwhile
  56. movq -8(%rbp), %rax #rax = aux
  57. movl (%rax), %eax #rax = *aux
  58. cmpl %eax, %edi #compara *aux com size
  59. je endwhile
  60. endverif:
  61. movq -8(%rbp), %rax #rax = aux
  62. movl (%rax), %ebx #rbx = *aux
  63. addl %ebx, %eax #rax = aux + *aux
  64. addq $5, %rax # rax = aux + *aux + 5
  65. movq %rax, -8(%rbp) #aux += *aux + 1
  66. jmp condwhile
  67. endwhile:
  68. movq $12,%rax
  69. pushq %rdi
  70. movq $0, %rdi
  71. syscall
  72. popq %rdi
  73. movq -8(%rbp), %rcx #rcx = aux
  74. cmpq %rcx, %rax
  75. jge else
  76. pushq %rdi
  77. movq $0, %rdi
  78. movq $12, %rax
  79. syscall
  80. popq %rdi
  81. addq %rdi, %rax #rax = end. heap + size
  82. addq $5, %rax #rax = end. heap + size + 5 //novo valor do topo da pilha
  83. pushq %rdi
  84. movq %rax, %rdi
  85. movq $12, %rax
  86. syscall
  87. popq %rdi
  88. movq %rdi, %rbx #rbx = size
  89. movq -8(%rbp), %rax #rax = aux
  90. movl %ebx, (%rax) #*aux = size
  91. movq -8(%rbp), %rax #rax = aux
  92. subq $1, %rax # rax = aux-1
  93. movb $1, (%rax) # M[aux-1] = OCUPADO
  94. jmp endif
  95. else:
  96. movq -8(%rbp), %rax #rax = aux
  97. subq $1, %rax #rax = aux-1
  98. movb $1, (%rax) #M[aux-1] = OCUPADO
  99. movq -8(%rbp), %rax #rax = aux
  100. movl (%rax), %eax #eax = *aux
  101. cmpl %eax, %edi
  102. jge endif
  103. movl %eax, -20(%rbp) #oldsize = *aux
  104. movq -8(%rbp), %rax #rax = aux
  105. movl %edi, (%rax) #*aux = size
  106. movq -8(%rbp), %rax #rax = aux
  107. movl (%rax), %ebx #ebx = *aux
  108. addl %ebx, %eax #rax = aux + *aux
  109. addq $5, %rax #rax = aux + *aux + 5
  110. movq %rax, -16(%rbp) #aux2 = aux + *aux + 5
  111. movq -16(%rbp), %rax #rax = aux2
  112. movl -20(%rbp), %ebx #ebx = oldsize
  113. subl %edi, %ebx #ebx = oldsize - size
  114. subl $5, %ebx #ebx = oldsize - size - 5
  115. movl %ebx, (%rax) #*aux2 = oldsize - size - 5
  116. movq -16(%rbp), %rax #rax =aux2
  117. subq $1, %rax # rax = aux2 - 1
  118. movb $0, (%rax)
  119. endif:
  120. movq -8(%rbp), %rax #rax = aux
  121. addq $4, %rax
  122. addq $20, %rsp
  123. popq %rbp
  124. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement