C and the ARM assembler calls to each other (5 summed number, string copy)

Development Environment: Keil5

Project directory:

 

 

First, the assembly call C-- adding most

  Use assembly call C, it should be noted that the transmission parameters. X86 is different from the parameter passing rules in the ARM architecture, if the parameter does not exceed the number 4, to the use of special registers R0 ~ R3 transmitted; if the parameter is greater than four, it is necessary that part of the extra parameters stack passing.

  After the push pointer movement position, the push sequence is not forget to push the last parameter -> penultimate parameter -> ,,,,

  The following code implements summation five parameters, firstly R0 = 0, R1 = 1, R2 = 2, R3 = 3, the fifth transmission parameter stack, and the stack pointer moves.

(1)startup.s

Stack_size EQU 0x100          ; initialize stack size is 256B 
    the AREA STACK, NOINIT, READWRITE, the ALIGN = . 3 
Stack_Mem the SPACE stack_size 
__initial_sp 

    PRESERVE8 The 
    THUMB 
        
    the AREA the RESET, the DATA, the READONLY ; interrupt vector table 
        the DCD __initial_sp 
        the DCD Reset_Handler 
    the AREA the RESET, the DATA, READWRITE 
    



    the Sum the AREA, CODE , the READONLY ; statement called a code segment Sum readable 
Reset_Handler the PROC ;           ; reset function begins execution here 
    IMPORT main              ; main function is not in this file, it is necessary to introduce 
    IMPORT sum5              ; sum5 function is not in this file, it is necessary to introduce 
    

CALLSUM5 
    MOVR0, # 0 
    MOV Rl, # . 1 
    MOV R2, # 2 
    MOV R3, # . 3 
    MOV R4, # . 4                 ; the first four parameters R0 ~ R3 transfer, 
    the STR R4, [the SP, # - . 4 ]!         ; 5th use stack transfer 
    BL sum5                  ; calling c procedures 
    the LDR R0, = main    
    BX R0                ; return to main function C 
    ENDP 
    the END

(2) main.c

int sum5(int a, int b, int c, int d, int e) {
    return (a+b+c+d+e);
}    
int main(void) {

    return 0;
}

 

Two, C calls an assembly --strcopy

  The logic is better understood, first of all define the C function to call in the assembly file, and then in the .c file extern this function, you can call.

(1)startup.s

    The AREA | .text |, CODE, the READONLY ; statement called | .text | code segments, readable     
Reset_Handler the PROC 
    IMPORT main                  ; main function is not in this file, it is necessary to introduce 
    the EXPORT Reset_Handler         ; declare external function 

    the LDR R0, = main 
    BX R0 
    the ALIGN 
    ENDP     
    
    
    
    the AREA of SCopy, CODE, the READONLY           
    the EXPORT strcopy                ; want to be called external, must declare function 
strcopy 
    LDRB R2, [R1], # 1               ; B represents a byte address # R1 represents a 
    STRB R2, [R0 ], # 1 
    CMP   R2, # 0                    ; 0 is the amount of marks the end of a string, comparing R2 and 0, you can see whether the string end
    Strcopy BNE                  ; jump instruction, to jump when unequal above, forming a loop 
    BX the LR                       ; return field 

    END    

(2) main.c

extern void strcopy(char *d, const char *s);

int main(void) {
    const char  srcstr[] = "ABCDEFGH";//0x41-0x48
          char  dststr[] = "abcdefgh";//0x61-0x68
    strcopy(dststr, srcstr);

    return 0;
}

Guess you like

Origin www.cnblogs.com/Irvingcode/p/12111301.html