简体   繁体   English

将两个字符串组合到MIPS汇编中的一个地址

[英]Combine Two Strings to one address in MIPS Assembly

I have a MIPS program I am writing, in which the user types in their first name, then last name, and then the system prints out their full name. 我正在写一个MIPS程序,用户输入他们的名字,然后输入姓氏,然后系统打印出他们的全名。

I store both names in seperate registers, but I need to combine them to one before I print the full name out. 我将这两个名称存储在单独的寄存器中,但在打印全名之前我需要将它们组合成一个。

Any help would be appreciated. 任何帮助,将不胜感激。 Code is below: 代码如下:

.data
first:.asciiz "Please enter your first name: \n"
last:.asciiz "Please enter your last name: \n"
full:.asciiz "Your full name is: "
.text 
main:
#    First Name
li $v0, 4              # 4 prints a line
la $a0, first          # Print first name text
syscall                # Syscall

add $a1, $a1, 254      # Setting String length 
li $v0, 8              # 8 will read string
syscall                # calls the word
sw $v0, first
move $v0, $t1          # The name is now in $t1

#   Last Name
li $v0, 4              # 4 prints a line
la $a0, last           # Print last name text
syscall                # Syscall

li $v0, 8              # 8 will read string
syscall                # calls the word
sw $v0, last
move $v0, $t2          # The name is now in $t2

#    Full Name
li $v0, 4              # 4 prints a line
la $a0, full           # Print full name text
syscall  

    # Combine first and last name below

The first thing that needs to be said here is that you're using the read-string system call wrong. 这里需要说的第一件事是你正在使用read-string系统调用错误。 You seem to think that the system call is returning an address in $v0, and this is not the case. 您似乎认为系统调用在$ v0中返回一个地址,而事实并非如此。 You can find some info on MIPS system calls here . 您可以在此处找到有关MIPS系统调用的一些信息。

The second is that you appear to be attempting to re-use the memory you reserved for your prompts for the user's first and last names. 第二种情况是,您似乎正在尝试重新使用您为提示输入的内存,以获取用户的名字和姓氏。 This is a bad idea. 这是一个坏主意。

The first thing is to fix your data segment: 首先要修复您的数据段:

.data

fprompt:.asciiz "Please enter your first name: "
lprompt:.asciiz "Please enter your last name: "
oprompt:.asciiz "Your full name is: "
first: .space 255 #255 bytes for first name
last:  .space 255 #255 bytes for last name
full:  .space 512 #512 bytes for full name

Next, the main function has to be fixed as the wrong convention for the read string call is being used: 接下来,必须修复main函数,因为正在使用读取字符串调用的错误约定:

main:

    #Prompt for first name
    li $v0, 4
    la $a0, fprompt
    syscall

    #Enter first name
    la $a0, first
    li $a1, 255
    li $v0, 8
    syscall

    #Prompt for last name
    li $v0, 4
    la $a0, lprompt
    syscall

    #Enter last name
    la $a0, last
    li $a1, 255
    li $v0, 8
    syscall

    #Display output lead-up
    li $v0, 4
    la $a0, oprompt
    syscall

    #call the strcpy function
    move $s0 $ra
    la $a0 first
    la $a1 last
    la $a2 full
    jal strcpy
    move $ra $s0

    #display the full string
    la $a0 full
    li $v0 4
    syscall

    #display a new-line
    li $a0 10
    li $v0 11
    syscall

    #exit
    jr $ra

Finally, for the meet of the question is how to concatenate two strings. 最后,对于问题的满足是如何连接两个字符串。 For this I used a separate function called strcpy (which should be noted does NOT perform the same as C's strcpy). 为此,我使用了一个名为strcpy的独立函数(应注意与C的strcpy不同)。 The strcpy takes the 2 input strings in $a0 and $a1 and copies them to $a2. strcpy接受$ a0和$ a1中的2个输入字符串,并将它们复制到$ a2。 It also throws a space in between for good measure. 它还会在两者之间留出一个空间,以达到很好的效果。

strcpy:

    li $t8 10 #store newline in $t8

    #loop through first string and copy to output string
   sCopyFirst:

        lb   $t0 0($a0)
        beq  $t0 $zero sCopySpace #exit loop on null byte
        beq  $t0 $t8 sCopySpace   #exit loop on new-line
        sb   $t0 0($a2)
        addi $a0 $a0 1
        addi $a2 $a2 1
        b sCopyFirst

    sCopySpace:

        li   $t0 ' '
        sb   $t0 0($a2)
        addi $a2 $a2 1 

    #loop through second string and copy to output string 
    sCopySecond:

        lb   $t0 0($a1)
        beq  $t0 $zero sDone #exit on null byte
        beq  $t0 $t8 sDone   #exit on new-line
        sb   $t0 0($a2)
        addi $a1 $a1 1
        addi $a2 $a2 1
        b sCopySecond

    sDone:

        sb $zero 0($a2) #null terminate string
        jr $ra

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM