;;# struct block { ;;# void *addr; // pointer to the start address of the block ;;# u32 size; // the size of the block ;;# void *next // pointer to the previous block ;;# }; # #;; .section .data # #;; ;;;# Constants #;; .equ SYS_BRK, 214 #;; .equ BLOCK_SZ, 12 #;; .equ BLOCK_ARR_CAP, 1024 # #;; .section .bss # #;; system_break_start: #;; .space 4 #;; system_break_cur: #;; .space 4 # #;; block_root: #;; .space BLOCK_SZ #;; block_arr: #;; .space BLOCK_SZ * BLOCK_ARR_CAP # #;; .section .text # #;; .globl malloc #;; .globl free # #;; malloc: #;; mv t6, a0 ;# save the arg # #;; la t1, system_break_start ;# load the break #;; la t2, system_break_cur ;# #;; lw a0, 0(t1) ;# has the break been initialized #;; bnez a0, block_loop ;# # #;; li a7, SYS_BRK ;# do brk syscall with an invalid arg #;; ecall ;# to get the current break # #;; sw a0 0(t1) ;# save the break #;; sw a0 0(t2) ;# # #;; block_loop: ;# in this loop find the first free address # #;; ret #;; free: #;; ret #