问题
I am new to assembly language, and I have a project that I am struggling with. I don't want code; I just want to make sure I am thinking about the problem correctly so I don't drive myself crazy implementing a bad or down-right incorrect approach.
The problem can be summarized as such:
We have an array of strings in memory as such:
.data
animals: .asciiz "bear", "tiger", "gorilla", "horse", "dog"
We want to take a user's input string and store it into str:
.data
animals: .asciiz "bear", "tiger", "gorilla", "horse", "dog"
str: .space 64 #user input stored here
Then we want to see if the user has entered something that exists in the existing arry of strings in memory. So if the user were to enter, "horse" we would return "Found!" and if they were to enter "garage" we would return "Not found."
To actually do this, I am thinking that I would need to compare every character (each byte) of the input string with the corresponding character (byte) of each string in the array.
I have two main questions: is this a good approach/correct thinking about the problem? and how could I go about making the program recognize when has hit the end of one string in the array and started a new one?
Thank you so much for any responses. I'm sorry if any of this is unclear or a bad question. I am having a hard time wrapping my head around assembly.
回答1:
You need to know where each of the animals
strings start in memory. Try this:
.data
animals:
a0: .asciiz "bear"
a1: .asciiz "tiger"
a2: .asciiz "gorilla"
a3: .asciiz "horse"
a4: .asciiz "dog"
# addrs is a list of the starting addresses for each of the strings
addrs:
.word a0
.word a1
.word a2
.word a3
.word a4
.word 0
Now you can write code to loop through each of the addresses starting at addrs
and comparing the user's string with the reference strings.
来源:https://stackoverflow.com/questions/26042301/mips-comparing-user-input-string-to-an-array-of-strings-in-memory