sorting - 如何在 MIPS 中使用系统调用 9 (sbrk) 进行动态内存分配

标签 sorting mips

我有一个作业是用 MIPS 编写梳状排序。用户将要输入数组,当然还有它的大小。在搜索堆分配时,我找到了系统调用9。但是,我找不到使用方法。我写了这个:

    li $v0, 4
    la $a0, message1    # prints the first message 
    syscall
    
    li $v0, 5           # reads the size for the array        
    syscall
    
    mul $t0, $v0, 4     # because array contains integer, I change them into bytes
    la $a0, $t0         # allocate the size of the array in the heap
    li $v0, 9           # now, $v0 has the address of allocated memory
    syscall
    
    move $v1, $v0       # Because systemcall uses $vo register, I move it to $v1 keep it safe.
    
create_array:    
    la $a0, message2    # prints the first message
    li $v0, 4 
    syscall

    li   $s0, 0         # $s1 is the index, and loop induction variable
    li   $s1, 5         # $s1 is the sentinel value for the loop
        
Loop1:  
    bge  $s0, $s1, End_Loop1

    li $v0, 5           # Read integer values
    syscall
    
    mul  $t3, $s0, 4    # $t3 is the offset
    add  $t4, $t3, $t0  # $t4 is the address of desired index
    sw   $v0, ($t4)     # store the value in the array
    addi $s0, $s0, 1    # increment the index        
    j    Loop1

End_Loop1:

我得到这个错误:

la": Too few or incorrectly formatted operands. Expected: la $t1,($t2)

我该如何使用它?这是创建数组的正确方法吗?

最佳答案

替换

la $a0, $t0     # allocate the size of the array in the heap

move $a0, $t0

la 指令的目的是将符号的 [A] 地址 [L] 加载到寄存器中。例如:

la $a0, message1    # prints the first message 

会将message1的地址载入寄存器$a0la 实际上是一个伪指令,在本例中转换为:

lui $a0, message1/0x10000       # load the upper halfword of the address
ori $a0, $a0, message1%0x10000  # OR in the lower halfword of the address

正如您所想象的那样,尝试加载另一个寄存器的地址是没有意义的,因为寄存器没有地址。

当我们讨论 MIPS 伪指令时:move 也是其中之一,上面的 move $a0, $t0 指令转化为某种东西像 add $a0, $0, $t0

关于sorting - 如何在 MIPS 中使用系统调用 9 (sbrk) 进行动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15690978/

相关文章:

javascript - 按 "Levenshtein Distance"对数组进行排序,在 Javascript 中性能最佳

r - 使用不同的调用方案按列对数据帧进行排序

assembly - 预取指令

assembly - 负载词和移动词之间的区别?

assembly - MIPS 程序集返回调用分支语句

c++ - teq如何在mips中工作

php - 如何根据键可用性将所有输入值分配到数组中而不会发生冲突?

java - 如何不按键而是按值类的字段对映射进行排序?

java - Scala 中的基本插入排序,Haskell 版本的端口

将 C 代码转换为 MIPS 汇编 - 使用递归的组合函数