如何正确读取MIPS中的整数输入?

如何正确读取MIPS中的整数输入?,mips,qtspim,Mips,Qtspim,以下程序1。打印出数组2。给定用户输入的下限和上限,确定该范围内的最小和最小索引 它运行打印数组函数 但是,我尝试在QTSPIM中跟踪寄存器,它没有正确地将下限和上限分别分配给$a0和$a1。事实上,$v0似乎甚至不扫描任何东西。要将扫描的输入从$v0移动到$t0,请尝试改用“移动$t0,$v0”。问题仍然存在 # Ask the user for two indices li $v0, 5 # System call code for read_int

以下程序
1。打印出数组
2。给定用户输入的下限和上限,确定该范围内的最小和最小索引

它运行打印数组函数

但是,我尝试在QTSPIM中跟踪寄存器,它没有正确地将下限和上限分别分配给$a0和$a1。事实上,$v0似乎甚至不扫描任何东西。要将扫描的输入从$v0移动到$t0,请尝试改用“移动$t0,$v0”。问题仍然存在

# Ask the user for two indices
    li   $v0, 5             # System call code for read_int
    syscall 
    add  $t0, $v0, $zero    # store input in $t0          
    sll  $t0, $t0, 2    # relative address position (lower bound)
    add  $a0, $t9, $t0      # array pointer (lower bound) 

    li   $v0, 5             # System call code for read_int
    syscall           
    add  $t0, $v0, $zero    # store input in $t0          
    sll  $t0, $t0, 2    # relative address position (upper bound) 
    add  $a1, $t9, $t0      # array pointer (upper bound) 
完整代码如下。如果有什么不对劲,有人能告诉我吗

# arrayFunction.asm
       .data 
array: .word 8, 2, 1, 6, 9, 7, 3, 5, 0, 4
newl:  .asciiz "\n"

       .text
main:
    # Print the original content of array
    # setup the parameter(s)
    la $a0, array            # base address of array
    add $t9, $a0, $zero      # store base address
    la $a1, 10       # number of elements in array
    # call the printArray function
    jal printArray           # call function 


    # Ask the user for two indices
    li   $v0, 5             # System call code for read_int
    syscall 
    add  $t0, $v0, $zero    # store input in $t0          
    sll  $t0, $t0, 2    # relative address position (lower bound)
    add  $a0, $t9, $t0      # array pointer (lower bound) 

    li   $v0, 5             # System call code for read_int
    syscall           
    add  $t0, $v0, $zero    # store input in $t0          
    sll  $t0, $t0, 2    # relative address position (upper bound) 
    add  $a1, $t9, $t0      # array pointer (upper bound) 

    # Call the findMin function
    # setup the parameter(s)    
    # call the function
    jal findMin     # call function 


    # Print the min item
    # place the min item in $t3 for printing
    addi $t3, $t1, 0 
    # Print an integer followed by a newline
    li   $v0, 1         # system call code for print_int
        addi $a0, $t3, 0        # print $t3
        syscall             # make system call

    li   $v0, 4         # system call code for print_string
        la   $a0, newl      
        syscall             # print newline

    #Calculate and print the index of min item
    la  $a0, array
    add $t3, $v0, $a0
    srl $t3, $t3, 2 

    # Place the min index in $t3 for printing   

    # Print the min index
    # Print an integer followed by a newline
    li   $v0, 1         # system call code for print_int
        addi $a0, $t3, 0        # print $t3
        syscall             # make system call

    li   $v0, 4         # system call code for print_string
    la   $a0, newl      # 
    syscall             # print newline

    # End of main, make a syscall to "exit"
    li   $v0, 10        # system call code for exit
    syscall             # terminate program


#######################################################################
###   Function printArray   ### 
#Input: Array Address in $a0, Number of elements in $a1
#Output: None
#Purpose: Print array elements
#Registers used: $t0, $t1, $t2, $t3
#Assumption: Array element is word size (4-byte)
printArray:
    addi $t1, $a0, 0    #$t1 is the pointer to the item
    sll  $t2, $a1, 2    #$t2 is the offset beyond the last item
    add  $t2, $a0, $t2  #$t2 is pointing beyond the last item
l1: 
    beq  $t1, $t2, e1
    lw   $t3, 0($t1)    #$t3 is the current item
    li   $v0, 1         # system call code for print_int
        addi $a0, $t3, 0        # integer to print
        syscall             # print it
    addi $t1, $t1, 4
    j l1            # Another iteration
e1:
    li   $v0, 4         # system call code for print_string
        la   $a0, newl      # 
        syscall             # print newline
    jr $ra          # return from this function


#######################################################################
###   Student Function findMin   ### 
#Input: Lower Array Pointer in $a0, Higher Array Pointer in $a1
#Output: $v0 contains the address of min item 
#Purpose: Find and return the minimum item 
#              between $a0 and $a1 (inclusive)
#Registers used: $t0 (counter), $t1 (max add), $t2 (min), $v0 (min pos), $t3 (current item)
#Assumption: Array element is word size (4-byte), $a0 <= $a1
findMin:


    lw, $t2, 0($a0)         # initialise min (value) to the lower bound  
    addi $t0, $a0, 0    # initialise $t0 (current pointer) to lower bound 
    addi $t1, $a1, 0    # initialise $t1 (add of end of array) to upper bound 
