微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ARM 汇编的mov操作立即数的疑问

ARM 汇编的mov操作立即数的疑问

时间:11-09 来源:互联网 点击:
1. 因为对arm汇编有些指令还不能理解,特别是一些相似功能指令间的区别。偶然在网上搜到“faq ARM assembly”,其中描述的几个问题还是值得好好研究一下。

2. 慢慢的发现自己也不再害怕英文的文档了,耐心看至少也能懂个大概。大批经典的文章和书籍都是en文的,所以经常看英文文档是一个非常好的习惯。看看GNU的一些reference manual,哪个不是经典而又值得学习并研究的!

3. 学习别人写文档的风格,重点要注意条理性。能够把一个问题、一个知识点阐述清晰明白,这不仅需要对知识点的掌握,还需要良好的语言表达能力以及对文章细心、整洁的排版。我想,这些细节才能够体现一个人的水平和他所能到达的高度。

--
本篇来看一下mov这个指令,相关指令 ldr

Question on MOV
Does the instruction “Mov” have indirect addressing?
Answer. No, e.g. you cannot use mov r1,[r2]
“MOV loads a value into the destination register, from another register, a shifted register, or an immediate 8-bit value.”
Examples:
MOV R0, R1 if R1 has 0x01234, after this ,R1=R2=0x01234
MOV R0, #0x12; after this R0 has #0x12
MOV R0, #300; is wrong the value should be less than 255
MOV R0, 200; is wrong, # is missing “mov R0,#200: is correct

Note: the immediate value must be prefixed by #

从上面的描述可以看出,mov的作用有两个:

1. 在寄存器之间传递值。

2. 给寄存器传递一个立即数,此时需要用“#”来修饰立即数,并且立即数为8位的,其值不能超过255.

但是在vivi中的head.S里,有许多类似 mov r1, #0x53 的语句:

简单的写几句汇编,测试一下:

.global _start
.align 0
_start:
mov r1, #0x12
mov r2, #300
mov r3, #0x53

.end

这样,编译并没有报错。

反汇编后如下

8: e3a01012 mov r1, #18 ; 0x12
8004: e3a02f4b mov r2, #300 ; 0x12c
8008: e3a03453 mov r3, #1392508928 ; 0x53

这是为什么呢?为什么用mov也可以?看别的汇编里都是写ldr r1, =0x53

将程序改一下:

.global _start
.align 0
_start:
mov r1, #0x12
mov r2, #300
ldr r3, =0x53
.end

汇编也没有报错,反汇编可看到如下三句话:

8: e3a01012 mov r1, #18 ; 0x12
8004: e3a02f4b mov r2, #300 ; 0x12c
8008: e3a03453 mov r3, #1392508928 ; 0x53

发现,ldr r3, =0x53 被经过汇编之后,实际上变为了 mov r3, #0x53 。可见,此处ldr相当于一个伪指令。

关于ldr,下面这段说的很清楚。在分析汇编时,很重要一点就是分清“地址” 和“地址里的内容”,对这个能熟练把握了,相信能对C语言中的指针有更深的理解。

Question on the use of LDR,ADR
Are there any difference between statement 1,2,3 in the following program?
Data1p DCD 0, 0 ;just happen the address is at 0x40
;DCD (reserve a 32-bit word)is a pseudo instruction to
;allocate a memory location for this data.
align
align
:
:
Statment1 LDR R0, =Data1p ; Put the address Data1p into R0
Statment2 ADR R0, Data1p ; Put the address Data1p into R0
Statment3 LDR R0, =0x40 ; Put the value0x40 into R0,
;just happen it is the address of Data1p
Answer: They are all the same. Every statement will generate the same result. Such that the address, not the data content, will be saved into R0. For example, the value in R0 is 0x40.

到这里,相信一定对“mov”和“ldr”两个产生了一些疑惑。(注:此处的ldr指的是伪指令pseudo-instruction,而不是内存到寄存器传递值的instruction)

下面的这段对两者的区别作了很全面的分析。

Question on differences between LDR and MOV
What is the difference between MOV and LDR, they seem to have the same function of saving information into registers?
Answer part 1: How different are they?
Note: “#” for mov, “=” for ldr. To define an immediate value
MOV can only move an 8-bit value (0x00->0xff=255) into a register while LDR can move a 32-bit value into a register. The immediate value is prefixed by different characters in mov and ldr: “#” formov, “=” for ldr. E.g.

Mov r1,#255 ; ok, 255 is the biggest number you can mov
Mov r1,255 ; is wrong , missing #
Mov r1,#256 ; is wrong, the number is bigger than 255
Mov r1,#0x12340 ; is wrong, the number is bigger than 255
Ldr r1,=255; you can do this,
Ldr r1,=256; you can do this,
Ldr r1,=0x12340; you can do this,

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top