新聞中心

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

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

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

        2. 通過C語言調(diào)用階乘運算結(jié)果并顯示出來。

        本文引用地址:http://www.104case.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) 調(diào)試觀察結(jié)果:

        顯示:Hello! Please Input N:

        輸入5

        顯示5!=120;

        輸入3!=6;

        結(jié)果正確。



        評論


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

        關(guān)閉
        主站蜘蛛池模板: 开远市| 锦屏县| 凯里市| 临汾市| 阿城市| 连南| 中阳县| 游戏| 贵溪市| 敦化市| 花垣县| 邢台县| 滨州市| 大足县| 吴忠市| 桑日县| 裕民县| 措勤县| 延安市| 高雄县| 安平县| 祥云县| 云南省| 达州市| 太和县| 逊克县| 墨脱县| 福州市| 交城县| 普宁市| 苏尼特右旗| 邯郸市| 商城县| 安国市| 高密市| 石泉县| 丰镇市| 公主岭市| 竹溪县| 吴川市| 昂仁县|