Using Lookup Table for a Beginner
scourge069
Posts: 2
..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!
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
Do you understand how to define strings of bytes in MASM? I guess it might be something equivalent to
Let's make life easy and make all the string the same length like so:
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:
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?
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
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
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?
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.