简体   繁体   English

ARM汇编,POP堆栈并存储在寄存器中

[英]ARM Assembly, POP Stack and store in register

I was wondering how I could POP a variable off the stack and store it in a register. 我想知道如何从堆栈中POP一个变量并将其存储在寄存器中。 For example: 例如:

asm(" MOV R5, #10");
asm ("MOV R6, #20");
asm("PUSH {R5,R6}");

I now want to POP the variables off the stack and store the value of R6 in R5 and vica versa. 我现在想POP从堆栈变量和的值存储R6R5和正相反。 I have searched the ARM site ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/Babefbce.html ) but I can't find a valuable answer. 我已经搜索了ARM网站( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/Babefbce.html ),但是找不到有用的答案。 I hope someone could help me. 我希望有人可以帮助我。

EDIT: 编辑:

I figured this would do the job: 我认为这可以完成工作:

  asm("MOV R5, #10");
  asm("MOV R6, #20");
  asm("PUSH {R5,R6}");
  asm("POP {R5,R6}");

But the registers do not change according to my debugger. 但是根据我的调试器,寄存器不会更改。

The problem you are encountering is that when multiple registers are specified in a PUSH or POP they are always pushed or popped in numerical order. 您遇到的问题是,当在PUSHPOP中指定多个寄存器时,它们总是按数字顺序推送或弹出。 The order that they are specified in the instruction does not matter. 指令中指定它们的顺序无关紧要。 The only way to force the register contents to be swapped in this manner is to perform one of the operations (either the push or the pop) on the two registers individually, in two separate instructions. 强制以这种方式交换寄存器内容的唯一方法是在两条单独的指令中分别对两个寄存器执行操作之一(推入或弹出)。 You will need to check the documentation for the PUSH and POP instructions to determine the default order used when handling multiple registers, and then intentionally reverse that order using two separate instructions. 您将需要查看PUSHPOP指令的文档,以确定处理多个寄存器时使用的默认顺序,然后有意地使用两条单​​独的指令来颠倒该顺序。

push and pop are both pseudo-instructions. push和pop都是伪指令。

They are translated to stm and ldm respectively at assembly time, the suffix depending on the stack type (full descending being the de-factor standard) : 它们在组装时分别转换为stm和ldm,后缀取决于堆栈类型(全降是分解标准):

  • full descending : stmdb, ldmia 全降序:stmdb,ldmia
  • full ascending : stmib, ldmda 完全升序:stmib,ldmda
  • empty descending : stmda, ldmib 空降序:stmda,ldmib
  • empty ascending : stmia, ldmdb 空升序:stmia,ldmdb

And a very simple rule applies : lower numbered registers load/store the value from/to the lower addresses. 有一个非常简单的规则:编号较低的寄存器从/向低地址加载/存储值。

It's impossible to alter the way it works. 改变其工作方式是不可能的。 If you want to push the values in a different order than the rule says above, either sort prior to pushing, or push them separately in an order you see it fit like : 如果您要按与上述规则不同的顺序推送值,请在推送之前进行排序,或者按照您认为合适的顺序分别推送它们:

push {r6}
push {r5}

Honestly, I don't see any good reason for doing this. 老实说,我认为这样做没有任何充分的理由。 If you absolutely need this kind of "sorting", ask yourself first whether you aren't doing something wrong. 如果您绝对需要这种“分类”,请先问自己是否做错了什么。

  asm("MOVS R5, #10");
  asm("MOVS R6, #20");

  asm("PUSH {R5,R6}");
  asm("POP {R6}");
  asm("POP {R5}");

I have fixed the problem now, I was completely new to this, and the answer turns out to be very straight forward. 现在,我已经解决了这个问题,对此我是一个全新的人,答案很简单。 Thanks everyone for their time. 感谢大家的宝贵时间。

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

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