Shop OBEX P1 Docs P2 Docs Learn Events
Using Lookup Table for a Beginner — Parallax Forums

Using Lookup Table for a Beginner

scourge069scourge069 Posts: 2
edited 2013-02-25 18:46 in General Discussion
..Need help !

I am working on a program that asks the user to enter a color then display the hexadecimal equivalent of that color ..We were told to have atleast 50 available colors ..my problem is, if I am to use a simpe If_Else for this, I would have to use tons of variables for comparison..my instructor doesn't want that..instead, he told me to search for Lookup tables and study them ..but as a beginner in programming..I don't understand anything regarding this topic yet ..:D
Can anyone help me with this please ? atleast can someone make me a simple program for this..

PREVIEW:
Enter a color: alice blue

Hex: #f0f8ff

something like this, please people, I'm really having a hard time understanding MASM..my future depends on this ..:(

I'm using Irvine32.Inc and nothing else ..
MASM615,


THANK YOU IN ADVANCE!

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-02-23 03:44
    Can't really help with MASM, I presume you mean the assembler, but how about some clues.
    Do you understand how to define strings of bytes in MASM? I guess it might be something equivalent to
    str1: DB 'b', 'l', 'u', 'e'
    

    Let's make life easy and make all the string the same length like so:
    str1: DB 'b', 'l', 'u', 'e', ' ', ' '
    str2: DB 'r', 'e', 'd', ' ', ' ', ' ' 
    str3: DB 'g', 'r', 'e', 'e', 'n, ' '
    str4: DB 'o', 'r', 'a', 'n', 'g', 'e'
    

    Ah, well now we have a table of color names. Or colour names as we say around here. So we don't need to name them all, just the first one like so:
    colour_table: DB 'b', 'l', 'u', 'e', '0', '0'
                   DB 'r', 'e', 'd', '0', '0', '0', '0' 
                   DB 'g', 'r', 'e', 'e', 'n, '0', '0'
                    DB 'o', 'r', 'a', 'n', 'g', 'e', '0'
    
    Note, I put a zero at the end of each string. That's so I know when it ends when I am reading it later.

    So, to get any colour name we only need to use the address of colour_table and add on some number (0, 1, 2, 3, etc) times 6. e.g. color_table + 12 is the start of the string "green"

    Now all we have to do is get the input string from the user. And compare it to the strings we can find in the table. When we have found a match the offset into the table will be some number.

    We can now use that number to index another table containing the r,g,b hex values of that colour, or whatever the output should be.

    Any questions?
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-02-23 07:48
    MASM=Microsoft Macro Assembler
    http://cs.uns.edu.ar/~jechaiz/organizacion/TechDocs/MASMDoc/index.html

    comparing input to a table byte by byte.
    as colornames have many different lenghts, your routine have to look for the zero and make a match and then add 3 to data pointer to look for the next color match.
    I made the 24bit hex value in to three bytes, so merge them correctly as reading them as long will not work as they can start on a odd address.

    table
    db "blue", 0, 0x00,0x00,0xff
    db "red", 0, 0xff,0x00,0x00
  • lanternfishlanternfish Posts: 366
    edited 2013-02-23 18:20
    Hi

    I am assuming that this table will reference lighting gel.

    I am working a small project that will enable me to quickly sort gel that has been cut but not numbered (apparently some of my lx people think it is too much hassle)

    My system will use a colour sensor and use a look up table to give me the Rosco,Lee or Chris James equivalent.

    And I just know that this project will end up taking far more time to bring to fruition than I would like.

    Cheers
  • scourge069scourge069 Posts: 2
    edited 2013-02-25 18:13
    Heater. wrote: »
    Can't really help with MASM, I presume you mean the assembler, but how about some clues.
    Do you understand how to define strings of bytes in MASM? I guess it might be something equivalent to
    str1: DB 'b', 'l', 'u', 'e'
    

    Let's make life easy and make all the string the same length like so:
    str1: DB 'b', 'l', 'u', 'e', ' ', ' '
    str2: DB 'r', 'e', 'd', ' ', ' ', ' ' 
    str3: DB 'g', 'r', 'e', 'e', 'n, ' '
    str4: DB 'o', 'r', 'a', 'n', 'g', 'e'
    

    Ah, well now we have a table of color names. Or colour names as we say around here. So we don't need to name them all, just the first one like so:
    colour_table: DB 'b', 'l', 'u', 'e', '0', '0'
                   DB 'r', 'e', 'd', '0', '0', '0', '0' 
                   DB 'g', 'r', 'e', 'e', 'n, '0', '0'
                    DB 'o', 'r', 'a', 'n', 'g', 'e', '0'
    
    Note, I put a zero at the end of each string. That's so I know when it ends when I am reading it later.

    So, to get any colour name we only need to use the address of colour_table and add on some number (0, 1, 2, 3, etc) times 6. e.g. color_table + 12 is the start of the string "green"

    Now all we have to do is get the input string from the user. And compare it to the strings we can find in the table. When we have found a match the offset into the table will be some number.

    We can now use that number to index another table containing the r,g,b hex values of that colour, or whatever the output should be.

    Any questions?
    colour_table: DB 'b'. 'l', 'u', 'e', '0', '0'                      ;6 characters?
                        DB 'r', 'e', 'd', '0', '0', '0', '0'                ; 7 characters?
    

    you said all strings were supposed to be of the same length ?
    can you show me how to compare the entered string by the user to colour_table and to another table containing the hex values ? please? please?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-25 18:46
    You have to remember that this is a Parallax support forum. We provide support for Parallax products and a little support for the use of Parallax products with other microcontrollers. MASM is way off topic for what we do although the same techniques can be used with both the Basic Stamps and the Propeller.

    A lot depends on how you need to store the tables. Usually with the Stamps and Propeller, names would be stored as an ordinary character string, one byte per character, with a zero byte to mark the end. The string you want to look up would also be terminated with a zero byte. You compare the two byte by byte until one of the bytes doesn't match. In that case, you move ahead in the name table until you find the zero byte marking the end of the table entry and position your pointers to the next word (and reset the pointer to the string you're looking up). You also increment a counter that keeps track of the entry number. If the last match was for two zero bytes, you're done and the current entry number is what you want. If the next name to be looked at in the table starts with a zero byte, you've reached the end of the table (which is ended by an empty string ... just a zero byte) and the input string isn't in the table.
Sign In or Register to comment.