0

i just found a solution for concatenate strings in MIPS assembly (on MARS), which I report the code here:

# String concatenate
.text

# Copy first string to result buffer
la $a0, str1
la $a1, result
jal strcopier
nop

# Concatenate second string on result buffer
la $a0, str2
or $a1, $v0, $zero
jal strcopier
nop
j finish
nop

# String copier function
strcopier:
or $t0, $a0, $zero # Source
or $t1, $a1, $zero # Destination

loop:
lb $t2, 0($t0)
beq $t2, $zero, end
addiu $t0, $t0, 1
sb $t2, 0($t1)
addiu $t1, $t1, 1
b loop
nop

end:
or $v0, $t1, $zero # Return last position on result buffer
jr $ra
nop

finish:
j finish
nop

.data
str1:
.asciiz "Hello "
str2:
.asciiz "world"
result:
.space 200

But my question is: what about concatenate a float number between the two strings ?

Thank you in advance.

Michael
  • 57,169
  • 9
  • 80
  • 125
Jay
  • 145
  • 2
  • 12
  • 1
    Please don't type your question in all caps. It makes it more difficult to read and is very shouty. – Dragonthoughts Jul 04 '18 at 21:20
  • 1
    MIPS doesn't have float->string built in; you have to do that yourself and then concat the strings. (MARS has a system call that can *print* a float as text, but I don't think you can use that as a library function to get the string in memory.) – Peter Cordes Jul 05 '18 at 01:28
  • @PeterCordes i know there's a system call for print a float but i need to concatenate the float to the string and then put the new string in $a0 as parameter of system call 50 (Confirm Dialog) – Jay Jul 05 '18 at 15:17
  • See [Turn float into string](https://stackoverflow.com/q/17632150) for writing it yourself. It's non-trivial, but not too bad if you don't need it to work for floats larger than 2^32. – Peter Cordes Jul 05 '18 at 18:45

0 Answers0