Мне дали две функции в C, которые мне нужно преобразовать в инструкции MIPS32.
Я новичок в программировании.До сих пор я пытался понять, что на самом деле происходит в C, а затем использовал инструкции MIPS для перевода кода C на язык ассемблера.В основном я использовал инструкцию перехода, чтобы переключаться между различными частями кода.Я уверен, что, возможно, есть более эффективные способы использования регистров для краткости кода, но на данный момент я просто пытаюсь понять мои концепции правильно и понять манипуляции со строками.После этого я буду работать над оптимизацией кода.
C Код:
char firstmatch(char *s1, char *s2) {
char *temp;
temp = s1;
do {
if (strchr(s2, *temp) != 0)
return temp;
temp++;
} while (*temp != 0);
return 0;
}
char *strchr(register const char *s, int c) {
do {
if (*s == c) {
return (char*)s;
}
} while (*s++);
return (0);
}
Код MIPS:
.data
str1: .asciiz "hello \n" #String 1
str2: .asciiz "meh \n" #String 2
char_found: .asciiz " is the first character in string s1 that is also in s2 \n"
char_not_found: .asciiz "No character match between the two strings \n"
.text
main:
la $a0, str1 #Loading address of string 1 into register $a0
la $a1, str2 #Loading address of string 2 into register $a1
firstmatch:
move $t0, $a0 #Passing address of str1 from $a0 to $t0
move $t1, $a1 #Passing address of str1 from $a1 to $t1
addi $t2,$t0,0 #Using $t2 register for the temp variable
Load_Bytes:
lb $t4, 0($t1) #Loading the first character of str2
lb $t3, 0($t2) #Loading the first character of str1
strchr:
beq $t4,$t3,Label_1 #Checking for character match.
j Label_2 #No match, go to Label_2
Label_1: #Label to Print character as well as char_found string
li $v0, 4
la $s0, ($t4)
syscall
la $t5, char_found
syscall
Label_2:
addi $t2, $t2, 1 #Incrementing temp by 1
lb $t6, 0($t2) #Using t6 to check for NULL character
beqz $t6, Label_4 #Checking if value at $t2 is 0.
j Load_Bytes
Label_3: #Label to print when no character match
li $v0, 4
la $t5, char_not_found
syscall
Label_4:
addi $t1, $t1, 1 #Increment str2 byte by 1 after string 1 iteration reaches the NULLL character
lb $t7, 0($t1) #Using t7 to check for NULL character
beqz $t7, Label_3 #Checking if value at str 2($t1) is 0
addi $t2, $t0, 0 #Re-initialize string 1
j Load_Bytes
В настоящее время мой код работает бесконечно и печатает строку1. Я подозреваю, что либо я неправильно понимаю концепцию загрузки адресов и байтов, либо существует условие перехода, которое продолжает повторять программу.Любая помощь будет оценена.