MIPS-如何将一个字符串添加到另一个 [英] MIPS- How to add one string to another

查看:1372
本文介绍了MIPS-如何将一个字符串添加到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写在一个MIPS过程,会发现两个输入字符串参数的长度。此后,该过程需要较短的字符串添加到更大的一个,计算它的长度和打印整个字符串。然后写一个主要的功能,让你完成流程,将测试过程:从键盘输入的字符串,调用程序和打印新阵列屏幕的长度。

我存储在独立的寄存器两个名字,但我需要之前,我打印的全名,向他们结合起来的。
code:


    。数据
第一:.asciiz第一个字符串:\\ n
最后:.asciiz第二个字符串:\\ n
全:.asciiz全串
。文本
主要:
#第一个字符串
李$ V0,4#4打印一行
LA $ A0,第一#打印第一个字符串文本
系统调用系统调用#加$ A1,$ A1,254#设置字符串长度
李$ V0,8#8将读取字符串
系统调用#调用字
的sw $ V0,第一
移动$ V0,$ T1#现在的字符串是$ T1#第二个字符串
李$ V0,4#4打印一行
LA $ A0,最后打印#第二个字符串文本
系统调用系统调用#李$ V0,8#8将读取字符串
系统调用#调用字
的sw $ V0,最后
移动$ V0,$#T2现在的字符串是$ T2#完全字符串
李$ V0,4#4打印一行
LA $ A0,充满#打印全文
系统调用


解决方案

您将需要提示,带换行,并计算字符串的长度为您的输入字符串。你也应该有输入字符串和组合输出字符串单独的字符串缓冲区。

下面是一些工作code:

 。数据
第一:.asciiz第一串
最后:.asciiz第二个字符串
全:.asciiz全串
换行:.asciiz\\ n字符串1:256。空间#缓冲区第一串
字符串2:。空间256#缓冲区第二个字符串
STRING3:512。空间组合#输出缓冲    。文本主要:
    #提示符并读取第一个字符串
    LA $ A0,第一#提示字符串
    LA $ A1,字符串1#缓冲区地址
    日航提示
    此举$ S0,$#V0保存字符串的长度    #提示符并读取第二个字符串
    LA $ A0,去年#提示字符串
    LA $ A1,字符串2#缓冲区地址
    日航提示
    此举$ S1,$#V0保存字符串的长度    #点相结合的字符串缓冲区
    #注:本被对面的strcat调用更新(这是我们_want_)
    LA $ A0,STRING3    #决定哪些字符串基于较短的长度上
    BLT $ S0,S1 $,string1_short    #1串更长 - 追加到输出
    LA $ A1,字符串1
    日航的strcat    #2串短 - 追加到输出
    LA $ A1,字符串2
    日航的strcat    Ĵprint_fullstring1_short:
    #2线较长 - 追加到输出
    LA $ A1,字符串2
    日航的strcat    #串1短 - 追加到输出
    LA $ A1,字符串1
    日航的strcat# 显示结果
print_full:
    #输出完整的字符串preFIX消息
    李$ v0,4
    LA $ A0,全
    系统调用    #输出字符串组合
    李$ v0,4
    LA $ A0,STRING3
    系统调用    #结束此行
    李$ v0,4
    LA $ A0,换行符
    系统调用    李$ v0,10
    系统调用#提示符 - 字符串提示用户

#返回值:
#V0 - 一个字符串的长度(新行剥离)

#参数:
#A0 - 提示字符串的地址
#A1 - 字符串缓冲区地址

#则会覆盖:
#V1 - 适用于换行符ASCII
提示:
    #输出提示
    李$#v0,4系统调用来打印字符串
    系统调用    #得到用户字符串
    李$#v0,8系统调用字符串读
    此举$ A0,$ A1#的地方存放字符串
    李$字符串a1,256#最大长度
    系统调用    作为换行符李$#v1,0x0A ASCII值
    此举$ A1,A0 $#记得字符串的开始#带换行,并得到字符串的长度
prompt_nltrim:
    磅$ v0,0($ A0)#获得字符串下一个字符
    阿迪$ A0,$#a0,1 pre-递增1,使其指向下一个字符
    BEQ V0,$ V1,prompt_nldone#是换行?如果是,飞
    BNEZ $ V0,prompt_nltrim#是EOS?不,环prompt_nldone:
    SUBI $ A0,$#a0,1补偿pre-增量
    SB $零,0($ A0)#零出新行
    子$ V0,$ A0,$ A1#得到字符串的长度
    JR $ RA#回报#strcat的 - 后置字符串

#返回值:
#A0 - 更新到目的地的结束

#参数:
#A0 - 指向目标缓冲区
#A1 - 指向源缓冲区

