??? 05/12/07 22:44 Read: times |
#139150 - seems solved - so now for the equivalent in C? Responding to: ???'s previous message |
Well I'm trying not to reproduce the bugs within the assembly code:)
Just to provide some C code which might come close to what was meant in assembly: #include <8052.h> unsigned char __idata ReceiveBuffer[0x20]; void ReceiveString (void) { unsigned char __idata *rptr = ReceiveBuffer; unsigned char c; do { do { while (!RI) // wait until character received ; c = SBUF; RI = 0; } while (c & 0x80); // if unprintable (>= 80h), ignore it SBUF = c; // echo it if (c < 0x20) // ctrl char? { if (c == 0x08 && rptr != ReceiveBuffer) rptr--; // if BackSpace, eat last character from buffer } else *rptr++ = c; // put it into buffer if (rptr == &ReceiveBuffer[sizeof ReceiveBuffer - 1] + 1) // not in original code rptr = &ReceiveBuffer[sizeof ReceiveBuffer - 1]; // prevent buffer overflow } while (c != 0x13); *rptr = 0; // end of string, not in original code } compiles to: 0000 339 _ReceiveString: 0000 78r00 348 mov r0,#_ReceiveBuffer 0002 349 00101$: 0002 30 98 FD 350 jnb _RI,00101$ 0005 AA 99 351 mov r2,_SBUF 0007 C2 98 352 clr _RI 0009 EA 353 mov a,r2 000A 20 E7 F5 354 jb acc.7,00101$ 000D 8A 99 355 mov _SBUF,r2 000F BA 20 00 356 cjne r2,#0x20,00130$ 0012 357 00130$: 0012 50 0C 358 jnc 00111$ 0014 BA 08 0C 359 cjne r2,#0x08,00112$ 0017 E8 360 mov a,r0 0018 B4r00 02 361 cjne a,#_ReceiveBuffer,00134$ 001B 80 06 362 sjmp 00112$ 001D 363 00134$: 001D 18 364 dec r0 001E 80 03 365 sjmp 00112$ 0020 366 00111$: 0020 A6 02 367 mov @r0,ar2 0022 08 368 inc r0 0023 369 00112$: 0023 E8 370 mov a,r0 0024 B4r20 02 371 cjne a,#(_ReceiveBuffer + 0x0020),00116$ 0027 78r1F 372 mov r0,#(_ReceiveBuffer + 0x001f) 0029 373 00116$: 0029 BA 13 D6 374 cjne r2,#0x13,00101$ 002C 76 00 375 mov @r0,#0x00 002E 22 376 ret Note the C source implements three things that the assembler version did not: it appends zero to the end of the string, it checks for the end of the buffer and it includes ret. So it might be fair to subtract some bytes of the C version if you compare against the assembler version. |