I'm looking for a simpler code that isn't resource hungry that can compare two strings stored in different locations in the extended memory of a classic 8052 (namely the AT89S52 microcontroller) to make sure the characters match exactly.

The error flag can be a bit value, but the only thing that concerns me is that I need scanning to begin at any two 16-bit memory locations of my choice and that the same number of consecutive bytes in each location need to be scanned.

I was considering applying boolean logic to DPH or the DPL variables (example ANL and ORL) but at the same time will I be able to make that programmer friendly? I don't want to make the memory addressing confusing later on. Also, I'm looking for code that can replace this that can provide better performance (meaning fewer instructions required to execute a string comparison code).

Any ideas are welcome.

;Load # bytes to check in R7
mov R7,#BYTESTOCHECK

;R6 = our error status. Assume no error here
mov R6,#0h

;Load first memory pointer address to R2:R3
mov R2,#HIGHBYTE1
mov R3,#LOWBYTE1

;Load second memory pointer address to R4:R5
mov R4,#HIGHBYTE2
mov R5,#LOWBYTE2

checknextbyte:

  mov DPL,R2
  mov DPH,R3
  ;DPTR here = R2:R3
  movx A,@DPTR
  ;Store value at DPTR in B
  mov B,A
  inc DPTR
  mov R3,DPH
  mov R2,DPL

  mov DPL,R4
  mov DPH,R5
  ;DPTR here = R4:R5
  movx A,@DPTR
  ;Store value at DPTR in A
  inc DPTR
  mov R5,DPH
  mov R4,DPL

  CJNE A,B,notsame
    ;Both addresses aren't same so string isn't same
    ;make R7=1 to exit loop faster
    mov R7,#1h
    ;Make R6=1 to show we have error
    mov R6,#1h
  notsame:

djnz R7,checknextbyte
up vote 2 down vote accepted
  • AT89S52 is not a classic 8052. Like all modern 8052 variants I know of, it has one feature which really shines here. Namely, it is dual data pointers. Instead of reloading DPL and DPH every time, initialize them once, and just toggle the AUXR1.DPS bit:

    ; setup
        clr AUXR1.DPS
        mov DPL, R2
        mov DPL, R3
    
        cpl AUXR1.DPS
        mov DPL, R4
        mov DPL, R5
        ....
    
    ; loop
    checknexbyte:
        cpl AUXR1.DPS
        movx A, @DPTR
        mov B, A
        inc DPTR
    
        cpl AUXR1.DPS
        movx A, @DPTR
        inc DPTR
    
        cjne mismatch
        djnz R7, checknextbyte
    mismatch:
    
  • You may spare a register (R6), few cycles, and some bytes by using C flag (set by CJNE) as a match/mismatch indication.

  • It looks like the exit logic is incorrect. On a mismatch (A != B) the jump is taken, and the loop continues. Correct me if I am wrong.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.