News:

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

'typecasting' with masm32

Started by vg, December 31, 2005, 10:46:19 PM

Previous topic - Next topic

vg

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!

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

vg

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! :)

tenkey

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
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

vg

I thought i tried that already, but it works :)
Thanks!

GregL

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




vg

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

MusicalMike

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.