C语言和ARM汇编混合编程实现阶乘运算
2. 通过C语言调用阶乘运算结果并显示出来。
本文引用地址://m.amcfsurvey.com/article/201611/316769.htm(1) 用汇编语言编写阶乘运算子程序,命名为zmc.s;
程序如下:
AREA asmfile,CODE,READONLY
EXPORT asmDouble
asmDouble
sub R1,R0,#1
cmp R1,#00
BEQ L2
L1 mul R2,R0,R1
sub R1,R1,#1
mov R0,R2
cmp R1,#00
BNE L1
L2 mov pc, lr
END
(2) 将其添加到半主机程序中的SYS中;
(3) 将半主机程序的main修改如下:
#include "def.h"
#include "44b.h"
#include "stdio.h"
#include "sys_lcd.h"
extern int asmDouble(int a);
void Delay(int time)
{
volatile int i,j;
i = 0;
j = 0;
for(i = 0; i
{
j = 0;
while(j++<30)
;
}
}
struct __FILE
{
int handle;
};
FILE __stdout, __stdin;
void UART_Init(int baud)
{
rUFCON0=0x0; // FIFO disable
rUFCON1=0x0;
rUMCON0=0x0;
rUMCON1=0x0;
rULCON0=0x3; // UART0
rUCON0=0x245;
rUBRDIV0=( (int)(MCLK/16./baud + 0.5) -1 );
rULCON1=0x3; // UART1
rUCON1=0x245;
rUBRDIV1=( (int)(MCLK/16./baud + 0.5) -1 );
Delay(10);
}
#define SemiSWI 0x123456
__swi(SemiSWI) void _WriteC(unsigned op, char *c);
#define WriteC(c) _WriteC(0x3,c)
__swi(SemiSWI) unsigned char _ReadC(unsigned op, char val);
#define ReadC() _ReadC(0x7,0)
__swi(SemiSWI) void _Exit(unsigned op, unsigned except);
#define Exit() _Exit(0x18,0x20026)
void UART_PutChar(unsigned char data)
{
rUTXH0=data;
while(!(rUTRSTAT0 & 0x2))
;
}
char UART_GetChar(void)
{
while(!(rUTRSTAT0 & 0x1))
;
return rURXH0;
}
extern void LCD_PutChar(unsigned char data);
int fputc(int ch, FILE *f)
{
char tempch = ch;
WriteC( &tempch );
return ch;
}
int fgetc(FILE *f)
{
unsigned char tempch;
tempch = ReadC();
return tempch;
}
int ferror(FILE *f)
{
return EOF;
}
void _sys_exit(int return_code)
{
Exit(); /* for debugging */
label: goto label; /* endless loop */
}
int main(void)
{
int a;
int b;
Port_Init();
IO82C55A_Init();
UART_Init(115200);
LCD_Init();
LCD_ChangeMode(DspTxtMode);
LCD_Printf("Hello World!n");
while(1)
{
printf("Hello! Please Input N: nn");
LCD_Printf("Hello! Please Input N: nn");
scanf("%d",&a);
b=asmDouble(a);
printf("%d!=%dn",a,b);
LCD_Printf("%d!=%dn",a,b);
}
}
(4) 调试观察结果:
显示:Hello! Please Input N:
输入5
显示5!=120;
输入3!=6;
结果正确。
评论