我是新來的MIPS彙編編程最長的字的地址。我有這樣的項目:MIPS彙編找到字符串
首先,我們必須給定的字符串的地址加載到寄存器中。該字符串包含一個句子(一個字符數組)和單詞與空間分離。然後,我們必須找出每個單詞的長度,並將它們的長度與其他單詞的長度進行比較。最後,我們需要找到長度最長的單詞並打印出第一個字母的地址。
例如,如果字符串是:
string: .asciiz "This is a wonderful day"
,我們必須返回地址,存儲其W上。
到目前爲止,我已經寫了這一點:
.globl main
.text
main:
la $a0, string #Load the address of the string to $a0
addu $v1, $0, $a0 #Initialize the address of the longest word to v1
exit: #Exiting the program
addiu $v0, $zero, 10
syscall
.data
string: .asciiz "This is a wonderful day"
任何幫助表示讚賞。謝謝。
在Java中實現可能是這樣的:
int i = 0;
int Counter1 = 0;
int Counter2 = 0;
int address1 = getTheAddressOfCharacter(string[i]); //get the address of the first letter of the string.
while(string[i] != ' ') //find the length of the 1st word and store it in counter1.
{
Counter1 += 1;
i += 1;
}
int address2 = getTheAddressOfCharacter(string[i]); //get the address of the second letter of the string.
while(string[i] != '\0')
{
if(string[i] != ' ')
{
if(Counter2 == 0)
address2 = getTheAddressOfCharacter(string[i]);
Counter2 += 1;
}
else
{
if(Counter2 > Counter1)
{
address1 = address2;
Counter1 = Counter2;
}
Counter2 = 0;
}
i += 1;
}
和你有什麼問題?先設計一些算法,將其寫入代碼中。在評論中使用算法之後,請繼續完善它們,直到所有註釋都只描述一些簡單的操作爲止,並且直到您確認此算法正常工作(也適用於不同的輸入,例如空字符串,單字,僅輸入空格等)。 )。之後,你可以編寫指令,這將實現這一點。 – Ped7g
「在Java中」不能正常工作(我不是指像「getAddress」這樣的非java事物,我的意思是邏輯)。您無法在適當的時間更新地址2。它也會因空字符串輸入而崩潰。它會返回單個空間字符串的空間地址,也許顯示「找不到字」會更加正確。在討論'address2'更新機制時,記住一個惡作劇的測試人員也會在單詞之間或字符串的空格結尾添加兩三倍的空間(但通常不是一個錯誤的嘗試,所用的操作也足夠類似彙編指令)。 – Ped7g
可能會離開Java,並且認爲「string [i]」等同於「地址處的值」(因爲這在彙編中很容易做到),所以您不需要同時使用地址和索引,您可以使用他們在這項任務中的一切。 – Ped7g