1.阶乘运算有必要用汇编言语完成;
2. 经过C言语调用阶乘运算成果并显现出来。
(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
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: \n\n”);
LCD_Printf(“Hello! Please Input N: \n\n”);
scanf(“%d”,&a);
b=asmDouble(a);
printf(“%d!=%d\n”,a,b);
LCD_Printf(“%d!=%d\n”,a,b);
}
}
(4) 调试调查成果:
显现:Hello! Please Input N:
输入5
显现5!=120;
输入3!=6;
成果正确。