2012-03-30 53 views
0

好吧,基本上我遇到的問題是這樣的。如何使用動態內存在MIPS中存儲字符串?

我已經被分配去編寫一個動態存儲結構的MIPS程序。

基本上,它存儲一個ID,一年,一個標題和一個描述 它將被存儲使用二叉搜索樹。

如果你曾經用C++編寫過一個棧,你就知道我在說什麼了。 我已經成功地將ID和標題存儲在內存中, 但我無法存儲用戶輸入的字符串。

這是一個複雜的問題,有沒有很多,我已經 已經能夠在網上找到這樣的道具的信息,如果你能幫助我這個:)

這裏是我的內存設置:

$ S5 - 商店根節點

$ S7 - 商店樹(非必要)的尺寸

每一個新項目都包含344個字節的塊

的字節在設置爲這樣:

8字節 - [ID]

8字節 - [年]

64字節 - [標題]

256字節 - [說明]

8個字節 - [LastNodeAddress]

8字節 - [NextNodeAddress]

下面的代碼,你可能會看到問題:

li $v0, 9   #allocate memory for new record 
li $a0, 344   #enough memory for 2 addresses and all the data 
syscall 


move $s0, $v0   #hang onto the initial address of all our info 

li $v0, 4   #prompt for ID 
la $a0, addid 
syscall 

li $v0, 5   #enter integer 
syscall 

sw $v0, 0($s0)   #store our ID into memory Offset: 0 

li $v0, 4   #prompt for add year 
la $a0, addyear 
syscall 

li $v0, 5   #enter integer 
syscall 

sw $v0, 4($s0)   #store year into our memory Offset: 4 

li $v0, 4   #prompt for add title 
la $a0, addtitle 
syscall 

li $v0, 8   #read title into titlebuffer 
la $a0, titlebuffer 
li $a1, 64 
syscall 

sw $a0, 8($s0)   #store title into our memory Offset: 8 

li $v0, 4   #prompt for add description 
la $a0, adddescription 
syscall 

li $v0, 8   #read from input into descriptionbuffer 
la $a0, descriptionbuffer 
li $a1, 256 
syscall 

sw $a0, 72($s0)   #store description into our memory Offset: 72 

bne $s7, 0, setlocations #if this isn't root node let's set the locations 

add $s7, $s7, 1   #add 1 to the size of the records 

move $s5, $s0   #store this address as root node for now 

的問題是,所有正在存儲的是緩衝區的地址。 的緩衝區在我的數據部分定義是這樣的:

.data 
titlebuffer: .space 64 
descriptionbuffer: .space 256 

什麼我最終只是保存在我分配 內存中的地址,我不知道如何來存儲字符串爲分配的內存。

任何幫助將不勝感激! :)

+0

我已經找到了解決方案!!!!!!!! 這是:d:d:d:d:d 我的老師表明它是如何做不能相信我錯過了XX ,而不是拉$ A0,descriptionbuffer 而不是拉$ A0,titlebuffer 使用: la $ a0,8($ s0) la $ a0,72($ s0) 要打印您做同樣的事情! :) 希望這可以幫助某人 – user1274820 2012-03-30 02:41:32

回答

2

不要在你的程序開始時定義你的記憶,就像我在原始問題中展示的一樣。

取而代之,分配它並將值讀入動態存儲器的正確偏移量。

而不是la $a0, descriptionbuffer

而不是la $a0, titlebuffer

用途:

la $a0, 8($s0)

la $a0, 72($s0)

這裏我使用move $s0, $v0移動內存地址爲$s0和讀取數值詮釋o正確的偏移量。

打印你做同樣的事情!

這裏是工作代碼:

li $v0, 9   #allocate memory for new record 
li $a0, 344   #enough memory for 2 addresses and all the data 
syscall 

move $s0, $v0   #hang onto the initial address of all our info 

li $v0, 8   #read our title into the allocated space 
la $a0, 8($s0)   #Offset: 8 
li $a1, 64 
syscall 

li $v0, 8   #read our description into the allocated space 
la $a0, 72($s0)   #Offset: 72 
li $a1, 256 
syscall 

此外,你可以在這裏找到最終的解決方案:https://stackoverflow.com/a/9953839/1274820

編輯:嗯,之後在10k的意見,我決定在這裏添加更多的信息,以便你不必通過以後的代碼搜索

下面是完整的代碼來存儲4個不同的數據存儲:

li $v0, 9   #allocate memory for new record 
li $a0, 344   #[334 = how much memory - in bytes] 
syscall 

move $s0, $v0  #store the address of our allocated memory in $s0 

li $v0, 5   #enter integer 
syscall 
sw $v0, 0($s0)  #store our ID into memory Offset: 0 

li $v0, 5   #enter integer 
syscall 
sw $v0, 4($s0)  #store year into our memory Offset: 4 

li $v0, 8   #read our title into the allocated space 
la $a0, 8($s0)  #Offset: 8 
li $a1, 64 
syscall 

li $v0, 8   #read our description into the allocated space 
la $a0, 72($s0)  #Offset: 72 
li $a1, 256 
syscall 

這將存儲ID,年份,標題和描述 - 偏移量是我們將數據放置在動態內存中的位置。

假設自己需要的334個字節的塊(如上面):

[ 334 ]

我們存儲ID的整數(4個字節的數據)中的偏移0這樣的:

[(ID) 330 ]

然後,我們存儲它旁邊的年份(偏移量4)。

[(ID)(YR) 326 ]

等等......

要打印出來,像這樣做:

li $v0, 1   #Print the ID stored at $s0  [Offset: 0] 
lw $a0, 0($s0) 
syscall 
li $v0, 1   #Print the Year stored at $s0 [Offset: 4] 
lw $a0, 4($s0) 
syscall 
li $v0, 4   #Print the Title stored at $s0 [Offset: 8] 
la $a0, 8($s0) 
syscall 
li $v0, 4   #Print descript stored at $s0 [Offset: 72] 
la $a0, 72($s0) 
syscall 
相關問題