新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > C語(yǔ)言和ARM匯編混合編程實(shí)現(xiàn)階乘運(yùn)算

C語(yǔ)言和ARM匯編混合編程實(shí)現(xiàn)階乘運(yùn)算

作者: 時(shí)間:2016-11-11 來(lái)源:網(wǎng)絡(luò) 收藏
1.階乘運(yùn)算必須用匯編語(yǔ)言實(shí)現(xiàn);

2. 通過(guò)C語(yǔ)言調(diào)用階乘運(yùn)算結(jié)果并顯示出來(lái)。

本文引用地址:http://m.butianyuan.cn/article/201611/316769.htm

(1) 用匯編語(yǔ)言編寫階乘運(yùn)算子程序,命名為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) 將其添加到半主機(jī)程序中的SYS中;

(3) 將半主機(jī)程序的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) 調(diào)試觀察結(jié)果:

顯示:Hello! Please Input N:

輸入5

顯示5!=120;

輸入3!=6;

結(jié)果正確。



評(píng)論


技術(shù)專區(qū)

關(guān)閉