MIPS汇编语言if语句和goto循环计数pos和neg数字

时间:2017-03-18 05:44:49

标签: mips mips32

您好我正在尝试用汇编语言完成一个函数,该函数计算一维数组中的负数和非负数。我相信我已经正确编写了大部分代码但由于某种原因逐步运行它会让我相信我的“count()”函数甚至没有被实现,因为存储负数和非负数的寄存器当我在MARS中运行代码时,永远不会增加。任何建议或帮助将不胜感激。

伪随机数生成器seed = 1234应该创建一个67个负数和33个非负整数的数组。

#========================================================================================
# Equivalents for MARS System Calls
#========================================================================================
.eqv SYS_EXIT          10
.eqv SYS_PRINT_CHAR    11
.eqv SYS_PRINT_INT      1
.eqv SYS_PRINT_STR      4
.eqv SYS_RAND_INT_RNG  42
.eqv SYS_READ_INT       5
.eqv SYS_SET_SEED      40

#========================================================================================
# Data Section
#
# Contains global data for the program.
#========================================================================================
.data 

s_neg:      .asciiz "Number of negative ints: "
s_nonneg:   .asciiz "Number of non-negative ints: "

#========================================================================================
# Text Section
#
# Contains instructions for the program.
#========================================================================================
.text
main:
    addi    $sp, $sp, -524                  # Allocate 131 words in stack frame

# SysSetSeed(1, 1234)
    addi    $v0, $zero, SYS_SET_SEED        # $v0 = SysSetSeed service code
    addi    $a0, $zero, 1                   # $a0 = pseudorandom number generator id
    addi    $a1, $zero, 1234                # $a1 = seed
    syscall                                 # SysSetSeed(1, 1234)

# for i = ...
    sw      $zero, 512($sp)                 # i = 0
    addi    $t9, $zero, 127                 # $t9 = index of last element of a
    addi    $t8, $zero, 500                 # $t8 = constant 500

main_loop_begin:
    lw      $t0, 512($sp)                   # $t0 = i
    bgt     $t0, $t9, main_loop_end         # if i > 127 drop out of loop

# a[i] = SysRandInt(1, 1000) - 500
    addi    $v0, $zero, SYS_RAND_INT_RNG    # $v0 = SysRandInt service code
    addi    $a0, $zero, 1                   # $a0 = pseudorandom number generator id
    addi    $a1, $zero, 1000                # $a1 = upper limit of range [0, 1000]
    syscall                                 # SysRandInt(1, 1000)
    sub     $t1, $a0, $t8                   # $a0 = SysRandInt(1, 1000) - 500
    sll     $t0, $t0, 2                     # $t0 = 4i
    add     $t0, $sp, $t0                   # $t0 = a + 4i = &a[i]
    sw      $t1, 0($t0)                     # a[i] = SysRandInt(1, 1000) - 500

# i = i + 1
    lw      $t0, 512($sp)                   # $t0 = i
    addi    $t0, $t0, 1                     # $t0 = i + 1
    sw      $t0, 512($sp)                   # i = i + 1
    j       main_loop_begin                 # continue looping

main_loop_end:
    move    $a0, $sp                        # $a0 = &a
    jal     count                           # Call count(a)
    sw      $v0, 516($sp)                   # neg = first return value
    sw      $v1, 520($sp)                   # nonneg = second return value

# SysPrintStr ("Number of negative ints: ")
    addi    $v0, $zero, SYS_PRINT_STR       # $v0 = SysPrintStr service code
    la      $a0, s_neg                      # $a0 = address of string to be printed
    syscall                                 # Call SysPrintStr(...)

# SysPrintInt (neg)
    addi    $v0, $zero, SYS_PRINT_INT       # $v0 = SysPrintInt service code
    lw      $a0, 516($sp)                   # $a0 = neg
    syscall                                 # SysPrintInt(neg)

# SysPrintChar('\n')
    addi    $v0, $zero, SYS_PRINT_CHAR      # $v0 = SysPrintChar service code
    addi    $a0, $zero, '\n'                # $a0 = '\n'
    syscall                                 # SysPrintChar('\n')

# SysPrintStr ("Number of non-negative ints: ")
    addi    $v0, $zero, SYS_PRINT_STR       # $v0 = SysPrintStr service code
    la      $a0, s_nonneg                   # $a0 = address of string to be printed
    syscall                                 # Call SysPrintStr(...)

# SysPrintInt (nonneg)
    addi    $v0, $zero, SYS_PRINT_INT       # $v0 = SysPrintInt service code
    lw      $a0, 520($sp)                   # $a0 = nonneg
    syscall                                 # SysPrintInt(nonneg)

# SysPrintChar('\n')
    addi    $v0, $zero, SYS_PRINT_CHAR      # $v0 = SysPrintChar service code
    addi    $a0, $zero, '\n'                # $a0 = '\n'
    syscall                                 # SysPrintChar('\n')

# Call SysExit()
    addi    $sp, $sp, 524                   # Deallocate stack frame
    addi    $v0, $zero, SYS_EXIT            # $v0 = SysExit service code
    syscall                                 # Call SysExit()

#----------------------------------------------------------------------------------------
# count() -
#----------------------------------------------------------------------------------------
count:
    jr      $ra
# int i = 0;
    addi $sp, $sp, -512 # Allocate 128 words in stack frame: i at 0($sp), $a0 at 4($sp)
    sw $a0, 4($sp) # Save $a0 (param string) in stack frame
    sw $zero, 0($sp) # Initialize i in stack frame to 0
    li $t5, 0
# int neg = 0;
    li $t2, 0
# int nonneg = 0;
    li $t3, 0

    li $t7, 128 #loads t7 with the size of the array
    begin_loop:
    beq $a0, $t7, end_loop #if last
    lw $t4, 0($s0)

# if i ← n + 1 goto end_loop
    bgt $t5, $t7, end_loop
# if arrayi >= 0 jumps to else 1 or neg + 1
    bge $a0,$0, else1  #$a0 holds arrayi 
    addi  $t2, $t2, 1
# ++i;
    lw $t5, 0($sp) # $t0 = i
    addi $t5, $t0, 1 # $t0 = i + 1
    sw $t5, 0($sp) # ++i 
    j begin_loop  #$t0 holds neg and adds 1
    else1:
# else nonneg + 1
    addi  $t3, $t3, 1   #$t1 holds nonneg and adds 1
# ++i;
    lw $t5, 0($sp) # $t0 = i
    addi $t5, $t0, 1 # $t0 = i + 1
    sw $t5, 0($sp) # ++i
# goto begin_loop
    j begin_loop # Continue looping
    end_loop:
    addi $sp, $sp, 8 # Deallocate stack frame
# return neg, nonneg
    move $t2, $v0 #
    move $t3, $v1

    jr $ra # Return
# end function

0 个答案:

没有答案