#file:hello.c#include <stdio.h>#include <string.h>extern void printk(char);extern int max_p(int, int);char str[]="Hello world!\n";int main(){int i;for(i = 0; i < strlen(str); i++)printk(str[i]);int max = max_p(18, 200);printf("%d\n", max);printf("%d\n", max_p(10, 2));}#file: printk.s#include <stdio.h> .text .global printk .type printk, %function .global max_p.type max_p, %function .alignprintk:.fnstart stmfdsp!, {r0, r4-r7, lr} /*寄存器压栈*/mov r1, r0mov r0, r1bl putcharreturn:ldmfdsp!, {r0, r4-r7, pc} /* lr保存PC的值, input parameter:R0 ~ R3, return: R0 */.fnendmax_p:/*.fnstart*/stmfd sp!, {r4-r7, lr}cmp r0, r1bcs 1f/* cs为条件判断,大于等于0 */mov r0, r11:ldmfd sp!, {r4-r7, pc}/*.fnend*/#file:build.sh#!/bin/sharm-none-linux-gnueabi-as printk.s -o printk.oarm-none-linux-gnueabi-gcc -c hello.carm-none-linux-gnueabi-gcc -static hello.o printk.o -o hello
#include <stdio.h>memset: /* compute the offset to align the destination * offset = (4-(src&3))&3 = -src & 3 */ .fnstart .save {r0, r4-r7, lr} stmfd sp!, {r0, r4-r7, lr} rsb r3, r0, #0 ands r3, r3, #3 cmp r3, r2 movhi r3, r2 /* splat r1 */ mov r1, r1, lsl #24 orr r1, r1, r1, lsr #8 orr r1, r1, r1, lsr #16 /* memset(void *, int, int) 第二个参数只取低8位数据, 其他位使用该位填冲 */ /* 如果不对齐,数据以此拷贝,如r3=3, r3 << 31后cs标志位有效,同时r12的最高位位1,mi有效 */ movs r12, r3, lsl #31 strcsb r1, [r0], #1 /* can't use strh (alignment unknown) */ strcsb r1, [r0], #1 strmib r1, [r0], #1 subs r2, r2, r3 /* 1 ~3 个数据的情况下,函数返回 */ ldmlsfd sp!, {r0, r4-r7, lr} /* ls标志位表示小于或者等于0, return */ bxls lr /* align the destination to a cache-line */ mov r12, r1 mov lr, r1 mov r4, r1 mov r5, r1 mov r6, r1 mov r7, r1 /* 还有r1本身和r3 */ rsb r3, r0, #0 ands r3, r3, #0x1C beq aligned32 /* 32个字节对齐的情况下 */ cmp r3, r2 /* 计算出和32个字节对齐还需操作多少个字节 */ andhi r3, r2, #0x1C sub r2, r2, r3 /* conditionnaly writes 0 to 7 words (length in r3) */ /* 最多有28个字节:16 + 8 + 4 */ movs r3, r3, lsl #28 stmcsia r0!, {r1, lr} stmcsia r0!, {r1, lr} stmmiia r0!, {r1, lr} movs r3, r3, lsl #2 strcs r1, [r0], #4 aligned32: subs r2, r2, #32 /* count -= 32 */ mov r3, r1 bmi 2f 1: subs r2, r2, #32 stmia r0!, {r1,r3,r4,r5,r6,r7,r12,lr} /* 一次存储32Bytes */ bhs 1b /* 大于等于0的情况下循环:do {r2=r2 -32; copy;}while(r2>=0); */2: add r2, r2, #32 /* 最后的0 ~ 31的字节数据 */