交互操作示例

来源:互联网 发布:Windows 10 隐藏 文件 编辑:程序博客网 时间:2024/06/16 08:57

交互操作示例

以下是交互操作的示例:

  • Example 5.2 演示汇编语言交互操作

  • Example 5.3 演示使用中间代码的汇编语言交互操作

  • Example 5.4 演示 C 和 C++ 语言交互操作

  • Example 5.5 演示使用中间代码的 C、C++ 和汇编语言交互操作。

此外,RealView Development Suite 也附带了一些交互操作示例。 有关详细信息,请参阅 install_directory/RVDS/Examples/.../interwork 中的 readme.txt 文件。

Example 5.2. 汇编语言交互操作

此示例实现一小段后跟 ADR 指令的头文件节 (SECTION 1),以获取标签 THUMBProg 的地址,并设置该地址的最低有效位。 BX 指令将状态更改为 Thumb 状态。

SECTION2 中,Thumb 代码将两个寄存器的内容相加,使用 ADR 指令获取标签 ARMProg 的地址,并清除最低有效位。 BX 指令将状态改回 ARM 状态。

SECTION3 中,ARM 代码将两个寄存器中的值相加,然后结束。

     ; ********     ; addreg.s     ; ********     PRESERVE8     AREA     AddReg,CODE,READONLY  ; Name this block of code.     ENTRY                          ; Mark first instruction to call.; SECTION1start     ADR R0, ThumbProg:OR:1         ; Generate branch target address                                    ; and set bit 0, hence arrive                                    ; at target in Thumb state.     BX  R0                         ; Branch exchange to ThumbProg.; SECTION2     THUMB                          ; Subsequent instructions are Thumb code.ThumbProg     MOVS R2, #2                    ; Load R2 with value 2.     MOVS R3, #3                    ; Load R3 with value 3.     ADDS R2, R2, R3                ; R2 = R2 + R3     ADR R0, ARMProg     BX  R0                         ; Branch exchange to ARMProg.; SECTION3     ARM                            ; Subsequent instructions are ARM code.ARMProg     MOV R4, #4     MOV R5, #5     ADD R4, R4, R5; SECTION 4stop MOV R0, #0x18                  ; angel_SWIreason_ReportException     LDR R1, =0x20026               ; ADP_Stopped_ApplicationExit     SVC 0x123456                   ; ARM semihosting     END                            ; Mark end of this file.

按照以下步骤编译并链接模块:

  1. 若要汇编源文件以进行交互操作,请键入:

    armasm --debug --apcs=/interwork addreg.s
  2. 若要链接对象文件,请键入:

    armlink addreg.o -o addreg.axf

    或者,若要查看交互操作中间代码的大小,请键入:

    armlink addreg.o -o addreg.axf --info=veneers
  3. 通过对相应调试目标使用兼容调试器来运行映像。


Example 5.3. 使用中间代码的汇编语言交互操作

此示例演示汇编代码中的源代码交互操作,其功能是将寄存器 R0R2 分别设置为值 1、2 和 3。 寄存器 R0R2 由 ARM 代码设置。R1 由 Thumb 代码设置。 链接器会自动添加交互操作中间代码。 使用中间代码:

  • 必须用 --apcs=/interwork 选项来汇编代码

  • 使用 BX lr 指令返回,而不是 MOV pc,lr

     ; *****     ; arm.s     ; *****     PRESERVE8     AREA     Arm,CODE,READONLY   ; Name this block of code.     IMPORT   ThumbProg     ENTRY                        ; Mark 1st instruction to call.ARMProg     MOV  R0,#1                   ; Set R0 to show in ARM code.     BL   ThumbProg               ; Call Thumb subroutine.     MOV R2,#3                    ; Set R2 to show returned to ARM.                                  ; Terminate execution.     MOV  R0, #0x18               ; angel_SWIreason_ReportException     LDR  R1, =0x20026            ; ADP_Stopped_ApplicationExit     SVC 0x123456                 ; ARM semihosting (formerly SWI)     END     ; *******     ; thumb.s     ; *******     AREA  Thumb,CODE,READONLY    ; Name this block of code.     THUMB                        ; Subsequent instructions are Thumb.     EXPORT ThumbProgThumbProg     MOVS  R1, #2                 ; Set R1 to show reached Thumb code.     BX   lr                      ; Return to the ARM function.     END                          ; Mark end of this file.

按照以下步骤编译并链接模块:

  1. 若要汇编用于交互操作的 ARM 代码,请键入:

    armasm --debug --apcs=/interwork arm.s
  2. 若要汇编用于交互操作的 Thumb 代码,请键入:

    armasm --thumb --debug --apcs=/interwork thumb.s
  3. 若要链接对象文件,请键入:

    armlink arm.o thumb.o -o count.axf

    或者,若要查看交互操作中间代码的大小,请键入:

    armlink arm.o thumb.o -o count.axf --info=veneers
  4. 通过对相应调试目标使用兼容调试器来运行映像。


Example 5.4. C 和 C++ 语言交互操作

此示例演示一个 Thumb 例程,它执行对 ARM 子例程的交互操作调用。 该 ARM 子例程对 Thumb 库中的 printf() 进行交互操作调用。

     /*********************     *       thumbmain.c  *     **********************/     #include <stdio.h>     extern void arm_function(void);     int main(void)     {          printf("Hello from Thumb/n");          arm_function();          printf("And goodbye from Thumb/n");          return (0);     }     /*********************     *        armsub.c    *     **********************/     #include <stdio.h>     void arm_function(void)     {          printf("Hello and Goodbye from ARM/n");     }

按照以下步骤编译并链接模块:

  1. 若要编译用于交互操作的 Thumb 代码,请输入:

    armcc --thumb -c --debug --apcs=/interwork thumbmain.c -o thumbmain.o
  2. 若要编译用于交互操作的 ARM 代码,请输入:

    armcc -c --debug --apcs=/interwork armsub.c -o armsub.o
  3. 若要链接对象文件,请键入:

    armlink thumbmain.o armsub.o -o thumbtoarm.axf

    或者,若要查看交互操作中间代码的大小,请键入:

    armlink armsub.o thumbmain.o -o thumbtoarm.axf --info=veneers
  4. 通过对相应调试目标使用兼容调试器来运行映像。


Example 5.5. 使用中间代码的 C、C++ 和汇编语言交互操作

此示例演示 C 语言 Thumb 代码与汇编语言 ARM 代码之间的交互操作。

     /**********************     *       thumb.c      *     **********************/     #include <stdio.h>     extern int arm_function(int);     int main(void)     {          int i = 1;          printf("i = %d/n", i);          printf("And i+4 = %d/n", arm_function(i));          return (0);     }     ; *****     ; arm.s     ; *****     PRESERVE8     AREA  Arm,CODE,READONLY ; Name this block of code.     EXPORT arm_functionarm_function     ADD   R0,R0,#4           ; Add 4 to first parameter.     BX    lr                 ; Return     END

按照以下步骤编译并链接模块:

  1. 若要编译用于交互操作的 Thumb 代码,请输入:

    armcc --thumb --debug -c --apcs=/interwork thumb.c
  2. 若要汇编用于交互操作的 ARM 代码,请键入:

    armasm --debug --apcs=/interwork arm.s
  3. 若要链接对象文件,请键入:

    armlink arm.o thumb.o -o add.axf

    或者,若要查看交互操作中间代码的大小,请键入:

    armlink arm.o thumb.o -o add.axf --info=veneers
  4. 通过对相应调试目标使用兼容调试器来运行映像。

原创粉丝点击