The source code as below
#include <stdint.h> #include <stdio.h> typedef struct { uint8_t data1; uint8_t data2; uint8_t data3; uint8_t data4; uint8_t data5; } config_t; config_t gConfig[2] = {0}; uint8_t gIndex = 0; config_t* pConfig = 0; void func1(config_t* config) { config->data1 = 0; config->data2 = 100; } void func2(void) { gConfig[gIndex].data1 = 0; gConfig[gIndex].data2 = 100; } void func3(void) { pConfig->data1 = 0; pConfig->data2 = 100; } void func4(void) { gConfig[gIndex].data3 = 0; gConfig[gIndex].data4 = 100; } int main(void) { pConfig = &gConfig[gIndex]; func1(&gConfig[0]); func2(); func3(); return 0; }
code explore base on clang 13.0 arm v7
func1: sub sp, sp, #4 str r0, [sp] ldr r1, [sp] mov r0, #0 strb r0, [r1] ldr r1, [sp] mov r0, #100 strb r0, [r1, #1] add sp, sp, #4 bx lr func2: ldr r1, .LCPI1_0 .LPC1_0: add r1, pc, r1 ldrb r0, [r1] add r3, r0, r0, lsl #2 ldr r0, .LCPI1_1 .LPC1_1: add r0, pc, r0 mov r2, #0 strb r2, [r0, r3] ldrb r1, [r1] add r1, r1, r1, lsl #2 add r1, r0, r1 mov r0, #100 strb r0, [r1, #1] bx lr .LCPI1_0: .long gIndex-(.LPC1_0+8) .LCPI1_1: .long gConfig-(.LPC1_1+8) func3: ldr r1, .LCPI2_0 .LPC2_0: ldr r1, [pc, r1] mov r0, #0 strb r0, [r1] ldr r1, .LCPI2_1 .LPC2_1: ldr r1, [pc, r1] mov r0, #100 strb r0, [r1, #1] bx lr .LCPI2_0: .long pConfig-(.LPC2_0+8) .LCPI2_1: .long pConfig-(.LPC2_1+8) func4: ldr r1, .LCPI3_0 .LPC3_0: add r1, pc, r1 ldrb r0, [r1] add r2, r0, r0, lsl #2 ldr r0, .LCPI3_1 .LPC3_1: add r0, pc, r0 add r3, r0, r2 mov r2, #0 strb r2, [r3, #2] ldrb r1, [r1] add r1, r1, r1, lsl #2 add r1, r0, r1 mov r0, #100 strb r0, [r1, #3] bx lr .LCPI3_0: .long gIndex-(.LPC3_0+8) .LCPI3_1: .long gConfig-(.LPC3_1+8) main: push {r11, lr} mov r11, sp sub sp, sp, #8 mov r0, #0 str r0, [sp] @ 4-byte Spill str r0, [sp, #4] ldr r0, .LCPI4_0 .LPC4_0: add r0, pc, r0 ldrb r0, [r0] add r1, r0, r0, lsl #2 ldr r0, .LCPI4_1 .LPC4_1: add r0, pc, r0 add r1, r0, r1 ldr r2, .LCPI4_2 .LPC4_2: add r2, pc, r2 str r1, [r2] bl func1 bl func2 bl func3 ldr r0, [sp] @ 4-byte Reload mov sp, r11 pop {r11, lr} bx lr .LCPI4_0: .long gIndex-(.LPC4_0+8) .LCPI4_1: .long gConfig-(.LPC4_1+8) .LCPI4_2: .long pConfig-(.LPC4_2+8) gConfig: .zero 10 gIndex: .byte 0 @ 0x0 pConfig: .long 0
Conclusion : using function 1 method is more saving code size.
func1: sub sp, sp, #4 str r0, [sp] ldr r1, [sp] mov r0, #0 strb r0, [r1] ldr r1, [sp] mov r0, #100 strb r0, [r1, #1] add sp, sp, #4 bx lr func2: ldr r1, .LCPI1_0 .LPC1_0: add r1, pc, r1 ldrb r0, [r1] add r3, r0, r0, lsl #2 ldr r0, .LCPI1_1 .LPC1_1: add r0, pc, r0 mov r2, #0 strb r2, [r0, r3] ldrb r1, [r1] add r1, r1, r1, lsl #2 add r1, r0, r1 mov r0, #100 strb r0, [r1, #1] bx lr .LCPI1_0: .long gIndex-(.LPC1_0+8) .LCPI1_1: .long gConfig-(.LPC1_1+8) func3: ldr r1, .LCPI2_0 .LPC2_0: ldr r1, [pc, r1] mov r0, #0 strb r0, [r1] ldr r1, .LCPI2_1 .LPC2_1: ldr r1, [pc, r1] mov r0, #100 strb r0, [r1, #1] bx lr .LCPI2_0: .long pConfig-(.LPC2_0+8) .LCPI2_1: .long pConfig-(.LPC2_1+8) func4: ldr r1, .LCPI3_0 .LPC3_0: add r1, pc, r1 ldrb r0, [r1] add r2, r0, r0, lsl #2 ldr r0, .LCPI3_1 .LPC3_1: add r0, pc, r0 add r3, r0, r2 mov r2, #0 strb r2, [r3, #2] ldrb r1, [r1] add r1, r1, r1, lsl #2 add r1, r0, r1 mov r0, #100 strb r0, [r1, #3] bx lr .LCPI3_0: .long gIndex-(.LPC3_0+8) .LCPI3_1: .long gConfig-(.LPC3_1+8) main: push {r11, lr} mov r11, sp sub sp, sp, #8 mov r0, #0 str r0, [sp] @ 4-byte Spill str r0, [sp, #4] ldr r0, .LCPI4_0 .LPC4_0: add r0, pc, r0 ldrb r0, [r0] add r1, r0, r0, lsl #2 ldr r0, .LCPI4_1 .LPC4_1: add r0, pc, r0 add r1, r0, r1 ldr r2, .LCPI4_2 .LPC4_2: add r2, pc, r2 str r1, [r2] bl func1 bl func2 bl func3 ldr r0, [sp] @ 4-byte Reload mov sp, r11 pop {r11, lr} bx lr .LCPI4_0: .long gIndex-(.LPC4_0+8) .LCPI4_1: .long gConfig-(.LPC4_1+8) .LCPI4_2: .long pConfig-(.LPC4_2+8) gConfig: .zero 10
gIndex: .byte 0 @ 0x0
pConfig: .long 0 标签:comparison,r0,r1,sp,mov,Compile,ldr,add,result From: https://www.cnblogs.com/zjbfvfv/p/17520603.html