Consider two strings: string1 and string2. a) Write a high-level code for a proc
ID: 3627506 • Letter: C
Question
Consider two strings: string1 and string2.a) Write a high-level code for a procedure called concat that concatenates (joins together) the two strings: void concat(char[] string1, char[] string2, char[] stringconcat). The procedure does not return a value. It concatenates string1 and string2 and places the resulting string in stringconcat. You may assume that the character array stringconcat is large enough to accommodate the concatenated string.
b) Convert the procedure from part (a) into MIPS assembly language.
Explanation / Answer
please rate -thanks
void concat(char a[],char b[])
{int i,j,k=1;
i=(int)a[0]+1;
a[0]=(int)a[0]+(int)b[0];
for(j=i;j<=(int)a[0];j++)
a[j]=b[k++];
}
mips
## $s0 --- char of string
## $s1 --- p, index for input strings
## $s2 --- q, index for combined string
.text
.globl main
main: # while ( true )
li $v0,4 # print "Enter First String:"
la $a0,prompt1 #
syscall
li $v0,8 # read string into "string1"
la $a0,string1 # $a0 == buffer address
li $a1,132 # $a1 == buffer length
syscall
lb $s0,string1 # if ( string==empty )
nop
li $t1,10
beq $s0,$t1,done # done
nop
li $v0,4 # print "Enter Secnd String:"
la $a0,prompt2 #
syscall
li $v0,8 # read string into "string2"
la $a0,string2 # $a0 == buffer address
li $a1,132 # $a1 == buffer length
syscall
# copy string1 to string3
li $s1,0 # p = 0
li $s2,0 # q = 0
scan1: lb $s0,string1($s1) # while ( string1[p] != ' ' )
nop
beq $s0,$t1,end1 #
nop
sb $s0,string3($s2) # string3[q] = string1[p]
addiu $s1,$s1,1 # p = p+1
addiu $s2,$s2,1 # q = q+1
b scan1 # endwhile
nop
end1: # copy string2 to string3
li $s1,0 # p = 0
scan2: lb $s0,string2($s1) # while ( string2[p] != ' ' )
nop
beq $s0,$t1,end2 #
nop
sb $s0,string3($s2) # string3[q] = string1[p]
addiu $s1,$s1,1 # p = p+1
addiu $s2,$s2,1 # q = q+1
b scan2 # endwhile
nop
end2:
sb $t1,string3($s2)
li $s0,0
addiu $s2,$s2,1
sb $s0,string3($s2)
li $v0,4 # print "Concatenation :"
la $a0,prompt3 #
syscall
li $v0,4 # print new string
la $a0,string3 #
syscall
done:
li $v0,4 # print "done"
la $a0,don #
syscall
li $v0,10 # exit
syscall
.data
string1: .space 132
string2: .space 132
string3: .space 132
prompt1: .asciiz "Enter First String: "
prompt2: .asciiz "Enter Secnd String: "
prompt3: .asciiz "Concatenation : "
don: .asciiz "done "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.