MIPS dynamic memory allocation using sbrk

Sahil Babbar picture Sahil Babbar · Mar 23, 2014 · Viewed 9.7k times · Source

I was trying to use sbrk for dynamic memory allocation. But, being a newcomer to SPIM and MIPS, I was unable to do so. I sketched out a rough code for the same.

.data
    var: .word 25
.text   
    main:
        li $v0, 9
        la $v0, var
        lw $a0, var
        syscall     # DYNAMICALLY ALLOCATING MEMORY OF SIZE 4 BYTES AT ADDRESS OF VAR
        sw $v0, var
        
        li $v0, 10
        syscall

Answer

Sahil Babbar picture Sahil Babbar · Mar 24, 2014
.data
    var: .word 25
.text   
    main:
        li $v0, 9
        lw $a0, var
        syscall     # DYNAMICALLY ALLOCATING MEMORY OF SIZE 4 BYTES AT ADDRESS OF VAR
        sw $v0, var
        
        li $v0, 10
        syscall

Only the second statement needs to be omitted as the system is waiting to get a amount of byte that should be allocated but above I was trying to give the address of var but this is result. The sbrk service returns the address to a block of memory containing n additional bytes. This would be used for dynamic memory allocation.