Mips代码中出现错误的代码

时间:2017-02-02 11:15:18

标签: mips spim

我找到了一种方法来解决我的练习,但我的代码有一个错误......有些短语效果很好,其他没有......我用2个例子解释得更好:

  • aaaaabc --->这封信''出现次数最多的是当前时间。
  • 你好世界! --->这封信' l'出现次数最多,出现次数为3次。

这是我的代码,也许我在控制' a'信

if (e.gesture.pointerType === "touch") {

对于任何其他解释,可以在这篇文章下读我。非常感谢你。

编辑: 也许这个问题确实存在于' a'如果这是问题,请写信,因为如果我尝试使用' a'我没有结果(说明中的第一点),所有其他字母都是(描述中的第二点)

1 个答案:

答案 0 :(得分:0)

由于您只发布了少量代码(即频率表的扫描次数最多),因此难以确定您的真实/完整意图。

由于您的大部分程序发布,因此从头开始重新编码频率扫描部分会更容易。这是一个我相信符合您要求的工作计划:

    .data

    .align  4
freq:   .space  1024                # frequency table
ibuf:   .space  800                 # input buffer

msg_prompt: .asciiz "Enter phrase: "
msg_hi1:    .asciiz "The letter '"
msg_hi2:    .asciiz "' has the largest number of occurrences, is present "
msg_hi3:    .asciiz " times.\n"

    .text
    .globl  main
main:
    # prompt user
    li      $v0,4
    la      $a0,msg_prompt
    syscall

    # get input string
    li      $v0,8
    la      $a0,ibuf
    li      $a1,800
    syscall

    # exit program if empty line given
    lbu     $v0,0($a0)
    li      $v1,0x0A
    beq     $v0,$v1,main_exit

    jal     freqcalc                # calculate frequency table
    jal     freqbest                # get best entry
    j       main                    # get another

main_exit:
    li      $v0,10
    syscall

# freqcalc -- calculate frequency
#
# registers:
#   t0 -- current input char value
#   t1 -- miscelaneous
#   t5 -- frequency table current pointer
#   t6 -- current buffer pointer
#   t7 -- frequency table base pointer
freqcalc:
    la      $t7,freq                # get base address of frequency table

    # reset all entries
    li      $t6,256                 # number of entries
    move    $t5,$t7                 # point to table start
freqcalc_zero:
    sw      $zero,0($t5)            # reset entry
    addiu   $t5,$t5,4               # point to next entry
    subi    $t6,$t6,1               # decrement remaining count
    bgtz    $t6,freqcalc_zero

    la      $t6,ibuf                # point to input buffer

freqcalc_loop:
    lbu     $t0,0($t6)              # get current char
    addiu   $t6,$t6,1               # point to next buffer char
    beqz    $t0,freqcalc_done       # at end? if yes, fly

    subu    $t1,$t0,0x61            # subtract 'a'
    bltz    $t1,freqcalc_loop       # reject if too low
    li      $t2,26
    bge     $t1,$t2,freqcalc_loop   # reject if beyond 'z'

    sll     $t5,$t0,2               # get word offset
    add     $t5,$t7,$t5             # point to frequency table entry

    lw      $t0,0($t5)              # get entry value
    addi    $t0,$t0,1               # increment
    sw      $t0,0($t5)              # update entry value

    j       freqcalc_loop

freqcalc_done:
    jr      $ra

# freqbest -- find largest occurrence
#
# RETURNS:
#   t3 -- best letter offset
#   t4 -- best letter frequency
#
# registers:
#   t0 -- current frequency count value
#   t5 -- current letter offset
#   t6 -- number of remaining entries
#   t7 -- frequency table pointer
freqbest:
    la      $t7,freq                # get base address of frequency table
    li      $t6,256                 # number of frequency entries
    li      $t5,0                   # starting letter offset

    li      $t3,0                   # prime the best letter offset
    lw      $t4,0($t7)              # prime the best frequency count

freqbest_loop:
    lw      $t0,0($t7)              # get current frequency count
    ble     $t0,$t4,freqbest_next   # got a higher number? if no, fly

    move    $t4,$t0                 # remember better frequency count
    move    $t3,$t5                 # remember better letter

freqbest_next:
    addiu   $t7,$t7,4               # point to next frequency count
    addi    $t5,$t5,1               # advance letter offset
    subiu   $t6,$t6,1               # bump down count
    bgtz    $t6,freqbest_loop       # at end? if no, loop

freqbest_done:
    beqz    $t3,freqbest_exit       # bug out if no applicable chars matched

    li      $v0,4
    la      $a0,msg_hi1
    syscall

    # print letter with highest frequency
    li      $v0,11
    move    $a0,$t3
    syscall

    li      $v0,4
    la      $a0,msg_hi2
    syscall

    # print frequency count
    li      $v0,1
    move    $a0,$t4
    syscall

    li      $v0,4
    la      $a0,msg_hi3
    syscall

freqbest_exit:
    jr      $ra