The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: vg on December 31, 2005, 10:46:19 PM

Title: 'typecasting' with masm32
Post by: vg on December 31, 2005, 10:46:19 PM
i'm trying to access a dword as 4 bytes (containing ARGB colors).

The variable containing the colors is declared as:
LOCAL color:DWORD

To access only a byte of the variable, i typed:
mov al,byte ptr [offset color]

But since the variable is on the stack, it's obviously an illegal operation.
So logically, this should do the trick:
mov al,byte ptr [ADDR color]

But it seems the "ADDR" keyword is reserved for calling with "invoke"

So here's what i finally did, Inside a loop:
xor eax,eax
lea edi,[color]
lodsb


(i know, lodsb is a now slow instruction, but i learned asm language during the DOS era... can't beat old habits. ;)  I don't feel like optimizing too much just now.)

The above code is ok, but i just bumped into the same problem again: accessing the word part of a DWORD... So i thought i'd ask: Is there a way to get only the offset of the variable against BP? I could hard-code it, knowing the position of the variable on the stack, but that isn't a very good solution...

Thanks!
Title: Re: 'typecasting' with masm32
Post by: hutch-- on January 01, 2006, 12:03:51 AM
It sounds like normal dereferencing.


    LOCAL var :DWORD
      ....
    mov var, 004488AAh  load the value in the memory operand "var"
    mov eax, var


Chop up EAX any way you like. Word in AX, byte in AL & AH.

To get the other end of EAX,


    rol eax, 16


Chop it up the same way.
Title: Re: 'typecasting' with masm32
Post by: vg on January 01, 2006, 12:24:37 AM
Instead of going through eax, i would like to access the byte directly from memory.
So for example, if "color" is a DWORD at address ebp+10, i would like to do:

mov al,[ebp+10]
mov al,[ebp+11]
mov al,[ebp+12]
mov al,[ebp+13]


Thanks again, and happy new year! :)
Title: Re: 'typecasting' with masm32
Post by: tenkey on January 01, 2006, 12:44:16 AM
This is all it takes...

mov al, byte ptr [color] ; low byte
mov al, byte ptr [color+1]
mov al, byte ptr [color+2]
mov al, byte ptr [color+3] ; high byte
mov ax, word ptr [color] ; low word
mov ax, word ptr [color+2] ; high word
Title: Re: 'typecasting' with masm32
Post by: vg on January 01, 2006, 01:02:04 AM
I thought i tried that already, but it works :)
Thanks!
Title: Re: 'typecasting' with masm32
Post by: GregL on January 01, 2006, 01:54:43 AM
vg,

You could also use a UNION to keep track of things.


ARGB UNION
    D DWORD ?
    STRUCT
      B BYTE ?
      G BYTE ?
      R BYTE ?
      A BYTE ?
    ENDS
ARGB ENDS

.CODE

    LOCAL color:ARGB
    LOCAL byAlpha:BYTE
    LOCAL byRed:BYTE
    LOCAL byGreen:BYTE
    LOCAL byBlue:BYTE

    mov eax, 004488AAh
    mov color.D, eax
   
    mov al, color.A
    mov byAlpha, al
   
    mov al, color.R
    mov byRed, al
   
    mov al, color.G
    mov byGreen, al
   
    mov al, color.B
    mov byBlue, al



Title: Re: 'typecasting' with masm32
Post by: vg on January 01, 2006, 04:15:53 AM
Unions are in masm32 too? very nice! All I can say is that i'm just starting using it and it feels like a very powerful assembler!  :U
Title: Re: 'typecasting' with masm32
Post by: MusicalMike on January 02, 2006, 07:57:04 PM
pnt label POINT

.code
...
push offset pnt
call SomeFunctionThatRequiresAPoint
...


and

x label dword
y label dword

.code
...
push offset x
call SomeFunctionThatRequiresAPoint
...


are exactly the same, because a data structure is nothing more than a reagon of memory where data is stored. The address of the POINT pnt is the same as the address of the first element of pnt which is in this case pnt.x. However the nicething about using the Struct facility of MASM is it saves typing, as does the Local macro which saves us the trouble of having to do indirrect addressing on the stack all day as well as the proc macro which saves us the trouble of function prefixes. As I always say, don't reinvent the wheel if you don't have to.