News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

UNIONs and moving hi and lo words ...

Started by James Ladd, October 14, 2006, 02:07:19 AM

Previous topic - Next topic

James Ladd

If I have this structure

typedef union pointer {
  struct {
    volatile unsigned short count;
    volatile unsigned short ptr;
  } sep;
  volatile unsigned long con;
}pointer_t;


which I have converted to assembly and I have a pointer to this structure in memory
held in eax.

How can I move values into count and ptr in the correct order?
eg:

    mov ah, valueForPtr
    mov bh, valueForCount
    mov edx, offset pointer_t_thing
    mov [edx], eax


If I have a fixed values for Ptr and Count, say Ptr is to be 1 and Count is to be zero,
then what do I move then ?

    mov edx, offset pointer_t_thing
    mov [edx], ((hi)<<16)+(lo)

Where Hi will be 1 and lo is zero. ie: 256 ??

Rgs, James.

zooba

Quote from: James Ladd on October 14, 2006, 02:07:19 AM
How can I move values into count and ptr in the correct order?
eg:

    mov ah, valueForPtr
    mov bh, valueForCount
    mov edx, offset pointer_t_thing
    mov [edx], eax


Either move in a word at a time or do some shifting (you're moving bytes at the moment):


mov ax, valueForCount
mov bx, valueForPtr
mov edx, offset pointer_t_thing
mov [edx], ax
mov [edx+2], bx



mov ax, valueForPtr
shl eax, 16
mov ax, valueForCount
mov edx, offset pointer_t_thing
mov [edx], eax


Quote from: James Ladd on October 14, 2006, 02:07:19 AM

    mov edx, offset pointer_t_thing
    mov [edx], ((hi)<<16)+(lo)

Where Hi will be 1 and lo is zero. ie: 256 ??

Substitute 'SHL' for '<<' and it should work fine (though you may need to specify the data size):

mov [edx], DWORD PTR ((hi SHL 16) + lo)

Cheers,

Zooba :U

James Ladd

Thanks.

Which is the quickest approach, snippet one or snippet two ?
(Snippet One doesnt do a shl eax but snippet two does)

James Ladd

Also with the code:

mov ax, valueForCount
mov bx, valueForPtr
mov edx, offset pointer_t_thing
mov [edx], ax
mov [edx+2], bx


Can't I just do

mov [edx], eax

since ax and bx are parts of EAX ?

TNick

Hi, James!
EAX - 32 bit register; low word can be accesed with AX or with AH (high byte of the low word) and AL (low byte of the low word)
EBX - 32 bit register; low word can be accesed with BX or with BH (high byte of the low word) and BL (low byte of the low word)
ECX - ...
EDX - ...

High word in a register can't be acceseed this way, but you can do this:
shr   eax,  16 (AX have now the high word from the old EAX; high word contain 0)
or
ror/rol eax, 16 (AX have now the high word from the old EAX; high word contain the old low word)

You also can push eax, then pop AX and BX to have the two words separated.

Regards,
Nick

Ratch

James Ladd,

     Here are a few points.  First of all, you are not working with a UNION structure item.  Isn't it nice that MASM does not have all that razzimatazz with words like 'volatile', 'short', 'unsigned', blah, blah?  You should always know what the length of your word is, its format, and whether is is volatile or not.  MASM does have some baggage words like TYPEDEF and PROC, which I never use, but it is up to you.  The code below shows how to do what you want.  A word of caution.  Using 16-bit instructions like MOV DX,5 will cause a 066H prefix to be inserted.  This causes the CPU to switch into 16-bit mode which greatly affects performance.  So avoid those instructions by using shifts if you can.  Assemble the code below and look at the output.  Ratch

@ EQU OFFSET

pointer_t STRUC
count BYTE ?
pter   BYTE ?        ;using the symbol 'ptr' for a label causes a error because it is a reserved word in MASM
pointer_t ENDS

  .data?
  BYTE 10 DUP (?)
yourstruc BYTE pointer_t DUP (?)

.CODE
MOV EDX,@ yourstruc
MOV AL,[yourstruc.pointer_t.count] ;access with address of structure
MOVZX EAX,[yourstruc.pointer_t.count]
MOV BL,[EDX.pointer_t.pter]        ;access with address in register, notice how much smaller the generated code is with a register

MOV AX,4                           ;try to avoid 16-bit instructions due to 66 prefix
POP AX                             ;try to avoid 16-bit instructions due to 66 prefix

PBrennick

Ratch,
Is there a push missing in that code? And if you are going to pop AX, mov AX, 4 is a wasted instruction as it will e overwritten.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Ratch

PBrennick,

QuoteIs there a push missing in that code? And if you are going to pop AX, mov AX, 4 is a wasted instruction as it will e overwritten.

     You are certainly correct.  But I was not trying to write anything meaningful.  Instead I was giving an example of a couple of 16-bit instructions that the CPU in 32-bit mode would burp over.  If the OP assembles the code, he would see the extra 066H prefix that should be avoided if possible.  Ratch

PBrennick

I see and your snippet certainly shows that point. :U

Paul
The GeneSys Project is available from:
The Repository or My crappy website

James Ladd