2015-11-30 64 views
0

我有这个代码的问题。我试图通过输入到输入字符串,并将其保存到一个数组,这是我的代码:如何将字符串保存到mips数组中

.data 
    .align 2 
array: .space 80 
size: .word 20 
string: .space 20 
op: .asciiz "Enter the array length" 
prompt: .asciiz "Enter a string:" 
text: .asciiz "The array of string is:" 
newline: .asciiz "\n" 
    .text 
    .globl main 

main: 
    add $t0, $zero, $zero # index of array 
    addi $t1, $zero, 1 # counter=1 

    li $v0, 4 
    la $a0, op 
    syscall 
    jal new_line 

    li $v0, 5 
    syscall 

    addi $s0, $v0, 0 # $v0 contains integer read 
read_string: 
    bgt $t1, $s0, L1 # if ($t1 > length)then go to L1 

    li $v0, 4 
    la $a0, prompt 
    syscall 

    la $a0, string 
    li $a1, 20 
    li $v0, 8 
    syscall 

    sw $a0, array($t0) 
    addi $t0, $t0, 4 
    addi $t1, $t1, 1 

    j read_string 

L1: #### here i want to print the array #### 
    add $t0, $zero, $zero # index of array 
    addi $t1, $zero, 1 # counter=1 

    la $a0, text 
    li $v0, 4 
    syscall 
    jal new_line 

    while: bgt $t1, $s0, done 
    lw $t2, array($t0) 

    li $v0, 4 
    move $a0, $t2 
    syscall 
    jal new_line 

    addi $t0, $t0, 4 
    addi $t1, $t1, 1 
    j while 

    new_line: la $a0, newline 
    li $v0, 4 
    syscall 
    jr $ra 

done: li $v0, 10 
    syscall 

麻烦的是,这个节目告诉我,我已经输入最后输入的字符串,例如

Enter the array length: 
2 
Enter a string:asd 
Enter a string:123 
The array of string is: 
123 

123 

我需要一些帮助,非常感谢,祝您有愉快的一天。

回答

2

array索引的逻辑似乎是好的,但问题是,你总是存储条目以相同地址的string地址。该解决方案是一个较大的string区域和递增一个指针,当存储的字符串。

我已更正您的代码[未经测试]。请原谅无偿的风格清理,但我需要理解你的逻辑,然后再尝试修复它。我已经添加了更多评论[我是一个旧时间的asm人,我总是评论行]并注释您的代码[OLD]和我的替换[NEW]

.data 
    .align 2 
array:  .space 80 
size:  .word 20 
###string: .space 20   # [OLD] 
string:  .space 20000  # [NEW] 
op:   .asciiz "Enter the array length:" 
prompt:  .asciiz "Enter a string:" 
text:  .asciiz "The array of string is:" 
newline: .asciiz "\n" 

    .text 
    .globl main 
main: 
    # prompt user for array length 
    li  $v0,4 
    la  $a0,op 
    syscall 
    jal  new_line   # output newline 

    # read in array count 
    li  $v0,5 
    syscall 
    addi $s0,$v0,0   # $v0 contains the integer we read 

    add  $t0,$zero,$zero  # index of array 
    addi $t1,$zero,1   # counter=1 
    la  $s2,string   # load address of string storage area [NEW] 

read_string: 
    bgt  $t1,$s0,L1   # if ($t1 > length) then array is done -- fly 

    # prompt the user for next "string" 
    li  $v0,4 
    la  $a0,prompt 
    syscall 

    # get the string 
### la  $a0,string   # place to store string [OLD] 
    move $a0,$s2    # place to store string [NEW] 
    li  $a1,20 
    li  $v0,8 
    syscall 

    # store pointer to string into array 
    sw  $a0,array($t0) 

    addi $t0,$t0,4   # advance offset into pointer array 
    addi $t1,$t1,1   # advance iteration count 
    addi $s2,$s2,20   # advance to next string area [NEW] 

    j  read_string 

#### here i want to print the array #### 
L1: 
    add  $t0,$zero,$zero  # index of array 
    addi $t1,$zero,1   # counter = 1 

    # output the title 
    la  $a0,text 
    li  $v0,4 
    syscall 
    jal  new_line 

while: 
    bgt  $t1,$s0,done  # more strings to output? if no, fly 
    lw  $t2,array($t0)  # get pointer to string 

    # output the string 
    li  $v0,4 
    move $a0,$t2 
    syscall 
    jal  new_line 

    addi $t0,$t0,4   # advance array index 
    addi $t1,$t1,1   # advance count 
    j  while 

# new_line -- output a newline char 
new_line: 
    la  $a0,newline 
    li  $v0,4 
    syscall 
    jr  $ra 

# program is done 
done: 
    li  $v0,10 
    syscall 

UPDATE对于额外的信用,当你得到的原始版本的工作,你可以尝试用jal stradv,其中stradv是更换addi $s2,$s2,20

# stradv -- advance past end of string 
stradv: 
    ldb  $t2,0($s2)   # get char 
    addi $s2,$s2,1   # we pre-increment because we want EOS + 1 
    bne  $t2,$zero,stradv # is it EOS? if no, loop some more 
    jr  $ra 

这将使大型可变长度字符串[如果您还增加了读取系统调用字符串的长度]。

这是我通常会编码这样的东西,但我不想在基本代码完全正常工作之前添加它。

+0

非常感谢我的朋友......:D –

相关问题