Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .section .data
- .globl baseheap
- baseheap: .quad 0
- .section .text
- .global meuMalloc
- .type meuMalloc, @function
- meuMalloc:
- #############################################################################################################################
- # 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 #
- # heap que deve ser o valor setado para baseheap. #
- #############################################################################################################################
- pushq %rbp
- movq %rsp, %rbp
- subq $20, %rsp #-8(rbp) = aux, -16(rbp)= aux2, -20(rbp) = oldsize
- movq $12, %rax #syscall 12 obtem brk
- pushq %rdi #empilha size
- movq $0, %rdi
- syscall
- popq %rdi #recupera size
- cmpq $0, baseheap
- jne fimif
- movq %rax, baseheap
- fimif:
- cmpq $0, %rdi
- jg precondwhile
- movq $0, %rax
- addq $20, %rsp
- popq %rbp
- ret
- precondwhile:
- movq baseheap, %rax
- movq %rax, -8(%rbp) #aux = base da heap
- addq $1, -8(%rbp) #aux = base da heap + 1
- condwhile:
- movq $12, %rax #as proximas linhas obtem o ultimo endereco da heap
- pushq %rdi #empilha size
- movq $0, %rdi
- syscall
- popq %rdi #recupera size
- cmpq %rax, -8(%rbp) #se aux >= brk sai do loop
- jge endwhile
- movq -8(%rbp), %rbx #rbx = aux
- subq $1,%rbx #rbx = aux-1
- movb (%rbx), %cl #rcx = M[aux-1]
- cmp $1, %cl #rcx == OCUPADO?
- je endverif
- movq -8(%rbp), %rax #rax = aux
- movl (%rax), %eax #rax = *aux
- subq $5, %rax
- cmpl %eax, %edi #compara *(aux)-5 com size
- jle endwhile
- movq -8(%rbp), %rax #rax = aux
- movl (%rax), %eax #rax = *aux
- cmpl %eax, %edi #compara *aux com size
- je endwhile
- endverif:
- movq -8(%rbp), %rax #rax = aux
- movl (%rax), %ebx #rbx = *aux
- addl %ebx, %eax #rax = aux + *aux
- addq $5, %rax # rax = aux + *aux + 5
- movq %rax, -8(%rbp) #aux += *aux + 1
- jmp condwhile
- endwhile:
- movq $12,%rax
- pushq %rdi
- movq $0, %rdi
- syscall
- popq %rdi
- movq -8(%rbp), %rcx #rcx = aux
- cmpq %rcx, %rax
- jge else
- pushq %rdi
- movq $0, %rdi
- movq $12, %rax
- syscall
- popq %rdi
- addq %rdi, %rax #rax = end. heap + size
- addq $5, %rax #rax = end. heap + size + 5 //novo valor do topo da pilha
- pushq %rdi
- movq %rax, %rdi
- movq $12, %rax
- syscall
- popq %rdi
- movq %rdi, %rbx #rbx = size
- movq -8(%rbp), %rax #rax = aux
- movl %ebx, (%rax) #*aux = size
- movq -8(%rbp), %rax #rax = aux
- subq $1, %rax # rax = aux-1
- movb $1, (%rax) # M[aux-1] = OCUPADO
- jmp endif
- else:
- movq -8(%rbp), %rax #rax = aux
- subq $1, %rax #rax = aux-1
- movb $1, (%rax) #M[aux-1] = OCUPADO
- movq -8(%rbp), %rax #rax = aux
- movl (%rax), %eax #eax = *aux
- cmpl %eax, %edi
- jge endif
- movl %eax, -20(%rbp) #oldsize = *aux
- movq -8(%rbp), %rax #rax = aux
- movl %edi, (%rax) #*aux = size
- movq -8(%rbp), %rax #rax = aux
- movl (%rax), %ebx #ebx = *aux
- addl %ebx, %eax #rax = aux + *aux
- addq $5, %rax #rax = aux + *aux + 5
- movq %rax, -16(%rbp) #aux2 = aux + *aux + 5
- movq -16(%rbp), %rax #rax = aux2
- movl -20(%rbp), %ebx #ebx = oldsize
- subl %edi, %ebx #ebx = oldsize - size
- subl $5, %ebx #ebx = oldsize - size - 5
- movl %ebx, (%rax) #*aux2 = oldsize - size - 5
- movq -16(%rbp), %rax #rax =aux2
- subq $1, %rax # rax = aux2 - 1
- movb $0, (%rax)
- endif:
- movq -8(%rbp), %rax #rax = aux
- addq $4, %rax
- addq $20, %rsp
- popq %rbp
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement