尝试更改MIPS程序集中的数组值

时间:2018-10-21 16:51:28

标签: arrays assembly mips

我希望我能得到一些调试某些代码的帮助。我将尽量避免使它成为递归的MIPS组装项目。该程序仅应打印质数。  我的想法是,我有两个数组,一个数组列出每个整数,最多110个,另一个数组作为初始化为所有整数的标志数组。   对于标志数组中每个为1的位置,它都会打印数字,而对于每0则不打印数字。好消息是我找出了所有循环的问题,我遇到的问题是将值0保存到当前位置的标志数组中。我引用的所有内容都涉及使用:

sw [register], ([register])

据我了解,这是如何将一个寄存器的内容保存到另一个寄存器的位置?它似乎对我不起作用。我继续将代码发布到下面,这有点杂乱,而且很长,但是我递归的代码却写着Eratosthenes。

.data

NUM_ARRAY:  .space 800   # 800-byte memory space
FLAG_ARRAY: .space 800   # 800-byte memory space

START_MSG: .asciiz   "The prime numbers up to 110 are:\n"
END_MSG:   .asciiz   "Completed ...."
SPACE:     .asciiz   "  "
NEWLINE:   .asciiz   "\n"
BUG1:     .asciiz   "Out of loop"
INK:      .asciiz   "incrementK"
INX:      .asciiz   "incrementX"
.text
.globl main

# THE BEGINNING OF MODULE MAIN #######################
main:
    # save the ten registers --------------------------
    subu  $sp, $sp, 48    # stack frame for 12 registers

    sw    $ra,   ($sp)
    sw    $s0,  4($sp)
    sw    $s1,  8($sp)
    sw    $s2, 12($sp)
    sw    $s4, 16($sp)
    sw    $s5, 20($sp)
    sw    $t0, 24($sp)
    sw    $t1, 28($sp)
    sw    $t2, 32($sp)
    sw    $v0, 36($sp)
    sw    $a0, 40($sp)

    # initialize the essential parameters -------------
    li    $s0, 111         # the largest number (always - 1)
    li    $s1, 10          # the square-root of $s0 register
    li    $s2, 2           # the initial value for "k"
    li    $s4, 0           # the first numbers to consider
    li    $s5, 1           # '1' - it is a prime number

    li    $t2, 0           # loop ounter

# show an opening message ---------------------------------
    li    $v0, 4           # system call #4
    la    $a0, START_MSG
    syscall

# initialize the two arrays -------------------------------
    la    $t0, NUM_ARRAY   # set the address of the 1st array
    la    $t1, FLAG_ARRAY  # set the address of the 2nd array

loop1:  sw    $s4, ($t0)       # load a number
    sw    $s5, ($t1)       # load the initial property (1 - a prime)

    addi  $t2, $t2, 1      # increase the loop counter by one

    beq   $t2, $s0, EXIT1  # if all the array elements are set up

    addi  $s4, $s4, 1      # increase the number by one

    addu  $t0, $t0, 4      # increase the pointer by four bytes
    addu  $t1, $t1, 4      # increase the pointer by four bytes

    j     loop1            # repeat to "loop1"

EXIT1:  li $s2, 2
    li $s6, 2

    jal   Eratosthenes     # start recursive Eratosthenes

    li    $v0, 4           # system call #4
    la    $a0, NEWLINE
    syscall
    li    $v0, 4           # system call #4
    la    $a0, NEWLINE
    syscall

    li    $t2, 0           # reset the loop counter

    la    $t0, NUM_ARRAY   # reset the address of the 1st array
    la    $t1, FLAG_ARRAY  # reset the address of the 2nd array

loop3:  lw    $a0, ($t1)        # load the number there
    beq   $a0, $zero, SKIP1 # if the target is '0': skip
    beq   $t2, $zero, SKIP1 # if repeated enough: skip

    li    $v0, 1           # system call #1
    lw    $a0, ($t0)       # load the number there
    syscall

    li    $v0, 4           # system call #4
    la    $a0, NEWLINE
    syscall

SKIP1:  addi  $t2, $t2, 1      # increase the loop counter

    addu  $t0, $t0, 4      # increase the pointer by four bytes
    addu  $t1, $t1, 4      # increase the pointer by four bytes

    blt   $t2, $s0, loop3

    li    $v0, 4           # system call #4
    la    $a0, END_MSG
    syscall

# restore registers ---------------------------------------
    lw    $ra,   ($sp)
    lw    $s0,  4($sp)
    lw    $s1,  8($sp)
    lw    $s2, 12($sp)
    lw    $s4, 16($sp)
    lw    $s5, 20($sp)
    lw    $t0, 24($sp)
    lw    $t1, 28($sp)
    lw    $t2, 32($sp)
    lw    $v0, 36($sp)
    lw    $a0, 40($sp)

    addu  $sp, $sp, 48     # delete the stack frame

    jr    $31              # park it at the parking spot

# THE END OF MODULE MAIN ##########################################

Eratosthenes:
        subu $sp,$sp,36

        sw $ra, 0($sp)
        sw $t5, 4($sp)
        sw $t6, 8($sp)
        sw $t0, 12($sp)
        sw $t1, 16($sp)
        sw $t2, 20($sp)
        sw $s4, 24($sp)
        sw $s5, 28($sp)
        sw $s0, 32($sp)

        li $s5, 0 #change s5 to 0
        li $s6, 2 #reset the x
        li $t2, 0
        la $t0, NUM_ARRAY #reset Array 1
        la $t1, FLAG_ARRAY #reset Array 2

R_Loop:
        mult $s2,$s6
        mflo $t7
        addi $t2,$t2,1


        lw $s4, ($t0)

        beq $t7, $s4, SKIPone
        beq $t2, $s0, EXITL #branch if counter is max

        addu $t0, $t0, 4 #next spot in NUM_ARRAY
        addu $t1, $t1, 4 #next spot in FLAG_ARRAY

        li $v0,1
        move $a0, $t7
        syscall

        li $v0,4
        la $a0, NEWLINE
        syscall

        li $v0,4
        la $a0, NEWLINE
        syscall



        j R_Loop
SKIPone:
        addi $s6,$s6,1 #increment x by one

        sw $zero, ($t1) #attempt at making position at array 0
        #have also tried 
        #sw $s5, ($t1) #where s5 is initialized at 0
        #please take note that offset is 0, because it needs to be
        #at the current position at the array

        li $v0,4
        la $a0, NEWLINE
        syscall

        li $v0,4
        la $a0, NEWLINE
        syscall

        li $v0,1
        move $a0, $t4 #debug aid
        syscall

        li $v0,4
        la $a0, NEWLINE
        syscall

        li $v0,4
        la $a0, NEWLINE
        syscall

        beq $t2 $s0, EXITL
        li $v0, 4
        la $a0, INX  #debug aid
        syscall

        j R_Loop
EXITL:      
        li $t8,10

        li $v0, 4
        la $a0, INK #debug aid
        syscall

        beq $s2,$t8, EXITR
        addi $s2,$s2,1 #increment k by one
        jal Eratosthenes
        li $v0 4
        la $a0, BUG1
        syscall

EXITR:


        lw $ra, 0($sp)
        lw $t5, 4($sp)
        lw $t6, 8($sp)
        lw $t0, 12($sp)
        lw $t1, 16($sp)
        lw  $t2, 20($sp)
        lw $s4, 24($sp)
        lw $s5, 28($sp)
        lw $s0, 32($sp)
        addu $sp,$sp,36

        jr $ra

感谢任何帮助,似乎没有其他工作,我的老师也没有帮助。

0 个答案:

没有答案