NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: HD1920.1 on May 07, 2014, 05:52:03 PM

Title: GetCursorPos does not return y coordinate
Post by: HD1920.1 on May 07, 2014, 05:52:03 PM
Hello guys, I have just another problem. When I try to get the current cursor position with GetCursorPos and display the result with printf, only the x coordinate is shown. The y coordinate is zero. Where is my mistake? Code:
Code: [Select]
struc POINT
.x_coord: resq 1
.y_coord: resq 1
.limit:
endstruc

extern GetCursorPos
extern printf
extern exit
global Start

section .text
Start:
sub rsp, 0x28

mov rcx, Cursor
call GetCursorPos

mov rcx, format
mov rdx, [Cursor + POINT.x_coord]
mov r8, [Cursor + POINT.y_coord]
call printf

xor rcx, rcx
call exit
add rsp, 0x28
ret

section .data
Cursor: istruc POINT
iend
format: db "Cursor is at (%u, %u)", 10, 0

Title: Re: GetCursorPos does not return y coordinate
Post by: Gunner on May 09, 2014, 01:17:03 AM
Look at the definition of POINT:
Code: [Select]
typedef struct tagPOINT {
  LONG x;
  LONG y;
} POINT, *PPOINT;
A LONG is 32 bits in size, a LONG LONG is 64 bits in size, you are declaring your structure improperly.

You create your structure with qwords:
Code: [Select]
struc POINT
.x_coord: resq 1
.y_coord: resq 1
.limit:
endstruc

Instead, it should be with DWORDS:
Code: [Select]
struc POINT
.x_coord: resd 1
.y_coord: resd 1
.limit:
endstruc
Title: Re: GetCursorPos does not return y coordinate
Post by: HD1920.1 on May 09, 2014, 02:34:39 PM
Thank you very much, now it works. I thought that I have to use qwords because I'm programing in 64 bits.
Title: Re: GetCursorPos does not return y coordinate
Post by: Rob Neff on May 09, 2014, 03:42:12 PM
Thank you very much, now it works. I thought that I have to use qwords because I'm programing in 64 bits.

Nope.  You'll find many structure definitions for 64-bit operating systems contain 32/16/8 bit sized members.  BUT ( there's always a but ) YOU need to ensure the proper alignment of the data members when defining the structure itself.