开发者

What does this MIPS program do?

开发者 https://www.devze.com 2023-02-15 14:03 出处:网络
I know the program loads the address of .word 5 which is the initial value, I\'m getting confused as to what the program is actually doing. Is it comparing stacks of the word or adding.

I know the program loads the address of .word 5 which is the initial value, I'm getting confused as to what the program is actually doing. Is it comparing stacks of the word or adding.

.data   

 arg:   .word 5 
.text

.globl main

main:

la  $t3, arg    

lw  $t2, 0($t3) 

lw  $t3, 0($t3)


addi    $t1, $zero, 0

beqz    $t2, fin        


  fori:


add $t1, $t1, $t2   

addi    $t3, $t3, -1

bnez开发者_StackOverflow    $t3, fori       


 fin:


li  $v0, 10
syscall 


Looks like it does addition based on the first item in the space as it doesn't access the array from inside the loop.

So for instance if you input a character representing the ascii value of 5 (not '5' itself iirc) you might see something like this:

    int t2 = 5, t3 = 5, t1 = 0;
    do {
         t1 += t2;
         t3 -= 1;
    } while(t3 > 0);

Someone has already answered but I shall provide the translation I did:

.data   

 arg:   .word 5 
.text

.globl main

main:

la  $t3, arg    # load-address of arg into t3
lw  $t2, 0($t3) # load-word from the address in t3 + 0 offset, like t3[0]
lw  $t3, 0($t3) # same but to t3

addi    $t1, $zero, 0 # initialize t1 to 0? weird they don't use li
beqz    $t2, fin # if t2 is zero jump to fin:


  fori:


add $t1, $t1, $t2       # t1 = t1 + t2
addi    $t3, $t3, -1    # t3 -= 1
bnez    $t3, fori       # if(t3 != 0) goto fori


 fin:


li  $v0, 10    # load immediate 10 into v0, which is the syscall to terminate the program
syscall


Disclaimer: I don't have any direct MIPS experience, I just have been around a number of assembly languages.

That said, I think what the program does is calculating the square of 'arg' by repeated addition - in this case 5 * 5 = 25. However, the result in $t1 doesn't seem to be stored anywhere, instead the program just exits.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号