Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
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.

List of 44 messages in thread
TopicAuthorDate
Weekend Quiz - easy            01/01/70 00:00      
   Dumbbbbbb            01/01/70 00:00      
      I know of 3, and it is \"synthtetic\"...            01/01/70 00:00      
   Hi Jan!            01/01/70 00:00      
      2 out of 3            01/01/70 00:00      
   Comment lies!            01/01/70 00:00      
      that makes 4... embarrassing            01/01/70 00:00      
         #4            01/01/70 00:00      
            N-th            01/01/70 00:00      
               Similar            01/01/70 00:00      
         Thats the one I saw first too !            01/01/70 00:00      
   Another lyin\' comment            01/01/70 00:00      
      Bingo!            01/01/70 00:00      
   seems solved - so now for the equivalent in C?            01/01/70 00:00      
      Oops            01/01/70 00:00      
         Gee ... it was just an 'x' ...            01/01/70 00:00      
            Yup ...            01/01/70 00:00      
            "just" an x ?            01/01/70 00:00      
   writing SBUF without checking?            01/01/70 00:00      
      well...            01/01/70 00:00      
      the REAL mistake is using an HLL rather than ASM            01/01/70 00:00      
         Not so            01/01/70 00:00      
   Shall we continue this???            01/01/70 00:00      
      the spec itself is problematic            01/01/70 00:00      
         How would you fix the spec?            01/01/70 00:00      
            handle DEL (0x7f) and BS?            01/01/70 00:00      
               throwing in something to chew on... :-)            01/01/70 00:00      
               Are DEL and BS equivalent ???            01/01/70 00:00      
                  relax.            01/01/70 00:00      
      ReceiveString, rev.1            01/01/70 00:00      
         ReceiveString, rev. 2            01/01/70 00:00      
            there are many ways...            01/01/70 00:00      
               Comments on comments on ...            01/01/70 00:00      
                  (comments on)^3            01/01/70 00:00      
                  Caller-saves            01/01/70 00:00      
                     Caller-save vs. Callee-save            01/01/70 00:00      
                        Compiler trade-off            01/01/70 00:00      
      ReceiveString, rev. 3            01/01/70 00:00      
   Sunday Challenge (rev 4)            01/01/70 00:00      
      hard to beat...            01/01/70 00:00      
         Just one more byte ...            01/01/70 00:00      
            want to spare bytes?            01/01/70 00:00      
               CALL vs ACALL            01/01/70 00:00      
                  it depends            01/01/70 00:00      

Back to Subject List