assembly 如何通过汇编在Cortex-M3上获得数组中的索引值?

2exbekwf  于 2022-11-30  发布在  其他
关注(0)|答案(3)|浏览(150)

在Cortex-M3上,我想将数组的前两个值设置为0和1,所以我执行了以下操作:

main    MOV R4, #0

array   DCD 4,7,6,8
        LDR R1, =array ; R1 = base address of array

        STR R4, [R1, R4, LSL #2]
        ADD R4, R4, #1
        STR R4, [R1, R4, LSL #2]

        END

显然STR指令并没有达到我的预期。这有什么问题吗?在《Cortex-M3指令集》手册中写道:

STR R0, [R1, R2, LSL #2] ; Stores R0 to an address equal to sum of R1
; and four times R2.

反汇编如下所示:

0x00000000 0200      DCW      0x0200
0x00000002 2000      DCW      0x2000
0x00000004 0009      DCW      0x0009
0x00000006 0000      DCW      0x0000
0x00000008 F04F0400  MOV      r4,#0x00
0x0000000C 0004      DCW      0x0004
0x0000000E 0000      DCW      0x0000
0x00000010 0007      DCW      0x0007
0x00000012 0000      DCW      0x0000
0x00000014 0006      DCW      0x0006
0x00000016 0000      DCW      0x0000
0x00000018 0008      DCW      0x0008
0x0000001A 0000      DCW      0x0000
0x0000001C 4903      LDR      r1,[pc,#12]  ; @0x0000002C
0x0000001E F8414024  STR      r4,[r1,r4,LSL #2]
0x00000022 F1040401  ADD      r4,r4,#0x01
0x00000026 F8414024  STR      r4,[r1,r4,LSL #2]
0x0000002A 0000      DCW      0x0000
0x0000002C 000C      DCW      0x000C
0x0000002E 0000      DCW      0x0000
ryhaxcpt

ryhaxcpt1#

正如Michael所解释的,指令array DCD 4,7,6,8必须在AREA d, DATA指令之后-否则数组将被置于只读代码区,因此无法被覆盖。

ifsvaxew

ifsvaxew2#

保留8拇指区复位,数据,只读导出__矢量__矢量DCD 0x 20001000;堆栈指针初始化时在此处输入代码值

DCD Reset_Handler ;reset vector

        ALIGN

;AREA DATA

MAXP DCD最大值最小值DCD最小值
N双CD 12数字1双CD 3,-7,2,-2,10,20,30,15,32,8,64,66指针双CD数字1

AREA MYRAM, DATA, READWRITE

最大DCD 0最小DCD 0

AREA MYCODE,CODE, READONLY
ENTRY
EXPORT Reset_Handler

重设行程程式(_H)
主LDR R1,=指针;在R1 LDR R2中加载指针,[R1] ;从指针LDR R3获取数组地址,= MAX ;加载R3中的MAX指针LDR R6,[R3] ;从MAX指针LDR R7获取MAX的地址,= N ; R5 LDR R 0中的负载计数器地址,[R7] ;从R5 LDR R8中的计数器地址加载计数器值,= MIN ;在R8地址LDR R5中加载MIN指针,[R8] ;从最小指针LOOP LDR R4,[R2],#4加载最小值;从数组加载值并更新(递增)指针CMP R4、R6的地址;比较R6值与R3 IT GT ;如果大于,则执行下一指令MOVGT R6、R4 ;如果数组值大于R6中的值,则将R3中的R6值移到CMP R4、R5中;比较R6和R 0的最小值IT〈LT ;如果value小于最小值,则执行nex指令MOVLT R5、R4 ;更新R 0中最小数量的值

SUB R0,R0,#1       ;Decrement counter by one in every loop itration 
CMP R0, #0          ;Compare remaining counter value with zero
BNE LOOP            ;if value is not zero then jump to loop 

STR R5, [R8]         ;if counter become zero then store Min value at address MIN 
STR R6, [R3]         ;Store MAX value at address MAX

结束

mnowg1ta

mnowg1ta3#

PRESERVE8
                THUMB
                AREA RESET, DATA, READONLY
                EXPORT __Vectors
__Vectors
                DCD 0x20001000   ;stack pointer value when initilized
                
                DCD Reset_Handler ;reset vector
                    
                ALIGN
                

        ;AREA DATA

MAXP    DCD MAX
Min     DCD MIN

N       DCD 12
NUM1    DCD 3,-7,2,-2,10,20,30,15,32,8,64,66
POINTER DCD NUM1
    
        AREA MYRAM, DATA, READWRITE
MAX     DCD 0   
MIN     DCD 0   
    
        AREA MYCODE,CODE, READONLY
        ENTRY
        EXPORT Reset_Handler

Reset_Handler   

Main
        LDR R1 , = POINTER  ;Load Pointer in R1
        LDR R2 ,[R1]        ;get address of array from pointer
        LDR R3 , = MAX     ;Load MAX pointer in R3
        LDR R6, [R3]        ;Get address of MAX from MAX pointer 
        LDR R7 , = N        ;Load counter Adress in R5
        LDR R0,[R7]         ;Load Value of Counter from counter address which is in R5
        LDR R8, = MIN       ;Load MIN pointer in R8 Address
        LDR R5, [R8]        ;Load MIN value from MIN pointer   
LOOP    
        LDR R4 , [R2],#4    ;Loading value from array and updating(increment) the address of pointer    
        CMP R4, R6          ;Compare R6 value with R3
        IT GT               ;If greater then exectue next instruction
        MOVGT R6,R4         ;if Array value greater than than value in R6 then move R6 value in R3
        CMP R4, R5          ;compare R6 with R0 the minimum value 
        IT LT               ;if value is less than the minimum value then execute nex instruction 
        MOVLT R5,R4         ;updat value of minimum number in R0 
        
        SUB R0,R0,#1       ;Decrement counter by one in every loop itration 
        CMP R0, #0          ;Compare remaining counter value with zero
        BNE LOOP            ;if value is not zero then jump to loop 
        
        STR R5, [R8]         ;if counter become zero then store Min value at address MIN 
        STR R6, [R3]         ;Store MAX value at address MAX
        
    END

相关问题