这条 ARM 线是如何工作的? [英] How does this line of ARM work?

查看:20
本文介绍了这条 ARM 线是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚此汇编代码的工作原理时遇到了一些麻烦.我知道它循环 16 次并更新每次迭代的总数.特别是在 .L2 标签的那一行,自上一行以来不会出现分段错误:ldr ip, [ip, #0] reassigns ip?有人可以解释这是如何工作的吗?提前感谢您抽出宝贵时间.

I am having some trouble figuring how this assembly code works. I know it loops 16 times and updates the total every iteration. Specifically on the line at the .L2 label, wouldn't that give a segmentation fault since the previous line: ldr ip, [ip, #0] reassigns ip? Can someone explains how this works? Thank you in advance for your time.

.L5:
    .word data
    .word total
_start:
    ldr ip, .L5
    mov r1, #0
    ldr r0, .L5+4
    mov r3, r1
    mov r2, r1
    ldr ip, [ip, #0]
    str r1, [r0, #0]
.L2:
    ldr r1, [ip, r3]
    add r3, r3, #4
    cmp r3, #64
    add r2, r2, r1
    str r2, [r0, #0]
    bne .L2

推荐答案

您的代码:

  • 加载ipdata的地址和r0total的地址.
  • 将寄存器 r1r2r3 归零
  • data的内容加载到ip中,替换之前的内容(data的地址).这不是段错误.您可以从内存加载到任何寄存器,包括您刚刚用来指定加载的有效地址的寄存器.所有发生的事情都是平常的:无论旧值(在本例中为指针)都替换为新值(指针对象).
  • 0 存储到 total 中.
  • 重复 16 次:
    • 使用ip作为整数数组的基地址,将距基地址偏移r3处的整数加载到r1中.
    • r1累加到寄存器r2中.
    • r2 存储到 total.
    • Loads the addresses of data in ip and the address of total in r0.
    • Zeroes registers r1, r2 and r3
    • Loads into ip the contents of data, replacing the previous contents (the address of data). This does not segfault. You are allowed to load from memory into any register, including the one you just used to specify the effective address for the load. All that happens is the usual: Whatever old value was there (in this case, the pointer) is replaced with the new value (the pointee).
    • Stores 0 into total.
    • Repeats 16 times:
      • Using ip as the base address of an array of integers, load into r1 the integer at offset r3 from the base address.
      • Accumulate r1 into the register r2.
      • Store r2 to total.

      基于此,我认为 data 的类型为 int*total 的类型为 int>.因此,我对您的汇编程序的解释是:

      Based on this, I believe that data is of type int* and that total is of type int. Therefore, my interpretation of your assembler is:

      伪代码-y C 类汇编器

      int* data;
      int total;
      
      
      void start(void){
          int** ip = &data;
          int*  r0 = &total;
          int r1=0, r2=r1, r3=r1;
      
          int* ipnew = *ip;
          *r0 = r1;/* total = 0; */
      
          do{
              r1 = *(int*)((char*)ipnew + r3);/* r1 = ipnew[r3/4]; */
              r3 += 4;
              r2 += r1;
              *r0 = r2;/* total = r2; */
          }while(r3 != 64);
      }
      

      纯 C

      int* data;
      int  total;
      
      void start(void){
          int* arr = data;      /* ip */
          register int  sum = 0;/* r2 */
          register int  i   = 0;/* r3, divided by 4 */
      
          total = 0;
      
          do{
              sum   += arr[i++];/* r1 contains the value loaded from the array. */
              total  = sum;     /* r0 contains &total */
          }while(i != 16);
      }
      

      这篇关于这条 ARM 线是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