简体   繁体   中英

C++ to MIPS assembly

if i transfer this code to mips.

int arr[3];

cin>>arr[0];

cin>>arr[1];

arr[1]+=arr[0];

cin>>arr[2];

arr[2]+=arr[1];

if i have value of arr[0] in $t0 ,arr[1] in $t1 and address of the arr in $s0.

in this line

arr[1]+=arr[0];

what i should do from this ? use $t1 and $t0 direct like this

add $t1,$t1,$t0

or i should get the value again from the memory in a registers and do the add instruction like this:

lw $s1,($S0)

lw $s2,4($S0)

add $s2,$s2,$s1

what the compiler do ?

There is no need to get the values from the addresses again if you have properly loaded the values into $t0 and $t1 .

Compilers are very complex and what a compiler produces depends on many things, like optimization as Tilo mentioned. The best way to see is to compile it yourself and look at the code produced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM