汇编子程序模块:
NBYTE_BCD.asm- ;========================================================================
- ; extern void NBYTEBCD(unsigned char idata *pri, unsigned char leni, unsigned char idata *pro, unsigned char leno);
- ;
- ; N字节无符号二进制 对应 十进制BCD码字节数
- ; (N <= 4 ) 则 N+1
- ; (N <= 9 ) 则 N+2
- ; (N <= 12) 则 N+3
- ; ……
- $NOMOD51
-
- NAME NBYTE_BCD ;模块名
-
- ?PR?_NBYTEBCD?NBYTE_BCD SEGMENT CODE ;在程序存储区中定义段
- ?DT?_NBYTEBCD?NBYTE_BCD SEGMENT DATA OVERLAYABLE
- PUBLIC ?_NBYTEBCD?BYTE
- PUBLIC _NBYTEBCD ;用PUBLIC 声明函数为公共函数
-
- RSEG ?DT?_NBYTEBCD?NBYTE_BCD
- ?_NBYTEBCD?BYTE:
- pri?040: DS 1 ;R7
- leni?041: DS 1 ;R5
- pro?042: DS 1 ;R3
- ORG 3
- leno?043: DS 1
- RSEG ?PR?_NBYTEBCD?NBYTE_BCD ;用RSEG 表示函数可被连接器放置在任何地方
-
- ;========================================================================
- ;函数:N字节无符号二进制转十进制BCD码
- ;输入:*pri
- ;输出:*pro
- ;资源:ACC、CY、R0、R1、R3、R4、R5、R7、leno?043
- ; ROM:59B(包括调用函数时的形参输入)
-
- _NBYTEBCD:
- ;USING 0
- MOV A,R7
- ADD A,R5
- DEC A
- MOV R7,A
-
- MOV A,leno?043
- MOV R4,A
- ADD A,R3
- DEC A
- MOV R3,A
-
- MOV R0,A
- NP__0:
- MOV @R0,#0
- DEC R0
- DJNZ R4,NP__0
-
- MOV A,R5 ;转换(R5)字节二进制整数
- RL A
- RL A
- RL A
- MOV R1,A ;计数器
- CLR C
- NP__1:
- MOV A,R5
- MOV R4,A ;循环(R5)次
- MOV A,R7
- MOV R0,A
- NP__2:
- MOV A,@R0 ;从高端移出待转换数的1位到CY中
- RLC A
- MOV @R0,A
- DEC R0
- DJNZ R4,NP__2
-
- MOV A,leno?043
- MOV R4,A ;循环(leno)次
- MOV A,R3
- MOV R0,A
- NP__3:
- MOV A,@R0 ;BCD码带进位自身相加,相当于乘2
- ADDC A,@R0
- DA A ;十进制调整
- MOV @R0,A
- DEC R0
- DJNZ R4,NP__3
-
- DJNZ R1,NP__1
-
- RET
-
- END
复制代码使用方法,将
NBYTE_BCD.asm 添加到工程中,在
main.c 中声明后就可以调用,举例:
- unsigned char asc[8] = {0x0A,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};
- unsigned char nz[10];
-
- extern void NBYTEBCD(unsigned char idata *pri, unsigned char leni, unsigned char idata *pro, unsigned char leno);
-
- void main(void)
- {
- NBYTEBCD(asc, 8, nz, 10);
-
- while (1)
- {
-
- }
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
用C写这个N字节转换还真不会,
如果你有C写的可否帖上来
一周热门 更多>