在MIPS中采用输入的子字符串失败

时间:2019-03-07 05:05:44

标签: assembly mips

我真的是汇编代码的新手,不确定我的代码出了什么问题...我只允许使用slt,beq,bne进行比较,而只使用lw,sw作为输入/输出。

该程序应采用两个整数作为索引,并保存在$ 1,$ 2;中。其中值应为0 <= $ 1 <= $ 2 <=字符串长度。字符串索引从0开始,如果两个输入为2,4,则子字符串从索引2到3。在子字符串的末尾,附加了换行符。如果索引有效,则为$ 3 = 0;如果索引无效,则为1。

我的代码仅在$ 2 <$ 1(无效情况)时有效。在其他所有情况下,它都将失败,并显示错误“ MIPS仿真器内部错误。mips.MachineException $ OutOfBounds”。

下面是我的代码,请提供帮助...我不知道出了什么问题...

; $1 - starting index position for substring
; $2 - ending index position for substring 
; $3 - 1 when index positions invalid; 0 when index positions valid
; $4 - address of standard input
; $5 - address of standard output 
; $6 - used for comparison
; $7 - current address of character read
; $8 - New line character
; $9 - index counter
; $10 - check if substring is there
; $11 - temp

lis $4  
.word 0xffff0004
lis $5
.word 0xffff000c
lis $8
.word 0xa       ;$8=Newline character ASCII code
addi $9, $0, -1 ;initiate $9 value: $9=-1
addi $10, $0, 0 ;initiate $10=0
addi $6, $0, 1  ;$6 is 1
slt $3, $2, $1  ;if $2<$1, $3=1
beq $6, $3, end ;if $3=1, invalid index, go to end by printing newline
slt $3, $2, $0  ;if $2<0, $3=1 (0>$2>$1)
beq $6, $3, end
slt $3, $1, $0  ;if $1<0, $3=1; otherwise 0<$1<$2
beq $6, $3, end

loop:   lw $7, 0($4)    ;retrieve first element of input string
        addi $9, $9, 1  ;$9 increments index count by 1 (so start with index=0)
        slt $3, $9, $1  ;$3=1 if yet to achieve starting index
        bne $6, $3, print   ;if at starting index, go to print
        addi $4, $4, 4  ;if yet to achieve starting index, then move to next element
        beq $0, $0, loop    ;keep looping

print:  addi $10, $0, 2 ;$10=2 if need to add newline
        slt $3, $9, $2  ;$3=1 if before hitting last index
        beq $0, $3, end ;if $3=0, it means hit the last index, so go to end
        sw $7, 0($5)    ;print the desired character
        addi $4, $4, 4
        addi $5, $5, 4  ;move to next space for output
        beq $0, $0, loop

end:    addi $11, $0, 2 ;$11 is 2
        bne $11, $10, end2  ;$10=$11=2, then substring exists, add new line char
        addi $5, $5, 4
        sw $8, 0($5)  

end2:   jr $31

0 个答案:

没有答案