flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > [solved]how casting a *dword to *word works?

Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 22 Feb 2018, 20:34
I need to do something like this:

dword* pointer = (dword*)malloc(1024);
*pointer++ = 1;
*pointer++ = 4;
*(word)pointer++ = 1;
*(word)pointer++ = 2;
*(word)pointer++ = 3;
*(word)pointer++ = 4;

how?


Last edited by vivik on 25 Feb 2018, 06:16; edited 1 time in total
Post 22 Feb 2018, 20:34
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 22 Feb 2018, 22:36
vivik wrote:
I need to do something like this:

dword* pointer = (dword*)malloc(1024);
*pointer++ = 1;
*pointer++ = 4;
*(word)pointer++ = 1;
*(word)pointer++ = 2;
*(word)pointer++ = 3;
*(word)pointer++ = 4;

how?

Since the question is asked in “High Level Languages” section of the forum, I’ll ask what language do you want to use.
Post 22 Feb 2018, 22:36
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 23 Feb 2018, 00:10
dword == long I presume? And word == short?

I think a good way to see what your compiler does with that code it to build it and then debug/disassemble it to see what it is doing.
Post 23 Feb 2018, 00:10
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 23 Feb 2018, 05:15
Perhaps union?
Post 23 Feb 2018, 05:15
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 23 Feb 2018, 06:29
This worked:

*(unsigned short*)g_texture_thumb_cursor = 0xF1F2;
g_texture_thumb_cursor = (DWORD*)((unsigned short*)g_texture_thumb_cursor + 1);

Not very readable, huh? Had to google "void pointer increment".
Post 23 Feb 2018, 06:29
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 23 Feb 2018, 06:46
I think I'll use these types:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx

BYTE
WORD
DWORD


INT8
INT16
INT32
UINT8
UINT16
UINT32
Post 23 Feb 2018, 06:46
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 23 Feb 2018, 06:59
vivik wrote:
I think I'll use these types:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx

BYTE
WORD
DWORD


INT8
INT16
INT32
UINT8
UINT16
UINT32

Might not be a good idea in terms of portability.
Post 23 Feb 2018, 06:59
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 23 Feb 2018, 07:13
vivik wrote:
This worked:

*(unsigned short*)g_texture_thumb_cursor = 0xF1F2;
g_texture_thumb_cursor = (DWORD*)((unsigned short*)g_texture_thumb_cursor + 1);

Not very readable, huh? Had to google "void pointer increment".
So your compiler need all that ugliness just to do this:
Code:
g_texture_thumb_cursor dw 0xF1F2,0

inc dword[g_texture_thumb_cursor]    
Ugh.
Post 23 Feb 2018, 07:13
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 24 Feb 2018, 08:24
Actually, it's much more simple if I just use char*.

char* pointer = (char*)malloc(1024);

*(INT32*)pointer = 1;
pointer += 4;

*(INT32*)pointer = 2;
pointer += 4;

*(UINT16*)pointer = 1;
pointer += 2;
*(UINT16*)pointer = 2;
pointer += 2;
*(UINT16*)pointer = 3;
pointer += 2;
*(UINT16*)pointer = 4;
pointer += 2;
Post 24 Feb 2018, 08:24
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 24 Feb 2018, 09:39
vivik wrote:
Actually, it's much more simple if I just use char*.

char* pointer = (char*)malloc(1024);

*(INT32*)pointer = 1;
pointer += 4;

*(INT32*)pointer = 2;
pointer += 4;

*(UINT16*)pointer = 1;
pointer += 2;
*(UINT16*)pointer = 2;
pointer += 2;
*(UINT16*)pointer = 3;
pointer += 2;
*(UINT16*)pointer = 4;
pointer += 2;

Why not define a struct and cast the pointer?
Post 24 Feb 2018, 09:39
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 24 Feb 2018, 13:45
>Why not define a struct and cast the pointer?

Indeed why... I can merge the last four UINT16 into one struct at least. (Can't make everything into one structure, because the second INT32 is the amount of those UINT16.)

I need to compare the assembly, later.
Post 24 Feb 2018, 13:45
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.