Loop:   slt $t4, $t1, $t0
    bne $t4, $zero, End     # branch to end if upper < lower

    lw, $t3, 0($a0)     # store the content of the lower array pointer
    slt $t4, $t3, $t2   # if current ($t3) < min ($t2), store 1 in $t4
    beq $t4, $zero, LoopEnd # if it is 0, go to LoopEnd

    addi $t2, $t3, 0    # store content ($t3) as minimum ($t2)
    addi $v0, $t0, 0        # store the address of min

LoopEnd: addi, $t0, 4       # increments current pointer lower bound 
     j Loop         # Jump to loop 
End:    
    jr $ra          # return from this function

#arrayFunction.asm
.数据
数组:。字8、2、1、6、9、7、3、5、0、4
新建:.asciiz“\n”
.文本
主要内容:
#打印数组的原始内容
#设置参数
la$a0,数组#数组的基址
添加$t9、$a0、$零#存储基址
la$a1,数组中的元素数为10
#调用printArray函数
jal printArray#调用函数
#向用户询问两个索引
li$v0,5#读取int的系统调用代码
系统调用
添加$t0、$v0、$零#将输入存储在$t0中
sll$t0,$t0,2#相对地址位置(下限)
添加$a0、$t9、$t0#数组指针(下限)
li$v0,5#读取int的系统调用代码
系统调用
添加$t0、$v0、$零#将输入存储在$t0中
sll$t0,$t0,2#相对地址位置(上限)
添加$a1、$t9、$t0#数组指针(上限)
#调用findMin函数
#设置参数
#调用函数
日航findMin#呼叫功能
#打印最小项
#将最小项目放入$t3中进行打印
附加$t3、$t1、0
#打印后跟换行符的整数
li$v0,1#用于打印的系统调用代码
addi$a0、$t3、0#打印$t3
系统调用#进行系统调用
li$v0,4#打印字符串的系统调用代码
洛杉矶$a0,纽尔
系统调用#打印换行符
#计算并打印最小项的索引
la$a0,阵列
添加$t3、$v0、$a0
srl$t3、$t3、2
#将最小索引置于$t3中进行打印
#打印最小索引
#打印后跟换行符的整数
li$v0,1#用于打印的系统调用代码
addi$a0、$t3、0#打印$t3
系统调用#进行系统调用
li$v0,4#打印字符串的系统调用代码
洛杉矶$a0,纽尔
系统调用#打印换行符
#在main的末尾,对“exit”进行系统调用
li$v0,10#退出系统调用代码
系统调用#终止程序
#######################################################################
###函数printArray###
#输入:$a0中的数组地址,$a1中的元素数
#输出:无
#用途:打印数组元素
#使用的寄存器:$t0、$t1、$t2、$t3
#假设:数组元素为字大小(4字节)
打印阵列:
addi$t1、$a0、0#$t1是指向该项的指针
sll$t2,$a1,2#$t2是超出最后一项的偏移量
添加$t2、$a0、$t2#$t2指向最后一项之外
l1:
贝币$t1、$t2、e1
lw$t3,0($t1)#$t3是当前项目
li$v0,1#用于打印的系统调用代码
要打印的addi$a0、$t3、0#整数
syscall#打印它
附加$t1,$t1,4
j l1#另一个迭代
e1:
li$v0,4#打印字符串的系统调用代码
洛杉矶$a0,纽尔
系统调用#打印换行符
jr$ra#从此函数返回
#######################################################################
###学生函数findMin###
#输入:低位数组指针在$a0,高位数组指针在$a1
#输出:$v0包含最小项的地址
#目的:查找并返回最小项目
#介于$a0和$a1之间(含)
#使用的寄存器:$t0(计数器),$t1(最大添加),$t2(最小值),$v0(最小位置),$t3(当前项)

#假设:数组元素是字大小(4字节),$a0您可以正确地读入整数。问题在别处

  • findMin
    函数中,您使用
    lw、$t3、0($a0)
    ,但应将其与
    $t0
    一起使用,而不是
    $a0
  • 从该函数返回后,您意外地将
    $t1
    保存为最小值,而不是实际保存它的
    $t2
  • 此外,您也不会保存保存min值指针的
    $v0
    ,因此以后会使用一些垃圾数据,而不是预期的数据
  • 从指针计算最小值的索引时,使用的是
    add
    ,但它应该是
    sub
  • 正如
    LoopEnd
    中的注释所述,add在语法上是错误的。它应该是
    addi$t0,$t0,4
    。但这可能只是复制粘贴错误
这是固定代码。已更改,并标记有错误

# arrayFunction.asm
       .data 
array: .word 8, 2, 1, 6, 9, 7, 3, 5, 0, 4
newl:  .asciiz "\n"

       .text
