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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Phillip M Gallo
04/27/02 17:56
Read: 2090 times
Torrance
USA


 
#22219 - RE: what is lookup tabels and how they work
Muhammad,

Hard to add anything to Andy's explanation, so how about some '51 examples that give me a chance to play with HTML tags.

Task:

Convert a nibble into an ASCII Hex Digit.

Method:

Use the nibble in the accum as an index into a table of ASCII characters constructed to correlate to the nibbles Hex value.

The Conversion Table Struct.
Hex_table:
  db   '0','1','2','3'
  db   '4','5','6','7'
  db   '8','9','A','B'
  db   'C','D','E','F'


Input:
Accum least sig. nibble has the 4 bit value to convert:
-------
mov   dptr,#hex_table ;Point to table base
movc  a,@a+dptr       ;Acc nibble = Table index
-------
The accum now has the ASCII char corresponding to the nibble hex value.If you enter with the accumulator = 00001000b=08H you will exit with the ASCII Value for '8'(38H). If you enter with the accumulator = 00001110b=0EH you will exit with the ASCII Value for 'E' (45H).

Easy wasn't it? Well probable too easy as we did not validate that the Acc had value greater than 0FH. Good practice dictates that we validate the input or our index could end up invalid. In the routine above you could just "ANL A,#0FH" and force the value to validity, but, if you are having problems this could mask it. It's best to "bound" the value, erroring out to a handler upon invalid input.

Another Look Up routine that is very common in 8051 code is that where canned messages are output to a terminal or display based upon a value in the accumulator. Message size is fixed to fill a display field.

Task:
Display one of 4 messages on a display device based upon the value in the accumulator


Method:Multiply accepted values by the message length.


The message table struct:

msg_table:
 db  'Begin  '
 db  'More   '
 db  'Enter >'
 db  'Quit ? '


Note there are only 4 entries so we must bound the input to values = 0,1,2 or 3.
Also note each message is 7 characters long.

Input:Acc = Message Entry Number. Check input value, reject any value greater than 3.

------------
  mov  dptr,#msg_table ;Init the base of the Message Table

;  ************************
;  *** Input Validation ***
;  ************************

  jz   msg_index       ;If acc=0 we have an acceptable value.
  cjne a,#3,eval_acc   ;We have a non-zero value is other than 3?
  jmp  msg_index       ;No it is 3, a valid value.

; *** It's not either a 0 or 3 value ***
; *** Test for an invalid value      ***

eval_acc:
  jnc  input_err       ;If we didn't carry the value exceeds that allowable.

; *****************************
; *** Compute Message Index ***
; *****************************

msg_index:
  mov  b,#7            ;Init msg length multiplier
  mul  ab              ;Multiply Index (Element Number) by Element size.

; ************************************
; *** One Approach to Table Access ***
; ************************************

  add  a,dpl            ;Add the Index Value to the low byte of dptr
  mov  dpl,a
  mov  a,dph            ;Correct for any carry (cross page boundry)
  addc a,#0
  mov  dph,a

 *******************************************
 *** We have an index into the Msg Table ***
 ***      Output the Selected Message    ***
 *******************************************

  mov  r2,#7             ;Init a msg counter - msg length
msg_loop:
  clr  a                 ;Index already in dptr so clear acc
  movc a,@a+dptr
  inc  dptr             ;Point to next char
  call txo              ;Output to display
  djnz r2,msg_loop      ;Down count through available chars.
  ret

input_err:
 (error handling)

txo:
 (your display routine. (Must not contaminate DPTR or R2)

The routine above works for all data values. More typically ASCII Messages are tabled with a trailing 0 (null terminator) this would change the message routine in following way:

1)The message multiplied would be 8 as we have added a "00H" to the end of each table element.
2)We would not need a loop counter.

Our look up could now follow as:

 *******************************************
 *** We have an index into the Msg Table ***
 ***      Output the Selected Message    ***
 *******************************************

msg_loop:
  clr   a                ;Index already in dptr so clear acc
  movc  a,@a+dptr        ;Fetch a tabled char
  jz    msg_loop_exit    ;If it's a Null we are done
  inc   dptr             ;Point to next char
  call  txo              ;Output to display
  jmp   msg_loop         ;Loop until Terminator is found

msg_loop_exit:
  ret


The routine above is typical of ASCII handling but it's sensitivity to the null value
eliminates it's suitability for variable data.

Sorry for the "verbosity" the HTML tags are fun.

regards,
p










List of 8 messages in thread
TopicAuthorDate
what is lookup tabels and how they work      Muhammad Sajjad      04/27/02 00:06      
RE: what is lookup tabels and how they work      Andy Neil      04/27/02 09:13      
RE: what is lookup tabels and how they w      Steve M. Taylor      04/27/02 15:43      
RE: what is lookup tabels and how they work      Phillip M Gallo      04/27/02 17:56      
RE: what is lookup tabels and how they work      YASHWANTH Y.V.      04/28/02 09:54      
RE: when not to use a lookup table      J. Guy      04/28/02 10:58      
RE: when not to use a lookup table      Phillip M Gallo      04/28/02 21:47      
RE: when not to use a lookup table      Muhammad Sajjad      04/29/02 05:10      

Back to Subject List