我该如何压缩这个MIPS代码

时间:2016-04-03 01:10:32

标签: mips

所以我有点像MIPS的菜鸟。但我觉得有一种方法可以压缩下面的代码。 现在我必须将整个循环复制/粘贴9次,从A增加到I,你可以看到bge $t0, $t1, arrayloopB进入下一个循环。因为它代码工作,如果需要,我可以交付它,但我想改进它,所以它更容易阅读。 有没有办法增加" sizeA"和" A"到" sizeB"和" B"等等

问题:

You are provided with the location of all the items in the store. The information is given to you separated
by aisle, on array format. The total number of items in store is a 50. There are cases where the same item
can be found in multiple aisles. There are also cases where the item cannot be found in store.

其余的res只是概述了要采取的步骤。

所以这是完整的代码:

.data
A: .word 28, 10, 23, 12, 22, 9, 20, 46, 17, 50, 6
B: .word 23, 46, 5, 10, 10, 45, 24, 31, 17, 1, 23, 45, 32
C: .word 37, 12, 29, 39, 47, 41
D: .word 6, 10, 23, 12, 7, 9, 13, 46, 19, 5, 12
E: .word 2, 6, 5, 10, 18, 45, 27, 31, 36, 42, 38, 42, 38
F: .word 27, 12, 22, 19, 37, 43
G: .word 31, 3, 2, 1, 8, 9, 26, 18, 15, 37, 43
H: .word 23, 46, 5, 19, 1, 45, 24, 31, 16, 12, 28, 45, 32
I: .word 34, 23, 2, 9, 50, 46
size11: .word 11
size13: .word 13
size6: .word 6

arrayNameA: .asciiz " is in aisle A\n"
arrayNameB: .asciiz " is in aisle B\n"
arrayNameC: .asciiz " is in aisle C\n"
arrayNameD: .asciiz " is in aisle D\n"
arrayNameE: .asciiz " is in aisle E\n"
arrayNameF: .asciiz " is in aisle F\n"
arrayNameG: .asciiz " is in aisle H\n"
arrayNameH: .asciiz " is in aisle G\n"
arrayNameI: .asciiz " is in aisle I\n"

empty1: .asciiz "We apologize for the inconvenience, your item '" 
    empty2: .asciiz "' is out of stock \n"

userPrompt1: .asciiz "Please input type first number "
userPrompt2: .asciiz "Please input type second number "
userPrompt3: .asciiz "Please input type third number "


.text
main:
    li $v0, 4
    la $a0, userPrompt1
    syscall
take1:
    li $v0, 5
    syscall
    move $s0, $v0
    li $v0, 4
    la $a0, userPrompt2
    syscall
take2:
    li $v0, 5
    syscall
    move $s1, $v0
    li $v0, 4
    la $a0, userPrompt3
    syscall
take3:
    li $v0, 5
    syscall
    move $s2, $v0

arrayLoopA:
    li $t0, 0       #start ticker for array loop at 0
    lw $t1, size11      #store array size as endpoint for loop
    lw $t2, A       #store first value of array
    la $t3, A       #store address of the first value
    j loop1

loop1:
    bge $t0, $t1, arrayLoopB    #check array loop tick to see if erray loop 
    lw $t4, ($t3)
    beq $s0, $t4, saveA0
    beq $s1, $t4, saveA1
    beq $s2, $t4, saveA2
    return1:
    addi $t0, $t0, 1    #tick ticker for array loop by one
    addi $t3, $t3, 4    #tick adress rigth by for bits (getting next value)
    j loop1

    saveA0:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameA
    syscall
    addi $t5, $t5, 1
    beq $s1, $t4, saveA1
    beq $s2, $t4, saveA2
    j return1

    saveA1:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameA
    syscall
    addi $t6, $t6, 1
    beq $s2, $t4, saveA2
    j return1

    saveA2:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameA
    syscall
    addi $t7, $t7, 1
    j return1

arrayLoopB:
    li $t0, 0       #start ticker for array loop at 0
    lw $t1, size13      #store array size as endpoint for loop
    lw $t2, B       #store first value of array
    la $t3, B       #store address of the first value
    j loop2

loop2:
    bge $t0, $t1, arrayLoopC    #check array loop tick to see if erray loop 
    lw $t4, ($t3)
    beq $s0, $t4, saveB0
    beq $s1, $t4, saveB1
    beq $s2, $t4, saveB2
    return2:
    addi $t0, $t0, 1    #tick ticker for array loop by one
    addi $t3, $t3, 4    #tick adress rigth by for bits (getting next value)
    j loop2

    saveB0:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameB
    syscall
    addi $t5, $t5, 1
    beq $s1, $t4, saveB1
    beq $s2, $t4, saveB2
    j return2

    saveB1:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameB
    syscall
    addi $t6, $t6, 1
    beq $s2, $t4, saveB2
    j return2

    saveB2:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameB
    syscall
    addi $t7, $t7, 1
    j return2


arrayLoopC:
    li $t0, 0       #start ticker for array loop at 0
    lw $t1, size6       #store array size as endpoint for loop
    lw $t2, C       #store first value of array
    la $t3, C       #store address of the first value
    j loop3

loop3:
    bge $t0, $t1, final #arrayloopC #check array loop tick to see if erray loop 
    lw $t4, ($t3)
    beq $s0, $t4, saveC0
    beq $s1, $t4, saveC1
    beq $s2, $t4, saveC2
    return3:
    addi $t0, $t0, 1    #tick ticker for array loop by one
    addi $t3, $t3, 4    #tick adress rigth by for bits (getting next value)
    j loop3

    saveC0:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameC
    syscall
    addi $t5, $t5, 1
    beq $s1, $t4, saveC1
    beq $s2, $t4, saveC2
    j return3

    saveC1:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameC
    syscall
    addi $t6, $t6, 1
    beq $s2, $t4, saveC2
    j return3

    saveC2:
    li $v0, 1
    la $a0, ($t4)
    syscall
    li $v0, 4
    la $a0, arrayNameC
    syscall
    addi $t7, $t7, 1
    j return3

final:
    beqz $t5, noStock0
    exit1:
    beqz $t6, noStock1
    exit2:
    beqz $t7, noStock2
    j exit

    noStock0:
    li $v0, 4
    la $a0, empty1
    syscall
    li $v0, 1
    la $a0, ($s0)
    syscall
    li $v0, 4
    la $a0, empty2
    syscall
    j exit1

    noStock1:
    li $v0, 4
    la $a0, empty1
    syscall
    li $v0, 1
    la $a0, ($s1)
    syscall
    li $v0, 4
    la $a0, empty2
    syscall
    j exit2

    noStock2:
    li $v0, 4
    la $a0, empty1
    syscall
    li $v0, 1
    la $a0, ($s2)
    syscall
    li $v0, 4
    la $a0, empty2
    syscall
    j exit

exit:

你会注意到它现在正常工作,在A到C之间循环(不想复制9次),但我不知道如何让它变小。你怎么正确地停止该计划?

0 个答案:

没有答案