The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Neil on July 04, 2010, 01:37:02 PM

Title: xlat problem
Post by: Neil on July 04, 2010, 01:37:02 PM
I've used xlat many times in 16 bit programs without any problems, now this is the first time I've used it in 32 bit. I copied the code directly from a working program, it assembles OK but crashes at the xlat instruction. here is the code with labels changed to make it readable :-

lea bx,table-1
mov al,contents of variable
xlat

Any suggestions ?
Title: Re: xlat problem
Post by: KeepingRealBusy on July 04, 2010, 02:12:26 PM
Yes, use the following:


    mov   ebx,table-1
    movzx eax,WORD PTR (contents of variable)
    xlat


The entire contents of eax and ebx are used in xlat to access the lookup table. The upper half of eax and ebx probably had data
Title: Re: xlat problem
Post by: Neil on July 04, 2010, 02:16:13 PM
Solved, should be ebx not bx. The opcode help in Masm only mentions bx maybe this is incorrect or am i reading it wrong?
Title: Re: xlat problem
Post by: hutch-- on July 04, 2010, 02:16:26 PM
eil,

The address in 32 bit code must always be a 32 bit address, BX is simply not a valid address in 32 bit. XLAT? works OK but its usually faster to manually code the extraction from the 256 member lookup table than to use XLAT.
Title: Re: xlat problem
Post by: Neil on July 04, 2010, 02:25:56 PM
hutch do you mean something like this :-

lea ebx,table
movzx eax,variable contents
mov al byte ptr,[ebx+eax]
Title: Re: xlat problem
Post by: Farabi on July 04, 2010, 02:28:46 PM
Quote from: Neil on July 04, 2010, 02:25:56 PM
hutch do you mean something like this :-

lea ebx,table
movzx eax,variable contents
mov al byte ptr,[ebx+eax]

I bet that what he mean.
Title: Re: xlat problem
Post by: dedndave on July 04, 2010, 02:44:32 PM
mov   ebx,offset table
movzx eax,byte ptr variable_contents
movzx eax,byte ptr [ebx+eax]


even better...
movzx eax,byte ptr variable_contents
movzx eax,byte ptr table[eax]

will work if it's always the same table
Title: Re: xlat problem
Post by: Neil on July 04, 2010, 02:51:21 PM
Yes Dave, that's what i meant to write but it didn't quite turn out that way  :bg time for a lie down  :lol
Title: Re: xlat problem
Post by: dedndave on July 04, 2010, 02:54:55 PM
i just got up, Neil
i am good for another 3 or 4 hours   :lol
Title: Re: xlat problem
Post by: Neil on July 04, 2010, 03:09:13 PM
That second piece of code you posted looks good but i have a minor problem with it. The variable never contains zero so the offset will be 1 out in the table.
This won't assemble:-

movzx eax,byte ptr variable
movzx eax,byte ptr table-1[eax]

This will :-

movzx eax,byte ptr variable
movzx eax,byte ptr table[eax]

So to use it I have to put a padding byte at the start of the table, unless you've got a better suggestion.
Title: Re: xlat problem
Post by: dedndave on July 04, 2010, 03:13:08 PM
movzx eax,byte ptr table[eax-1]
now, you've got a 255 byte table
might be better to add the extra byte to the table, just for the sake of alignment
Title: Re: xlat problem
Post by: Neil on July 04, 2010, 03:22:39 PM
Quote from: dedndave on July 04, 2010, 03:13:08 PM
movzx eax,byte ptr table[eax-1]
now, you've got a 255 byte table
might be better to add the extra byte to the table, just for the sake of alignment


Nice one, I didn't think of that.
You could be right about alignment, i'll play with it a bit.
Title: Re: xlat problem
Post by: hutch-- on July 04, 2010, 10:33:40 PM
Something sounds wrong here, you should align the 256 item table to a boundary of 4 then when you use 1 characters as the reference you simply add the character value to the table offset to get its replacement. Range is 0 to 255 like normal, there should not be anything added to it.
Title: Re: xlat problem
Post by: dedndave on July 04, 2010, 10:53:27 PM
he said the index value to be translated is never 0, so he is considering eliminating that entry in the table
i can see that, if you put the table in the string area of the data segment
you may only have 5 possible values (say, 6-10)
the table only needs to be 5 bytes long
        movzx   eax,byte ptr Table[eax-6]
Title: Re: xlat problem
Post by: Neil on July 05, 2010, 08:20:48 AM
I've slept on this one, literally  :bg & decided it would be better if I made the variable range 0 to whatever rather than start at 1. This would solve any offset problem, got to look into it as I'm not sure how it will affect any other code.
Title: Re: xlat problem
Post by: dedndave on July 05, 2010, 04:23:50 PM
        movzx   eax,byte ptr Table[eax-6]
        movzx   eax,byte ptr Table[eax]
the assembler generates the same instruction, either way
        movzx   eax,byte ptr [eax+nnnnnnnn]
the only difference is the immediate offset value