main:
    # Print the original content of array
    # setup the parameter(s)
    la $a0, array            # base address of array
    add $t9, $a0, $zero      # store base address
    la $a1, 10       # number of elements in array
    # call the printArray function
    jal printArray           # call function 


    # Ask the user for two indices
    li   $v0, 5             # System call code for read_int
    syscall 
    add  $t0, $v0, $zero    # store input in $t0          
    sll  $t0, $t0, 2    # relative address position (lower bound)
    add  $a0, $t9, $t0      # array pointer (lower bound) 

    li   $v0, 5             # System call code for read_int
    syscall           
    add  $t0, $v0, $zero    # store input in $t0          
    sll  $t0, $t0, 2    # relative address position (upper bound) 
    add  $a1, $t9, $t0      # array pointer (upper bound) 

    # Call the findMin function
    # setup the parameter(s)    
    # call the function
    jal findMin     # call function 


    # Print the min item
    # place the min item in $t3 for printing
    addi $t3, $t2, 0 # ERROR: min is in $t2 not $t1
    addi $t4, $v0, 0 # ERROR: not saving the pointer to the min element
    # Print an integer followed by a newline
    li   $v0, 1         # system call code for print_int
        addi $a0, $t3, 0        # print $t3
        syscall             # make system call

    li   $v0, 4         # system call code for print_string
        la   $a0, newl      
        syscall             # print newline

    #Calculate and print the index of min item
    la  $a0, array
    sub $t3, $t4, $a0 # ERROR: sub should used not add
    srl $t3, $t3, 2 

    # Place the min index in $t3 for printing   

    # Print the min index
    # Print an integer followed by a newline
    li   $v0, 1         # system call code for print_int
        addi $a0, $t3, 0        # print $t3
        syscall             # make system call

    li   $v0, 4         # system call code for print_string
    la   $a0, newl      # 
    syscall             # print newline

    # End of main, make a syscall to "exit"
    li   $v0, 10        # system call code for exit
    syscall             # terminate program


#######################################################################
###   Function printArray   ### 
#Input: Array Address in $a0, Number of elements in $a1
#Output: None
#Purpose: Print array elements
#Registers used: $t0, $t1, $t2, $t3
#Assumption: Array element is word size (4-byte)
printArray:
    addi $t1, $a0, 0    #$t1 is the pointer to the item
    sll  $t2, $a1, 2    #$t2 is the offset beyond the last item
    add  $t2, $a0, $t2  #$t2 is pointing beyond the last item
l1: 
    beq  $t1, $t2, e1
    lw   $t3, 0($t1)    #$t3 is the current item
    li   $v0, 1         # system call code for print_int
        addi $a0, $t3, 0        # integer to print
        syscall             # print it
    addi $t1, $t1, 4
    j l1            # Another iteration
e1:
    li   $v0, 4         # system call code for print_string
        la   $a0, newl      # 
        syscall             # print newline
    jr $ra          # return from this function


#######################################################################
###   Student Function findMin   ### 
#Input: Lower Array Pointer in $a0, Higher Array Pointer in $a1
#Output: $v0 contains the address of min item 
#Purpose: Find and return the minimum item 
#              between $a0 and $a1 (inclusive)
#Registers used: $t0 (counter), $t1 (max add), $t2 (min), $v0 (min pos), $t3 (current item)
#Assumption: Array element is word size (4-byte), $a0 <= $a1
findMin:
    lw, $t2, 0($a0)         # initialise min (value) to the lower bound  
    addi $t0, $a0, 0    # initialise $t0 (current pointer) to lower bound 
    addi $t1, $a1, 0    # initialise $t1 (add of end of array) to upper bound 
Loop:
    slt $t4, $t1, $t0
    bne $t4, $zero, End     # branch to end if upper < lower

    lw, $t3, 0($t0)     # store the content of the lower array pointer, ERROR: t0 should be used not a0
    slt $t4, $t3, $t2   # if current ($t3) < min ($t2), store 1 in $t4
    beq $t4, $zero, LoopEnd # if it is 0, go to LoopEnd

    addi $t2, $t3, 0    # store content ($t3) as minimum ($t2)
    addi $v0, $t0, 0        # store the address of min

LoopEnd:
    addi $t0, $t0, 4       # increments current pointer lower bound 
    j Loop         # Jump to loop 
End:    
    jr $ra          # return from this function
#arrayFunction.asm
.数据
数组:。字8、2、1、6、9、7、3、5、0、4
新建:.asciiz“\n”
.文本
主要内容:
#打印数组的原始内容
#设置参数
la$a0,数组#数组的基址
添加$t9、$a0、$零#存储基址
la$a1,数组中的元素数为10
#调用printArray函数
jal printArray#调用函数
#向用户询问两个索引
li$v0,5#读取int的系统调用代码
系统调用
添加$t0、$v0、$零#将输入存储在$t0中
sll$t0,$t0,2#相对地址位置(下限)
添加$a0、$t9、$t0#数组指针(下限)
li$v0,5#读取int的系统调用代码
系统调用
添加$t0、$v0、$零#将输入存储在$