Download asm/memory.asm from Snapkitty/snapkitty-twin-mars-asm: direct link, hf CLI and curl.
- Browser
- Download file 860 Bytes
-
https://huggingface.co/Snapkitty/snapkitty-twin-mars-asm/resolve/main/asm/memory.asm
- Command line
-
hf download hf://Snapkitty/snapkitty-twin-mars-asm/asm/memory.asm
-
curl -L -o memory.asm https://huggingface.co/Snapkitty/snapkitty-twin-mars-asm/resolve/main/asm/memory.asm
860 Bytes
| ; memory.asm — bump allocator via brk/mmap | |
| "include/syscall.inc" | |
| "include/memory.inc" | |
| .bss | |
| 16 | |
| heap_base: resq 1 | |
| heap_ptr: resq 1 | |
| heap_end: resq 1 | |
| .text | |
| mem_init | |
| 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 | |