0
我想編寫一個以字符串作爲參數(來自用戶輸入)並存儲在動態內存中的子例程。MIPS:如何在動態內存(堆)中存儲字符串
這是我想出了:
.data
name: .space 32 # allocates 32 bytes of memory to store a name
namePrompt: .asciiz "name: "
.text
.globl main
main:
la $a0, namePrompt
li $v0, 4 # system call to print a string.
syscall # print namePrompt.
la $a0, name # adress where to store the input
li $a1, 32 # max input size in bytes
li $v0, 8
syscall
la $a0, name # name as first parameter of save_string subroutine
jal save_string
save_string:
move $s0, $a0 # s0 = name.
# allocate 32 bytes in heap memory.
li $v0, 9
li $a0, 32
syscall
sw $s0, 0($v0) # store the name in allocated memory
jr $ra
但是我有一種感覺,這是不這樣做的正確方法。
此外,如何釋放空間?