Max最多不在Assembly中填充

时间:2017-10-02 23:45:23

标签: assembly mips

我已经能够正确填充最大值,但这导致min填充为零。这将显示正确的唯一时间是,如果零实际上是用户输入的整数之一。例如,如果我选择输入4个整数,如7,4,2,6。输出将显示Min = 0和Max = 7.有没有人想过为什么会这样?

# Reinitialize counters, reload array address for printing
li $t0, 0               # Initilize array index value back to 0
li $t3, 0               # Initial counter back to zero

# Reinitialize counters, reload array address for min/max calculations
la $t0,array            # Load address of array back -> t0
lw $t1,count            # t1 = count, exit loop when it goes to 0
lw $t5,($t0)            # t5 = min = a[0] (initialization)
lw $t6,($t0)            # t6 = max = a[0] (initialization)

add $t0,$t0,4           # move pointer ahead to next array element a[1]
add $t1,$t1,-1          # decrement counter to keep in step with array   

loop:   lw $t4,($t0)                # Load next array(t0) element -> t4
        bge $t4,$t5,notMin          # IF array element is >= min go to notMin
        move $t5,$t4                # Move array element t4 -> min      
        j notMax

notMin: ble $t4,$t6,notMax          # IF array element t4 is <= max go to notMax
        move $t6,$t4                # ELSE Move array element t4 -> max

notMax: add $t1,$t1,-1              # Decrement counter by 1
        add $t0,$t0,4               # Increment index to point to next word
        bnez $t1,loop               # Loop back to top if count != 0

la $a0,p1               # Load address of output string -> a0
li $v0,4                # Load call code for display a string [4] -> v0
syscall                 # System call to display "The Minimum Number is: "

move $a0,$t5            # Display the minimum number 
li $v0,1                # Load the call code to display an integer[1] -> v0
syscall                 # System call to print integer

la $a0,p2               # Load address of output string -> a0
li $v0,4                # Load call code for display a string [4] -> v0
syscall                 # System call to display "The Maximum Number is: "

move $a0,$t6            # Display the maximum number 
li $v0,1                # Load the call code to display an integer[1] -> v0
syscall                 # System call to print integer

# Move to new line for readability  
la $a0,endl             # Load new line character -> a0
li $v0,4                # Load call code to print a string[4] -> v0
syscall                 # System call to print new line character

li $v0,10               # End Of Program
syscall                 # Call to system 


.data
prompt1:    .asciiz "Enter Number of Integers :"    # Initial Prompt to set array size
intPrompt:  .asciiz "Enter an Integer: "            # Prompt for each int in the array  
title:      .asciiz "\nArray: "                     # Output String of "Array:"
comma:      .asciiz ", "                            # Output of comma
count:      .word 15                                # Loop counter for min/max
p1:         .asciiz "\nThe Minimum Number is: "     # Minimum Number String
p2:         .asciiz "\nThe Maximum Number is: "     # Maximum Number String
endl:       .asciiz "\n"                            # New line character
array:      .word 60                                # 15 words

非常新的装配,有一个阵列,需要找到最小值和最大值并显示,min很好,无法找出我的最大值出错的地方,非常感谢任何帮助。为清晰起见,我只发布了实际的循环部分和数据部分。

# Reinitialize counters, reload array address for min/max calculations
li $t0, 0               # Initilize array index value back to 0
la $t0,array            # Load address of array back -> t0

lw $t1,count            # t1 = count, exit loop when it goes to 0
lw $t5,0($t0)           # t5 = min = a[0] (initialization)
lw $t6,0($t0)           # t6 = max = a[0] (initialization)

add $t0,$t0,4           # move pointer ahead to next array element a[1]
add $t1,$t1,-1          # decrement counter to keep in step with array   

loop:   lw $t4,($t0)                # t4 = next element in array
        bge $t4,$t5,notMin          # if array element is >= min goto notMin
        move $t5,$t4                # min = a[i]
        j notMax

notMin: ble $t4,$t6,notMax          # if array element is <= max goto notMax
        move $t6,$t4                # max = a[i]

notMax: add $t1,$t1,-1              # t1 -- -> counter --
    add $t0,$t0,4                   # increment counter to point to next word
    bnez $t1,loop

    la $a0,p1           # Load address of output string -> a0
    li $v0,4            # Load call code for display a string [4] -> v0
    syscall             # System call to display "The Minimum number is "

    move $a0,$t5        # Display the minimum number 
    li $v0,1            # Load the call code to display an integer[1] -> v0
    syscall             # System call to print integer

    la $a0,p2           # Load address of output string -> a0
    li $v0,4            # Load call code for display a string [4] -> v0
    syscall             # System call to display "The Maximum number is "

    move $a0,$t6        # Display the maximum number 
    li $v0,1            # Load the call code to display an integer[1] -> v0
    syscall             # System call to print integer

    la $a0,endl         # Display "cr/lf"
    li $v0,4            # a0 = address of message
    syscall             # v0 = 4 which indicates display a string

    li $v0,10           # End Of Program
    syscall             # Call to system

.data
array:      .word 60                            # 15 words
count:      .word 15                            # Loop counter for min/max
p1:         .asciiz "\nThe Minimum number is "  # Minimum Number String
p2:         .asciiz "\nThe Maximum number is "  # Maximum Number String
endl:       .asciiz "\n"                        # New line character

0 个答案:

没有答案