MIPS - 帮助从 C 转换代码

发布于 2024-10-27 04:34:55 字数 479 浏览 4 评论 0原文

我是 MIPS 的初学者,我正在尝试编写一个简单的代码,该代码可以在内存中小于 10 个单元(例如 9 个单元)的给定数组上运行,并在屏幕上打印最大的数字。

我写了一个 C 代码来解决这个问题,但我不知道如何将它(没有 mips gcc)转换为工作的 MIPS 汇编代码。

我写的代码:

int N = 9 , i = 0 , biggest = 0 ;
int arr [N] = -2 , 3 , 9 , -1 , 5 , 6 , 10 , 52 , 9 ;

while ( i <= N )
{
    if ( arr [i] > biggest )
        biggest = arr [i] ;

    i++ ;
}

printf ( "biggest number is: %d" , biggest ) ;

如果有人能在 MIPS 汇编中编写该代码并向我解释,我将非常高兴。

谢谢 !

I am a beginner in MIPS and I am trying to write a simple code that runs over a given array in memory that is smaller than 10 cells, lets say 9 cells, and prints on screen the biggest number.

I wrote a C code that solves this issue, but I don't know how to convert it (without mips gcc) to a working MIPS assembly code.

The code I wrote:

int N = 9 , i = 0 , biggest = 0 ;
int arr [N] = -2 , 3 , 9 , -1 , 5 , 6 , 10 , 52 , 9 ;

while ( i <= N )
{
    if ( arr [i] > biggest )
        biggest = arr [i] ;

    i++ ;
}

printf ( "biggest number is: %d" , biggest ) ;

I will be more than happy if someone can write that code in MIPS assembly, and explain it to me.

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-11-03 04:34:56

只需关注循环,尝试类似的操作:

   .text
   .set noreorder
   .global get_max
get_max:
   li    $4, array               // start pointer
   li    $5, array_end-array-4   // end pointer
   li    $2, 0                   // 'biggest' as result
   lw    $6, 0($4)               // load first table entry
1: slt   $3, $2, $6              // boolean flag (biggest<arr[i])
   movn  $2, $6, $3              // update 'biggest' when flag is set
   lw    $6, 4($4)               // load next table entry
   bne   $4, $5, 1b              // continue until we hit end of array
   addiu $4, 4                   // advance to next cell (using bne delay slot)
   jr    $31                     // return to the caller
   nop                           // safely fill the delay slot

           .data
array:     .long -2 , 3 , 9 , -1 , 5 , 6 , 10 , 52 , 9 
array_end: .long 0

将其编译为单独的汇编源文件并与主 C 代码链接。

不要忘记从 C 代码中调用该函数:

printf("biggest=%d\n",get_max());

Just focusing on the loop, try something like that:

   .text
   .set noreorder
   .global get_max
get_max:
   li    $4, array               // start pointer
   li    $5, array_end-array-4   // end pointer
   li    $2, 0                   // 'biggest' as result
   lw    $6, 0($4)               // load first table entry
1: slt   $3, $2, $6              // boolean flag (biggest<arr[i])
   movn  $2, $6, $3              // update 'biggest' when flag is set
   lw    $6, 4($4)               // load next table entry
   bne   $4, $5, 1b              // continue until we hit end of array
   addiu $4, 4                   // advance to next cell (using bne delay slot)
   jr    $31                     // return to the caller
   nop                           // safely fill the delay slot

           .data
array:     .long -2 , 3 , 9 , -1 , 5 , 6 , 10 , 52 , 9 
array_end: .long 0

Compile this into a separate assembly source file and link with your main C code.

Don't forget to call the function from your C code:

printf("biggest=%d\n",get_max());
我要还你自由 2024-11-03 04:34:56

你的初始化有问题...

You have a problem with your initialization...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文