C++程序:C++到MIPS - 小问题if语句
int main()
{
char string[256];
int i=0;
char *result = NULL; // NULL pointer
// Obtain string from user
scanf("%255s", string);
// Search string for letter t.
// Result is pointer to first t (if it exists)
// or NULL pointer if it does not exist
while(string[i] != '\0')
{
if(string[i] == 't')
{
result = &string[i];
break; // exit from while loop early
}
i++;
}
if(result != NULL)
printf("Matching character is %c\n", *result);
else
printf("No match found\n");
}
MIPS的代码,我有:
.globl main
.text
# main
main:
li $v0,4 # Load value 4
la $a0, msg0 # Load array
syscall
li $v0,8 # Load value 8
la $a0,string # Load array
syscall # Syscall
li $v0,4 # Load immediate value 4
la $a0, string # Load array
syscall # Syscall
la $t0, string # array
la $t1, result # array
lb $t2, result # array
while:
lb $t3, 0($t0)
beq $t3, $0, if2 # if !=0
beq $t3, 't', if # If = "t"
addi $t0, $t0,1 # i++
j while # Jump to While
if:
sw $t3, result # Save result to memory
li $v0,4 # Load value 4
la $a0, found # Load array
syscall # Syscall
j exit
j if2 # Jump to if2
if2:
li $v0,4 # Load value 4
la $a0, notfound # Load array
syscall # Syscall
j exit
exit:
li $v0, 10
syscall # Exit
.data
msg0: .asciiz "Enter Word: "
string: .byte 0:256
i: .word 0
result: .word 0
found: .asciiz "Found!"
notfound: .asciiz "Not Found"
MIPS的代码,我写了似乎是工作,但我认为它不遵循上面的C++代码结构。我也认为我已经搞乱了if语句,但无法弄清楚什么以及如何解决它。任何建议我怎么能改进它?
谢谢
你能提供什么你认为是错的MIPS代码,或者你是如何“搞砸了if语句的东西”的一些细节? – 2013-04-04 20:57:12
我的朋友评论我的代码,它没有遵循C++结构。我不确定这是如何这就是为什么我决定在这里发布它,以便其他人可能能够检测到它。他是班级的分级师,所以他不想给我更多的细节。我认为它与if2或if(result!= NULL)有关系。 – Damon 2013-04-05 00:51:41