首页 > 解决方案 > asm文件之间的变量导出

问题描述

在 asm file1 中,我尝试导出一个变量并在另一个中使用它。我试图从手册和教程中找到如何做到这一点,但没有成功。

那么,如何在 asm 文件之间共享一个全局变量呢?

  // File 1
  // Here is saved value of a register (r10) to a variable.
  .section .data
  .global r10_save
  r10_save_addr:   .word  r10_save
  
  .section .text
  ldr r13, =r10_save_addr   // Load address for the global variable to some reg (r13)
  str r13, [r10]            // Save r13 to global variable
  // File 2
  // Here the intention is to use the variable (that should have r10 value stored).
  .section .data
  
  str_r10:
  .asciz "r10 = 0x"
  strlen_r10 = .-str_r10
  
  
  .section .text
  
  /* Here I want to use the reference of a variable 
     which has got its value in other file.
  */
  mov r0, $1            //
  ldr r1, =str_r10      // address of text string
  ldr r2, =strlen_r10   // number of bytes to write
  mov r7, $4            //
  swi 0

标签: variablesassemblyarm

解决方案


您可以使用extern 来获取变量的值global

// File 2
  // Here the intention is to use the variable (that should have r10 value stored).
  .section .data
  
  str_r10:
  .asciz "r10 = 0x"
  strlen_r10 = .-str_r10
  
  
  .section .text
  
  /* Here I want to use the reference of a variable 
     which has got its value in other file.
  */

  .extern r10_save // or EXTERN DATA(r10_save)
  

  mov r0, $1            //
  ldr r1, =str_r10      // address of text string
  ldr r2, =strlen_r10   // number of bytes to write
  mov r7, $4            //
  swi 0

然后你也可以访问r10_save第二个文件。


推荐阅读