c 到 mips 源和目标指针

标签 c mips

#include <stdio.h>

int source[] = {3, 1, 4, 1, 5, 9, 0};
int dest[10];

int main ( ) {
    int k;
    for (k=0; source[k]!=0; k++) {
    dest[k] = source[k];
    }
    printf ("%d values copied\n", k);
    return 0;
}

这是C代码

很简单,它只是将数组的值复制到 dest 数组,除非它是 0

现在,问题出在mips代码上

    .file   1 "arrcopy.c"
    .globl  source
    .data
    .align  2
source:
    .word   3
    .word   1
    .word   4
    .word   1
    .word   5
    .word   9
    .word   0
    .rdata
    .align  2
$LC0:
    .ascii  "%d values copied\n\000"
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $sp,24,$31      # vars= 0, regs= 1/0, args= 16, extra= 0
    .mask   0x80000000,-8
    .fmask  0x00000000,0
    subu    $sp,$sp,24
    sw  $31,16($sp)
    jal __main
    la  $9,source
    lw  $2,0($9)
    move    $8,$0
    beq $2,$0,$L8
    move    $7,$0
    la  $10,dest
$L6:
    addu    $8,$8,1
    sll $3,$8,2
    addu    $5,$7,$9
    addu    $2,$3,$9
    addu    $6,$7,$10
    lw  $4,0($2)
    move    $7,$3
    lw  $3,0($5)
    #nop
    sw  $3,0($6)
    bne $4,$0,$L6
$L8:
    la  $4,$LC0
    move    $5,$8
    jal printf
    lw  $31,16($sp)
    move    $2,$0
    addu    $sp,$sp,24
    j   $31
    .end    main

    .comm   dest,40

我什至分不清源指针和目标指针是什么

甚至不确定循环何时开始

我想它从 $L6 开始

它使用什么指令来加载源指针和目标指针的地址?

如果你们能找到循环,你们能告诉我每一行在做什么吗?

它看起来和手工看起来很不一样

最佳答案

我添加了一些注释来解释反汇编:

  la  $9,source     # The source pointer
  lw  $2,0($9)      # Load source[0]
  move    $8,$0     # "next_k"
  beq $2,$0,$L8     # skip the loop if source[0]==0
  move    $7,$0     # k
  la  $10,dest      # destination pointer
$L6:
  addu    $8,$8,1   # Increase the next_k counter
  sll $3,$8,2       # scale to a word offset (multiply by 4)
  addu    $5,$7,$9  # $5 = &source[k]
  addu    $2,$3,$9  # $2 = &source[next_k]
  addu    $6,$7,$10 # $6 = &dest[k]
  lw  $4,0($2)      # $4 = source[next_k]
  move    $7,$3     # k = next_k (i.e. k++)
  lw  $3,0($5)      # $3 = source[k]
  #nop
  sw  $3,0($6)      # dest[k] = source[k]
  bne $4,$0,$L6     # Loop until source[next_k]==0
$L8:

关于c 到 mips 源和目标指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337555/

相关文章:

c - 如何在没有头/尾指针的情况下将多个节点添加到C中的链表中?

c - 存储大随机数的最佳哈希函数是什么?

c - gcc -O 优化 : Help me understand the effect

c - 使用 SetParent 将 HWND 嵌入到外部进程中

Verilog 实现 a<b ? 1 : 0

c - 了解程序如何加载到内存并执行的任何动手练习

c - 隐式有符号到无符号转换 mplab xc8

c++ - MIPS 32 位上缺少 __sync_fetch_and_add_8 的替代方法

c - 在 ubuntu 中为 MIPS 编译 netcat 的问题

c - C MIPS 环境中延迟 1 微秒