Convert C++ to MIPS assembly

半腔热情 提交于 2019-12-31 03:31:07

问题


This code is about to find maximum element from an array i want to convert this code into MIPS assembly code can anyone help me...Or just tell me how to initialize an array in MIPS.

void max_array()
{
    int a[10]={2,3,421,4,32,4,3,1,4,5},max;
    for(int i=0;i<9;i++)
    {
        cout<<a[i];
    }
    max=a[0];
    for(int j=1;j<9;j++)
    {
        if (max<a[j])
        {
             max=a[j];
        }
    }
    return max;

 }

回答1:


Here's an example

        .data
array1:     .space  12              #  declare 12 bytes of storage to hold array of 3 integers
        .text
__start:    la  $t0, array1     #  load base address of array into register $t0
        li  $t1, 5                  #  $t1 = 5   ("load immediate")
        sw $t1, ($t0)                #  first array element set to 5; indirect addressing
        li $t1, 13                  #   $t1 = 13
        sw $t1, 4($t0)           #  second array element set to 13
        li $t1, -7                  #   $t1 = -7
        sw $t1, 8($t0)           #  third array element set to -7
        done



回答2:


    .data   # variable decleration follow this line

array1: .word   2, 3, 421, 4, 32, 4, 3, 1, 4, 5 #10 element array is declared
max:    .word   2

la  $t0, array1         #load base address of array1
main:                       #indicated the start of the code
#to access an array element ($t0), 4($t0), 8($t0)..........


来源:https://stackoverflow.com/questions/7538659/convert-c-to-mips-assembly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!