Embedded Assembly Experiment 3

Embedded Assembly Experiment 3 Environment: ADS Software: Metrowerks CodeWarrior for ARM Developer Suite v1.2 Note: the ...
1, Swap string
2, Sort
3, Accessing global variables
4, Function call
Embedded Assembly Experiment 3

Environment: ADS
Software: Metrowerks CodeWarrior for ARM Developer Suite v1.2
Note: the name of the assembly file. s cannot be the same as the name of the c file. c, because they need to generate their own. o files.

1, Swap string

Requirements: C language and assembly language mixed programming implementation: in the C language program called assembly language code, to complete the string STR1 and STR2 content exchange (assuming that the length of STR1 and STR2 is the same).

C language:

# include <stdio.h> extern void swap(char *d,char *s); int main() { char *str1 = "AAAAA"; char *str2 = "BBBBB"; printf("Before swaping:\n"); printf("%s\n%s\n", str1, str2); swap(str1, str2); printf("After swaping:\n"); printf("%s\n%s\n", str1, str2); return(0); }

Assembly language:

AREA Swap,CODE,READONLY EXPORT swap swap LDRB R2,[R0] LDRB R3,[R1] STRB R2,[R1] STRB R3,[R0] ADD R0,R0,#1 ADD R1,R1,#1 CMP R2,#0 BNE swap MOV PC,LR END

2, Sort

Requirements: C language and assembly language mixed programming implementation: in the C language program call assembly language code, the input of 20 byte data, from large to small sorting, large numbers in front of the decimal in the back.

C language:

# include <stdio.h> extern void rank(char *s); int main() { char *str = "ABCDEFGHIJKLMNOPQRST"; printf("Before ranking:\n"); printf("%s\n", str); rank(str); printf("After ranking:\n"); printf("%s\n", str); return(0); }

Assembly language:

AREA Rank,CODE,READONLY EXPORT rank rank ; Bubble sorting MOV R1,#nineteen ; R1: outer cycle times loop1 ; External circulation MOV R0,R4 MOV R2,#nineteen ; R2: inner circulation times loop2 ; Internal circulation LDRB R3,[R0],#1 LDRB R5,[R0] MOV R6,PC ; preservation PC address ADD R6,R6,#8 CMP R3,R5 BLO swap SUB R2,R2,#1 CMP R2,#0 BNE loop2 SUB R1,R1,#1 CMP R1,#0 BNE loop1 MOV PC,LR swap MOV R7,R3 MOV R3,R5 MOV R5,R7 STRB R3,[R0,#-1] STRB R5,[R0] MOV PC,R6 END

Assuming that the address of MOV R6 and PC is 0x00008000, after executing the statement, the value of R6 becomes 0x00008008 and the value of PC becomes 0x00008004, that is, when passing the PC value with MOV instruction, the value of PC will be passed after + 8.

When calling the assembler, the C program selects R0~R3 to transmit parameters, and R4~R7 is used as the initial address of R0~R3 to ensure that the value of R0~R3 can return normally after changing. Therefore, R0 is used to transmit the string in this problem. The initial address of R0 recorded by R4 cannot be modified, so R4 is not used.

3, Accessing global variables

Requirements: mixed programming with C language and assembly language: access to C language global variables with assembly language. Assuming that CVAR1 and CVAR2 are global variables defined in C language, please access them with an assembly language to complete the addition operation of the two, and the results are stored in CVAR1.

C language:

# include <stdio.h> extern void test3(); unsigned int CVAR1 = 1; unsigned int CVAR2 = 5; int main() { printf("Before adding:\n"); printf("%d\n", CVAR1); test3(); printf("After adding:\n"); printf("%d\n", CVAR1); return(0); }

Assembly language:

AREA Test3,CODE,READONLY EXPORT test3 IMPORT CVAR1 IMPORT CVAR2 test3 LDR R0,=CVAR1 LDR R1,=CVAR2 LDR R2,[R0] LDR R3,[R1] ADD R2,R2,R3 STR R2,[R0] MOV PC,LR END

4, Function call

Requirements: mixed programming with C language and assembly language: use assembly language program to call C language subroutine to find the sum of i+ 2i + 3i +4i + 5i +6*i (set i as an integer constant).

C language:

# include <stdio.h> extern int g(int a, int b, int c, int d, int e, int f) { return a+b+c+d+e+f; }

Assembly language:

AREA f,CODE,READONLY EXPORT f IMPORT g ENTRY start MOV R0,#one ; Value of i ADD R1,R0,R0 ;calculation i*2 ADD R2,R1,R0 ;calculation i*3 ADD R3,R1,R2 ;calculation i*5 STR R3,[SP,#-4]! ADD R3,R2,R2 ;calculation i*6 STR R3,[SP,#-4]! ADD R3,R1,R1 ;calculation i*4 BL g END

In this problem, six parameters need to be passed. R0~R3 are not enough. Therefore, press the extra i * 5 and i * 6 into the stack. Suppose that the initial value of SP stack pointer is 0x00000000. After pressing i * 5, the value of address 0xFFFFFFFC is i * 5 and the value of SP is 0xFFFFFFFC. Then press i * 6, the value of SP becomes 0xFFFFFFF8 and the value of address is i * 6

When calling the function, R0~R3 are respectively passed into a~d variables. The parameters of the stack start from the small address, first pass i * 6 in address 0xfffff8 into variable D, and then pass i * 5 in address 0xfffffc into variable e

29 October 2021, 05:16 | Views: 1549

Add new comment

For adding a comment, please log in
or create account

0 comments