#则会覆盖:
#V0 - 当前字符
strcat的:
    磅$ v0,0($ A1)#获取当前字符
    BEQZ $ V0,strcat_done#为CHAR 0?如果是的话,做    SB $ v0,0($ A0)#商店目前焦炭    阿迪$ A0,$#a0,1提前目的指针
    阿迪$ A1,$#A1,1提前源指针
    Ĵstrcat的strcat_done:
    SB $零,0($ A0)#添加EOS
    JR $ RA#回报

I need to write a procedure in MIPS that will find the length of two input string arguments. After that the procedure needs to add the shorter string to the bigger one,calculate its length and print the whole string. Then write a main function that will test procedure so that you complete the processes: input strings from the keyboard, calling the procedure and print the length of the new array screen.

I store both names in separate registers, but I need to combine them to one before I print the full name out. code:

    .data
first:.asciiz "First string: \n"
last:.asciiz "Second string: \n"
full:.asciiz "Full string: "
.text 
main:
#    first string
li $v0, 4              # 4 prints a line
la $a0, first          # Print first string 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 string is now in $t1

#   second string
li $v0, 4              # 4 prints a line
la $a0, last           # Print second string text
syscall                # Syscall

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

#    Full string
li $v0, 4              # 4 prints a line
la $a0, full           # Print the whole text
syscall  

解决方案

You'll need to to prompt, strip newline, and calculate string length for both of your input strings. You should also have separate string buffers for input strings and the combined output string.

Here's some working code:

    .data
first:      .asciiz     "First string: "
last:       .asciiz     "Second string: "
full:       .asciiz     "Full string: "
newline:    .asciiz     "\n"

string1:    .space      256             # buffer for first string
string2:    .space      256             # buffer for second string
string3:    .space      512             # combined output buffer

    .text

main:
    # prompt and read first string
    la      $a0,first               # prompt string
    la      $a1,string1             # buffer address
    jal     prompt
    move    $s0,$v0                 # save string length

    # prompt and read second string
    la      $a0,last                # prompt string
    la      $a1,string2             # buffer address
    jal     prompt
    move    $s1,$v0                 # save string length

    # point to combined string buffer
    # NOTE: this gets updated across strcat calls (which is what we _want_)
    la      $a0,string3

    # decide which string is shorter based on lengths
    blt     $s0,$s1,string1_short

    # string 1 is longer -- append to output
    la      $a1,string1
    jal     strcat

    # string 2 is shorter -- append to output
    la      $a1,string2
    jal     strcat

    j       print_full

string1_short:
    # string 2 is longer -- append to output
    la      $a1,string2
    jal     strcat

    # string 1 is shorter -- append to output
    la      $a1,string1
    jal     strcat

# show results
print_full:
    # output the prefix message for the full string
    li      $v0,4
    la      $a0,full
    syscall

    # output the combined string
    li      $v0,4
    la      $a0,string3
    syscall

    # finish the line
    li      $v0,4
    la      $a0,newline
    syscall

    li      $v0,10
    syscall

# prompt -- prompt user for string
#
# RETURNS:
#   v0 -- length of string (with newline stripped)
#
# arguments:
#   a0 -- address of prompt string
#   a1 -- address of string buffer
#
# clobbers:
#   v1 -- holds ASCII for newline
prompt:
    # output the prompt
    li      $v0,4                   # syscall to print string
    syscall

    # get string from user
    li      $v0,8                   # syscall for string read
    move    $a0,$a1                 # place to store string
    li      $a1,256                 # maximum length of string
    syscall

    li      $v1,0x0A                # ASCII value for newline
    move    $a1,$a0                 # remember start of string

# strip newline and get string length
prompt_nltrim:
    lb      $v0,0($a0)              # get next char in string
    addi    $a0,$a0,1               # pre-increment by 1 to point to next char
    beq     $v0,$v1,prompt_nldone   # is it newline? if yes, fly
    bnez    $v0,prompt_nltrim       # is it EOS? no, loop

prompt_nldone:
    subi    $a0,$a0,1               # compensate for pre-increment
    sb      $zero,0($a0)            # zero out the newline
    sub     $v0,$a0,$a1             # get string length
    jr      $ra                     # return

# strcat -- append string
#
# RETURNS:
#   a0 -- updated to end of destination
#
# arguments:
#   a0 -- pointer to destination buffer
#   a1 -- pointer to source buffer
#
# clobbers:
#   v0 -- current char
strcat:
    lb      $v0,0($a1)              # get the current char
    beqz    $v0,strcat_done         # is char 0? if yes, done

    sb      $v0,0($a0)              # store the current char

    addi    $a0,$a0,1               # advance destination pointer
    addi    $a1,$a1,1               # advance source pointer
    j       strcat

strcat_done:
    sb      $zero,0($a0)            # add EOS
    jr      $ra                     # return

这篇关于MIPS-如何将一个字符串添加到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