【问题标题】:Initialize an array - MIPS初始化一个数组 - MIPS
【发布时间】:2021-04-04 15:39:27
【问题描述】:

您好,我是汇编语言(MIPS 语言)领域的新手。我正在尝试创建一个数组并用十乘十的值填充它,直到某个数字,然后显示它。由于某种原因,当我编译程序时,我认为它在 sw 执行后停止了,但我不知道为什么程序在此执行后没有运行并显示数组的内容。

这是我的代码:

.data
arr: .space 3000

.text
main:
    la $t0, arr #to => Pointer to arr1
    li $t3, 50   #t3 => Nbr of type run through the loop ==> Max
    li $t6, 10   #t6 = Init value of 10
loop:
    slt $t4, $t5, $t3 #if t5 >= Max t5 = 0
    beq $t4, $zero, display
    
    sw $t6, 0($t0)
    addi $t0,$t0, 4 # Point to the next adress
    addi $t6, $t6,10 #Increment t6 by 10
    
    addi $t5, $t5, 1
    j loop
display:
    li $t5, 0 # Reset t5 to zero
    slt $t4, $t5, $t3
    beq $t4, $zero, end
     
    lw $t6, 0($t0)
    addi $t0, $t0, 4
    
    li $v0, 1
    move $a0, $t6
    syscall 

    addi $t5, $t5, 1
end:    
    li $v0, 10
    syscall

【问题讨论】:

  • 你试过单步吗?你看到了什么?
  • 是的,我看到我的程序在商店执行后没有运行
  • 好的,你能说得更详细一点吗?它是否因错误而停止?有没有。环形? ...

标签: arrays assembly mips


【解决方案1】:

据我了解,您想创建一个包含 [10,20,30... (array_max*10)] 中的数字的数组 这是适合您的工作代码。请注意,通过将 loop_max 更改为您喜欢的任何数字,您可以动态调整数组的大小,而无需进一步努力。 我还建议使用 MARS 进行 MIPS 代码编辑/模拟,并且绝对使用断点来调试您的代码。

.data
arr: .space 3000

.text
.eqv loop_max 50        # represents: array size  & loop max

main:
    la $t0, arr     #to => Pointer to arr1
    li $t3, 0       #t3 => Nbr of type run through the loop ==> Max
    li $t6, 10      #t6 = Init value of 10

array_init_loop:
    beq $t3, loop_max, display
    sw $t6, 0($t0)
    addi $t0,$t0, 4     # Point to the next adress
    addi $t6, $t6,10        # Increment t6 by 10
    addi $t3, $t3, 1        # counter++
    j array_init_loop
    
display:
    li $t3, 0       # counter reset to 0
    la $t0, arr     # we have to load arr original address again because we altered it in the previous loop
    
    display_loop:
    beq $t3, loop_max, end
    # load next element from the array in a0 for print
    lw $a0, 0($t0)
    li $v0, 1       # v0 = 1 for printing integer
    syscall

    # print a space
    li $a0, 32      # 32 = space in ascii table
    li $v0, 11      # v0 = 11 for printing char
    syscall
    #########
    
    addi $t0, $t0, 4    # move to next word in the array (4 bytes)
    addi $t3, $t3, 1    # counter++
    j display_loop
    
end:    
    li $v0, 10
    syscall

运行此输出:

10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500 
-- program is finished running --

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2011-07-30
    • 2011-05-30
    • 2012-11-08
    • 2019-06-25
    相关资源
    最近更新 更多