零、前言
本人不擅长写汇编相关的东西,所以以下内容也是不断摸索(百度 ) 整出来的,和linux的实验报告的质量相比较低。
一、 实验目的
二、 实验设备
三、 实验任务与要求
一定要确认自己使用的 keil 版本,必须是 keil3 for arm
** keil 编辑时可能会出现1**
;3.1_asm1\Asm1_a 中的 asm1_a.s 文件
x EQU 45
y EQU 64
stack_top EQU 0x30200000
export Reset_HandlerAREA text,CODE,READONLYexport
Reset_Handlerldr sp, =stack_topmov r0, #xstr r0, [sp] mov r0, #yldr r1, [sp]add r0, r0, r1str r0, [sp]
stopb stopend
(1) 请按照嵌入式程序调试步骤进行单步执行,并将每一步执行的寄存器值及内存中的结果截图。
asm_1a:
常量定义,类似于C语言中的 #define
x EQU 45
意味着之后程序中 出现的 x
将会在预处理阶段被替换为 45
y EQU 64
意味着之后程序中 出现的 y
将会在预处理阶段被替换为 64
stack_top EQU 0x30200000
意味着之后程序中 出现的 stack_top
将会在预处理阶段被替换为 0x30200000
export Reset_Handler
表示 Reset_Handler 这个子程序可供其他模块调用。
定义一个代码段text
,CODE
代表它要定义的是一个代码段,READONLY
指定本段为只读,(代码段默认为READONLY)
此处参考来源:https://www.cnblogs.com/qiyuexin/p/12608776.html
此处代码可以debug
step 0:
此时程序走在第一步(还未执行),上图所示都是初始状态下的情况。
step 1:ldr sp, =stack_top
step 2:mov r0, #x
把x的值给 R0
step 3:str r0, [sp]
将 R0 的值保存到栈中
step 4:mov r0, #y
把y的值给 R0
step 5:ldr r1, [sp]
从堆栈中读取数据,并将其放入 R1
step 6:add r0, r0, r1
把 R0 和 R1 的值加起来
step 7:str r0, [sp]
step 8:b stop
代码结束
asm_1b
常量定义,类似于C语言中的 #define
x EQU 45
意味着之后程序中 出现的 x
将会在预处理阶段被替换为 45
y EQU 64
意味着之后程序中 出现的 y
将会在预处理阶段被替换为 64
z EQU 87
意味着之后程序中 出现的 z
将会在预处理阶段被替换为 87
stack_top EQU 0x30200000
意味着之后程序中 出现的 stack_top
将会在预处理阶段被替换为 0x30200000
export Reset_Handler
表示 Reset_Handler 这个子程序可供其他模块调用。
定义一个代码段text
,CODE
代表它要定义的是一个代码段,READONLY
指定本段为只读,(代码段默认为READONLY)
此处参考来源:https://www.cnblogs.com/qiyuexin/p/12608776.html
此处代码可以debug
step 0:
step 1:mov r0, #x
把x的值放到 r0
step 2: mov r0, r0, lsl #8
x = x << 8 左移八位
step 3: mov r1, #y
把y的值放入 r1 中
step 4: add r2, r0, r1, lsr #1
R2 = (R1>>1) + R0
step 5: ldr sp, =stack_top
step 6: str r2, [sp]
step 7: mov r0, #z
把z的值放到 r0
step 8: and r0, r0, #0xFF
从 r0 中获得低八位
step 9: mov r1, #y
y的值放入r1 中
step 10: add r2, r0, r1, lsr #1
R2 =(R1>>1)+ R0
step 11: ldr r0, [sp]
step 12: mov r1, #0x01
step 13:orr r0, r0, r1
step 14:mov r1, R2
把y的值放入r1 中
step 15: add r2, r0, r1, lsr #1
R2 =(R1>>1) + R0
step 16: b stop
结束代码
(2) 在上面的代码基础上进行实验的拓展,加入ADD、SUB、AND、ORR等指令,并加上寄存器移位寻址方式。
(这里我仅在 asm_1a.s文件里进行了拓展)
; ADD
ldr r0, =0x20
ldr r1, =0x30
add r2, r0, r1; SUB
ldr r0, =0x3f
ldr r1, =0x1d
sub r2, r0, r1; AND
MOV R0,#0x02
AND R0,#0x01; ORR
MOV R0,#0x01
ORR R0,R0,#0x0F;寄存器移位寻址
;LSL 逻辑左移
MOV R1, #0x02
MOV R2, #0x03
ADD R0,R1,R2,LSL #2
更改完毕之后,点击此处重新Build
然后在下方的控制台查看是否build成功
这时候就可以 Debug -> Start Debug 然后挨个执行指令了。
(3) 在上面的代码基础上进行LDR/STR指令对内存随机访问的指令,需要在程序中利用数据定义伪操作定义字数据并分配内存。
缩进很重要!缩进不对无法编译
;newldr R0, =data1ldr R1, =data2ldr R3, [R0]str r4, [R1]LTORG
data1DCD 0x123
data2DCD 0
(4) LDR/STR指令访问内存时分别采用寄存器间接寻址方式、基址变址寻址方式中的前索引偏移和后索引偏移进行设计,并将结果截图。
前索引偏移:
AREA text,CODE,READONLYReset_Handlerldr r4,=0x3cldr r0,[r4,#8]mov r6,#2ldr r0,[r4, r6]ldr r0, [r4,r6,LSL #2]stopb stop END
后索引偏移:
AREA text,CODE,READONLYReset_Handlerldr r2, =0x68str r1, [r2], #4str r3, [r2], #8stopb stop END
(5) 编写程序循环对 R4~R11 进行累加 8 次赋值,R4~R11 起始值为 1~8,每次加操作后把 R4~R11 的内容放入 SP 栈中。
stack_top EQU 0x00800000export Reset_HandlerAREA text,CODE,READONLYReset_Handlermov sp,#stack_topmov r4,#1mov r5,#2mov r6,#3mov r7,#4mov r8,#5mov r9,#5mov r10,#7mov r11,#8stmfd sp!,{r4-r11}mov r3,#0stopb stop END
以下是 ChatGPT给出的解释(一种AI程序):
asm_code1.s:
asm_code2.s:
asm_code1.s:
GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* 设置要复制的字数 */Reset_Handlerldr r0, =src ;/* R0 指向源数据块首地址 */ldr r1, =dst ;/* R1 指向目标数据块首地址 */mov r2, #num ;/* R2 复制的字数 */ldr sp, =0x30200000 ;/* 设置堆栈指针,用于保存工作寄存器数值 */
blockcopy movs r3,r2, LSR #3 ;/* 需要以8 个字为单位的数据复制 */beq copywords ;/* 如果移动的字的个数小于 8 则跳转到 copywords _子函数 */stmfd sp!, {r4-r11} ;/* 保存工作寄存器 */
octcopyldmia r0!, {r4-r11} ;/* 从数据源取出 8 个字数据放到寄存器,更新 RO */stmia r1!, {r4-r11} ;/*把数据写入目的地址,更新 R1 */subs r3, r3, #1 ;/* 将块复制制次数减 1 */bne octcopy ;/* 循环直到完成八个字为单位的人复制 */ldmfd sp!, {r4-r11} ;/* 恢复工作寄存器 */copywordsands r2, r2, #7 ;/*剩余不足 8 个字数据的字数 */beq stop ;/* 数据复制完成则跳转到 stop */
wordcopyldr r3, [r0], #4 ;/* 从数据源取出一个字数据到 R3,且更新 R3 */str r3, [r1], #4 ;/* 将字数据存储到目标地址,更新 R1 */subs r2, r2, #1 ;/* 字复制次数减 1 */bne wordcopy ;/* 循环到复制结束 */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
asm_code2.s
area start,code,readwriteentrycode32
num equ 2 export Reset_Handler
Reset_Handler;/* 设置三个参数 */mov r0, #0 ;/* 把 0 传送到 R0 寄存器中 */mov r1, #3 ;/* 把 3 传送到 R1 寄存器中 */mov r2, #2 ;/* 把 2 传送到 R2 寄存器中 */bl arithfunc ;/* 调用函数 */stopb stop;# ********************************************************************************************
;# * According R0 valude to execute the code *
;# ********************************************************************************************
arithfunc ;/* 标识函数 */cmp r0, #num ;/* 将函数代码视为无符号整数,R0 与立即数 NUM 进行比较 */bhs DoAdd ;/* 如果大于等于 2,则执行操作 0 */adr r3, JumpTable ;/* 根据参数 R0 的值跳转到对应的子程序 */ldr pc, [r3,r0,LSL#2] ;/* 跳转到适当的路线 */JumpTabledcd DoAdd ;/*占领四个字节,其中存储的是 DoAdd 对应的地址 */dcd DoSub ;/* 占领四个字节,其中存储的是 DoSub.对应的地址 */DoAddadd r0, r1, r2 ;/* R1+R2->R0 */bx lr ;/* 跳转到 lr 中存放的地址处*/DoSubsub r0, r1, r2 ;/* R0-R1->R3 */bx lr ;/* 跳转到 lr 中存放的地址处 */end ;/* 结束程序 */
(2) 将运行结果的寄存器值及内存中的查看结果截图;
asm_code1.s:
asm_code2.s:
(3) 将asm_code1.s代码中的进栈、出栈指令STMIA/LDMIA分别改成STMIB/LDMIB、STMDA/LDMDA、STMDB/LDMDB,并将运行结果截图。
提示:这里的改成就是一种简单的文字替换,用CTRL+H逐个替换即可
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* Set number of words to be copied */Reset_Handlerldr r0, =src ;/* r0 = pointer to source block */ldr r1, =dst ;/* r1 = pointer to destination block */mov r2, #num ;/* r2 = number of words to copy */ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy movs r3,r2, LSR #3 ;/* number of eight word multiples */beq copywords ;/* less than eight words to move ? */stmfd sp!, {r4-r11} ;/* save some working registers */
octcopyLDMIB r0!, {r4-r11} ;/* load 8 words from the source */STMIB r1!, {r4-r11} ;/* and put them at the destination */subs r3, r3, #1 ;/* decrement the counter */bne octcopy ;/* ... copy more */ldmfd sp!, {r4-r11} ;/* don't need these now - restore originals */copywordsands r2, r2, #7 ;/* number of odd words to copy */beq stop ;/* No words left to copy ? */
wordcopyldr r3, [r0], #4 ;/* a word from the source */str r3, [r1], #4 ;/* store a word to the destination */subs r2, r2, #1 ;/* decrement the counter */bne wordcopy ;/* ... copy more */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* Set number of words to be copied */Reset_Handlerldr r0, =src ;/* r0 = pointer to source block */ldr r1, =dst ;/* r1 = pointer to destination block */mov r2, #num ;/* r2 = number of words to copy */ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy movs r3,r2, LSR #3 ;/* number of eight word multiples */beq copywords ;/* less than eight words to move ? */stmfd sp!, {r4-r11} ;/* save some working registers */
octcopyLDMDA r0!, {r4-r11} ;/* load 8 words from the source */STMDA r1!, {r4-r11} ;/* and put them at the destination */subs r3, r3, #1 ;/* decrement the counter */bne octcopy ;/* ... copy more */ldmfd sp!, {r4-r11} ;/* don't need these now - restore originals */copywordsands r2, r2, #7 ;/* number of odd words to copy */beq stop ;/* No words left to copy ? */
wordcopyldr r3, [r0], #4 ;/* a word from the source */str r3, [r1], #4 ;/* store a word to the destination */subs r2, r2, #1 ;/* decrement the counter */bne wordcopy ;/* ... copy more */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* Set number of words to be copied */Reset_Handlerldr r0, =src ;/* r0 = pointer to source block */ldr r1, =dst ;/* r1 = pointer to destination block */mov r2, #num ;/* r2 = number of words to copy */ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy movs r3,r2, LSR #3 ;/* number of eight word multiples */beq copywords ;/* less than eight words to move ? */stmfd sp!, {r4-r11} ;/* save some working registers */
octcopyldmdb r0!, {r4-r11} ;/* load 8 words from the source */stmdb r1!, {r4-r11} ;/* and put them at the destination */subs r3, r3, #1 ;/* decrement the counter */bne octcopy ;/* ... copy more */ldmfd sp!, {r4-r11} ;/* don't need these now - restore originals */copywordsands r2, r2, #7 ;/* number of odd words to copy */beq stop ;/* No words left to copy ? */
wordcopyldr r3, [r0], #4 ;/* a word from the source */str r3, [r1], #4 ;/* store a word to the destination */subs r2, r2, #1 ;/* decrement the counter */bne wordcopy ;/* ... copy more */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
(4) 将asm_code1.s代码中的进栈、出栈指令STMFD/LDMFD分别改成STMFA/LDMFA、STMEA/LDMEA、STMED/LDMED,并将运行结果截图。
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* Set number of words to be copied */Reset_Handlerldr r0, =src ;/* r0 = pointer to source block */ldr r1, =dst ;/* r1 = pointer to destination block */mov r2, #num ;/* r2 = number of words to copy */ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy movs r3,r2, LSR #3 ;/* number of eight word multiples */beq copywords ;/* less than eight words to move ? */stmfa sp!, {r4-r11} ;/* save some working registers */
octcopyldmia r0!, {r4-r11} ;/* load 8 words from the source */stmia r1!, {r4-r11} ;/* and put them at the destination */subs r3, r3, #1 ;/* decrement the counter */bne octcopy ;/* ... copy more */ldmfa sp!, {r4-r11} ;/* don't need these now - restore originals */copywordsands r2, r2, #7 ;/* number of odd words to copy */beq stop ;/* No words left to copy ? */
wordcopyldr r3, [r0], #4 ;/* a word from the source */str r3, [r1], #4 ;/* store a word to the destination */subs r2, r2, #1 ;/* decrement the counter */bne wordcopy ;/* ... copy more */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* Set number of words to be copied */Reset_Handlerldr r0, =src ;/* r0 = pointer to source block */ldr r1, =dst ;/* r1 = pointer to destination block */mov r2, #num ;/* r2 = number of words to copy */ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy movs r3,r2, LSR #3 ;/* number of eight word multiples */beq copywords ;/* less than eight words to move ? */stmea sp!, {r4-r11} ;/* save some working registers */
octcopyldmia r0!, {r4-r11} ;/* load 8 words from the source */stmia r1!, {r4-r11} ;/* and put them at the destination */subs r3, r3, #1 ;/* decrement the counter */bne octcopy ;/* ... copy more */ldmea sp!, {r4-r11} ;/* don't need these now - restore originals */copywordsands r2, r2, #7 ;/* number of odd words to copy */beq stop ;/* No words left to copy ? */
wordcopyldr r3, [r0], #4 ;/* a word from the source */str r3, [r1], #4 ;/* store a word to the destination */subs r2, r2, #1 ;/* decrement the counter */bne wordcopy ;/* ... copy more */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/GLOBAL Reset_Handlerarea start,code,readwriteentrycode32
num EQU 20 ;/* Set number of words to be copied */Reset_Handlerldr r0, =src ;/* r0 = pointer to source block */ldr r1, =dst ;/* r1 = pointer to destination block */mov r2, #num ;/* r2 = number of words to copy */ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy movs r3,r2, LSR #3 ;/* number of eight word multiples */beq copywords ;/* less than eight words to move ? */stmed sp!, {r4-r11} ;/* save some working registers */
octcopyldmia r0!, {r4-r11} ;/* load 8 words from the source */stmia r1!, {r4-r11} ;/* and put them at the destination */subs r3, r3, #1 ;/* decrement the counter */bne octcopy ;/* ... copy more */ldmed sp!, {r4-r11} ;/* don't need these now - restore originals */copywordsands r2, r2, #7 ;/* number of odd words to copy */beq stop ;/* No words left to copy ? */
wordcopyldr r3, [r0], #4 ;/* a word from the source */str r3, [r1], #4 ;/* store a word to the destination */subs r2, r2, #1 ;/* decrement the counter */bne wordcopy ;/* ... copy more */stopb stop;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/ltorg
srcdcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstdcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0end
;EduKit2410_for_MDK\3.4_armmode中的armmode.s代码
mrs r0,cpsr bic r0,r0,#0x1f ;/* */orr r0,r0,#0x11 msr cpsr_cxfs,r0mov r8, #16 mov r9, #17 mov r10, #18 mov r11, #19 mov r12, #20 mov r13, #21 mov r14, #22
(1) 请解释上述模式切换指令;
很好,我选择求助 ChatGPT。
以下是ChatGPT(一种AI程序)给出的解释:
(2) 将运行结果及寄存器值截图;
(3) 将CPSR的格式写出来,并将程序运行的CPSR值截图。