简体   繁体   English

如何在MSP430上的汇编器中写入外部变量

[英]How to write to external variable in assembler on the msp430

I am currently working with the TI MSP430 and wrote the assembler code shown below. 我目前正在使用TI MSP430,并编写了如下所示的汇编代码。 I want to write the value '1' to the variable var, but indirectly via var_ptr, which holds the address of var. 我想将值“ 1”写入变量var,但要通过var_ptr(包含var地址)间接写入。 After reading about addressing modes in the User-Guide I thought this should work by using & in front of the pointer variable. 在阅读了《用户指南》中的寻址模式后,我认为这应该通过在指针变量前面使用&来起作用。

///< For testing
.extern var;
.extern var_ptr;

///< A function for testing different commands
.global testfunc
  .type testfunc, @function
testfunc:
  mov #1, &var_ptr

  ret

Those two external variables are defined in another c file. 这两个外部变量在另一个c文件中定义。

uint16_t    var = 0;
uint16_t*   var_ptr = 0;

I am printing the content of both values before and after the function call. 我在函数调用之前和之后都打印两个值的内容。

var_ptr = &var;
DEBUG_PRINT(("var: %u, var_ptr: %u\n", var, var_ptr));
testfunc();
DEBUG_PRINT(("var: %u, var_ptr: %u\n", var, var_ptr));

Results: 结果:

mov #1, &var_ptr

var: 0, var_ptr: 9630<\n>
var: 0, var_ptr: 1<\n>
-------------------------------------------
mov #1, var_ptr

var: 0, var_ptr: 9630<\n>
var: 0, var_ptr: 1<\n>

Independently of using &, the value '1' is always written directly to the variable var_ptr, but not to var. 与使用&无关,值“ 1”总是直接直接写入变量var_ptr,而不是var。 What is the correct way to write to the variable var using var_ptr? 使用var_ptr写入变量var的正确方法是什么?

EDIT: A great explanation about addressing modes can be found here . 编辑:有关寻址模式的详细说明,请参见此处

In MSP430 assembly syntax, &ADDR and ADDR only differ in that the former specifies a PC-relative address while the latter specifies an absolute address. 在MSP430汇编语法中, &ADDRADDR区别仅在于前者指定PC相对地址,而后者指定绝对地址。 The difference is mostly relevant for position independent code. 区别主要与位置无关代码有关。 To implement what you want, you need to perform two moves: 要实现所需的功能,您需要执行以下两个步骤:

mov var_ptr, r4 // load content of var_ptr into r4
mov #1, @r4     // write #1 to where r4 points

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM