typedef unsigned char uint8;
typedef unsigned short uint16;
#define str_size_t uint16
uint8 u8_to_ascii(uint8 h)
{
if( h<=9)
{
h=h+'0';
}
else if(h<=15)
{
h=h+'A'-10;
}
else
{
h=0;
}
return h;
}
char *STR_Rev(char *str)
{
char temp;
char *pStart = str;
char *pEnd = str;
while(pEnd[0]!=0)
{
pEnd++;
}
pEnd--;
while(pStart < pEnd)
{
temp = pStart[0];
(pStart++)[0] = pEnd[0];
(pEnd--)[0] = temp;
}
return str;
}
//uint16 转换成 10进制字符串,固定长度,不满则填充blank
str_size_t u16_to_str10_in_lenggth(uint16 val,char * buf,str_size_t len,uint8 blank)
{
str_size_t index=0;
uint8 temp;
do{
temp=val%10;
val=val/10;
temp=u8_to_ascii(temp);
((uint8 *)buf)[index]=temp;
index++;
if(index>=len)
{
break;
}
}while(val);
while(index<len)
{
((uint8 *)buf)[index]=blank;
index++;
}
((uint8 *)buf)[index]=0;
STR_Rev((char *)buf);
return index;
}
char str[50];
int main(void)
{
u16_to_str10_in_lenggth(0,str,4,'0');
while(1);
}
运行正确结果,str应该是"0000",
而IAR 8051 8.10运行结果却是"0";
汇编代码:
RSEG NEAR_CODE:CODE:NOROOT(0)
// 48 str_size_t u16_to_str10_in_lenggth(uint16 val,char * buf,str_size_t len,uint8 blank)
u16_to_str10_in_lenggth:
CODE
// 49 {
MOV A,#-0xf
LCALL ?FUNC_ENTER_XDATA
; Saved register size: 15
; Auto size: 0
MOV A,R2
MOV R6,A
MOV A,R3
MOV R7,A
MOV ?V0 + 0,R4
MOV ?V0 + 1,R5
MOV ?V0 + 4,R1
MOV A,#0xf
LCALL ?XSTACK_DISP0_8
MOVX A,@DPTR
MOV ?V0 + 6,A
INC DPTR
MOVX A,@DPTR
MOV ?V0 + 7,A
// 50 str_size_t index=0;
MOV R4,#0x0
MOV R5,#0x0
MOV DPL,?V0 + 0
MOV DPH,?V0 + 1
// 51 uint8 temp;
// 52 do{
// 53 temp=val%10;
??u16_to_str10_in_lenggth_0:
MOV A,R6
MOV R0,A
MOV A,R7
MOV R1,A
MOV R2,#0xa
MOV R3,#0x0
LCALL ?US_DIV_MOD
MOV A,R2
MOV ?V0 + 5,A
// 54 val=val/10;
MOV A,R6
MOV R0,A
MOV A,R7
MOV R1,A
MOV R2,#0xa
MOV R3,#0x0
LCALL ?US_DIV_MOD
MOV ?V0 + 2,R0
MOV ?V0 + 3,R1
MOV R6,?V0 + 2
MOV R7,?V0 + 3
// 55
// 56 temp=u8_to_ascii(temp);
MOV A,R2
SUBB A,#0xa
JNC ??u16_to_str10_in_lenggth_1
MOV A,#0x30
??u16_to_str10_in_lenggth_2:
ADD A,?V0 + 5
SJMP ??u16_to_str10_in_lenggth_3
??u16_to_str10_in_lenggth_1:
MOV A,R2
CLR C
SUBB A,#0x10
JNC ??u16_to_str10_in_lenggth_4
MOV A,#0x37
SJMP ??u16_to_str10_in_lenggth_2
??u16_to_str10_in_lenggth_4:
CLR A
// 57 ((uint8 *)buf)[index]=temp;
??u16_to_str10_in_lenggth_3:
MOVX @DPTR,A
// 58 index++;
MOV A,R4
ADD A,#0x1
INC R4
MOV A,R5
ADDC A,#0x0
MOV R5,A
// 59 if(index>=len)
CLR C
MOV A,R4
SUBB A,?V0 + 6
MOV A,R5
SUBB A,?V0 + 7
JNC ??u16_to_str10_in_lenggth_5
// 60 {
// 61 break;
// 62 }
// 63
// 64 }while(val);
INC DPTR
MOV A,R6
ORL A,R7
JNZ ??u16_to_str10_in_lenggth_0
??u16_to_str10_in_lenggth_5:
MOV A,?V0 + 6
CLR C
SUBB A,R4
MOV R2,A
MOV A,?V0 + 7
SUBB A,R5
MOV R3,A
// 65
// 66 while(index<len)
CLR C
MOV A,R4
SUBB A,?V0 + 6
MOV A,R5
SUBB A,?V0 + 7
JNC ??u16_to_str10_in_lenggth_6
// 67 {
// 68 ((uint8 *)buf)[index]=blank;
MOV A,?V0 + 0
ADD A,R4
MOV DPL,A
MOV A,?V0 + 1
ADDC A,R5
MOV DPH,A
??u16_to_str10_in_lenggth_7:
MOV A,?V0 + 4
MOVX @DPTR,A
INC DPTR
MOV A,R2
ADD A,#-0x1
DEC R2
MOV A,R3
ADDC A,#-0x1
MOV R3,A
ORL A,R2
JNZ ??u16_to_str10_in_lenggth_7
// 69 index++;
MOV A,R4
ADD A,R2
MOV R4,A
MOV A,R5
ADDC A,R3
MOV R5,A
// 70 }
// 71
// 72 ((uint8 *)buf)[index]=0;
??u16_to_str10_in_lenggth_6:
MOV A,?V0 + 0
ADD A,R4
MOV DPL,A
MOV A,?V0 + 1
ADDC A,R5
MOV DPH,A
CLR A
MOVX @DPTR,A
// 73
// 74 STR_Rev((char *)buf);
MOV R0,?V0 + 0
MOV R1,?V0 + 1
MOV DPL,R0
MOV DPH,R1
MOVX A,@DPTR
JZ ??u16_to_str10_in_lenggth_8
??u16_to_str10_in_lenggth_9:
MOV DPL,R0
MOV DPH,R1
INC DPTR
MOV R0,DPL
MOV R1,DPH
MOVX A,@DPTR
JNZ ??u16_to_str10_in_lenggth_9
??u16_to_str10_in_lenggth_8:
MOV A,R0
ADD A,#-0x1
DEC R0
MOV A,R1
ADDC A,#-0x1
MOV R1,A
CLR C
MOV A,?V0 + 0
SUBB A,R0
MOV A,?V0 + 1
SUBB A,R1
JNC ??u16_to_str10_in_lenggth_10
??u16_to_str10_in_lenggth_11:
MOV DPL,?V0 + 0
MOV DPH,?V0 + 1
MOVX A,@DPTR
MOV R2,A
MOV DPL,R0
MOV DPH,R1
MOVX A,@DPTR
MOV DPL,?V0 + 0
MOV DPH,?V0 + 1
MOVX @DPTR,A
INC DPTR
MOV ?V0 + 0,DPL
MOV ?V0 + 1,DPH
MOV A,R2
MOV DPL,R0
MOV DPH,R1
MOVX @DPTR,A
MOV A,R0
ADD A,#-0x1
DEC R0
MOV A,R1
ADDC A,#-0x1
MOV R1,A
CLR C
MOV A,?V0 + 0
SUBB A,R0
MOV A,?V0 + 1
SUBB A,R1
JC ??u16_to_str10_in_lenggth_11
// 75
// 76 return index;
??u16_to_str10_in_lenggth_10:
MOV A,R4
MOV R2,A
MOV A,R5
MOV R3,A
MOV R7,#0x8
LJMP ?FUNC_LEAVE_XDATA
// 77 }
// 78
一周热门 更多>