Linux · 2022年4月14日 0

ARM汇编实现两数相加

C代码

#include<stdio.h>
int main()
{
int a,b,sum;
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum=%d",sum);
}

ARM汇编程序

.LC0:
        .ascii  "%d%d\000"
        .align  2
.LC1:
        .ascii  "sum=%d\000"
        .align  2
.LC2:
        .word   __stack_chk_guard
        .text
        .align  2
        .global main
        .syntax unified
        .arm
        .fpu softvfp
        .type   main, %function
main:
        push    {fp, lr}
        add     fp, sp, #4
        sub     sp, sp, #16
        ldr     r3, .L4
        ldr     r3, [r3]
        str     r3, [fp, #-8]
        mov     r3,#0
        sub     r2, fp, #16
        sub     r3, fp, #20
        mov     r1, r3
        ldr     r0, .L4+4
        bl      __isoc99_scanf
        ldr     r2, [fp, #-20]
        ldr     r3, [fp, #-16]
        add     r3, r2, r3
        str     r3, [fp, #-12]
        ldr     r1, [fp, #-12]
        ldr     r0, .L4+8
        bl      printf
        mov     r3, #0
        ldr     r2, .L4
        ldr     r1, [r2]
        ldr     r2, [fp, #-8]
        eors    r1, r2, r1
        mov     r2, #0
        beq     .L3
        bl      __stack_chk_fail
.L3:
        mov     r0, r3
        sub     sp, fp, #4
        pop     {fp, pc}
.L5:
        .align  2
.L4:
        .word   .LC2
        .word   .LC0
        .word   .LC1