本文介绍了使用sbrk的MIPS动态内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用sbrk进行动态内存分配.但是,作为SPIM和MIPS的新手,我无法做到这一点.我为它绘制了一个粗略的代码.

I was trying to use sbrk for dynamic memory allocation. But, being a new comer 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

推荐答案

.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

仅第二条语句需要省略,因为系统正在等待获取应分配的字节数,但以上我试图给出var的地址,但这是结果. sbrk服务将地址返回到包含n个其他字节的内存块.这将用于动态内存分配.

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.

这篇关于使用sbrk的MIPS动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 22:58