snapkitty
hardware
x86-64-asm
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/snapkitty-twin-mars-asm
42fb483 verified
Raw History Blame Contribute Delete
860 Bytes
; memory.asm — bump allocator via brk/mmap
%include "include/syscall.inc"
%include "include/memory.inc"
section .bss
align 16
heap_base: resq 1
heap_ptr: resq 1
heap_end: resq 1
section .text
global mem_init
global mem_alloc
; void mem_init()
mem_init:
push rbp
mov rbp, rsp
; brk(0) to get current break
xor rdi, rdi
mov rax, SYS_BRK
syscall
mov [rel heap_base], rax
mov [rel heap_ptr], rax
; brk(base + HEAP_SIZE)
add rax, HEAP_SIZE
mov rdi, rax
mov rax, SYS_BRK
syscall
mov [rel heap_end], rax
pop rbp
ret
; void* mem_alloc(size_t bytes) — bump allocator
mem_alloc:
; align to 8
add rdi, 7
and rdi, ~7
mov rax, [rel heap_ptr]
mov rcx, rax
add rcx, rdi
cmp rcx, [rel heap_end]
jg .oom
mov [rel heap_ptr], rcx
ret
.oom:
xor rax, rax
ret