flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > Windows Console API Functions and Console Buffer

Author
Thread Post new topic Reply to topic
Nyatta



Joined: 23 Feb 2019
Posts: 3
Hello all,

I'm fairly new to assembly programming and have only dabbled here and there over the past 5-6 years. I understand the basic-basics, so please bear with me.

That aside, I've been attempting to invoke console API functions the past few days with limited success - specifically SetConsoleWindowInfo and SetConsoleScreenBufferSize in an attempt to remove the scroll bar from the console window in order to display and repeatedly update a neat block of text.
I'd also ran into unexpected behavior attempting to move the cursor with SetConsoleCursorPosition where I'd had to assign COORD as a quad value instead of the documented word value. The only way I'd been able to pass the value correctly has been as a hex quad whereas two decimals would be optimal.

These are the pages I'd been using for reference:
https://docs.microsoft.com/en-us/windows/console/setconsolewindowinfo
https://docs.microsoft.com/en-us/windows/console/setconsolescreenbuffersize
https://docs.microsoft.com/en-us/windows/console/setconsolecursorposition

My code is as follows:
Code:
format PE64 GUI 5.0
include 'win64ax.inc'

.code
  start:
        invoke  AllocConsole
                invoke  GetStdHandle, STD_OUTPUT_HANDLE                                 ;Get Console Output Handle
                mov             [console],      EAX                                                                     ;Store Handle
                invoke  GetConsoleWindow, 0                                                             ;Get Window Handle
                mov             [window], EAX                                                                   ;Store
        invoke  WriteConsole, [console], buffer, 9600, 0                ;Write Stored Message to Output Buffer
                invoke  SetConsoleCursorPosition, [console], 0                  ;Set Cursor to Top to Scroll Up
                invoke  SetConsoleCursorPosition, [console], 0x0028003C ;Center Cursor in Console
                invoke  SetConsoleScreenBufferSize, [console], [scale]  ;Adjust buffer size?
                invoke  SetConsoleWindowInfo, [console], 1, scale               ;Adjustment 2?
        invoke  Sleep,-1                                                                                ;Remain Open
.end start

.data
        console dd 0
        window  dd 0
        scale   dd 0x00007D50
        buffer  db 9600 dup (" ")                                                                       ;Placeholder    

Any suggestions or obvious errors?
Post 01 May 2019, 13:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16702
Location: In your JS exploiting you and your system
I assume your problem is in creating the dword value from two word values?

There are a number of ways it can be done.Here is one way.
Code:
x = 40
y = 60
mov eax,x shl 16 + y    
Post 01 May 2019, 13:50
View user's profile Send private message Visit poster's website Reply with quote
Nyatta



Joined: 23 Feb 2019
Posts: 3
Code:
mov             ax, [curposy]
shl             eax, 16
add             ax, [curposx]
invoke  SetConsoleCursorPosition, [console], eax    

Awesome, thank you!

A questions regarding that: is there any way to calculate the result of that algorithm upon compilation in order to bake it in as an immediate value and a stored value? Terminology/Vocabulary has been a killer for my Google queries.
Something like:
Code:
x dw 40
y dw 60
xy dd y shl 16 + x    

Edit: I was under the impression that by defining two values side-by-side, say two word values, I could simply call the first value as a double to retrieve both. Upon compilation are the sections in my code block that attempt to access that data being set to only access a word as part of the opcode itself due to the value being defined as a word within my data section? Is there a way to define I am retrieving a double from a word stored in the data block?
Post 01 May 2019, 20:42
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 222
to simplify things, and an aim for a better readability use structures.

Code:
.code
; ...
mov [crd.x],28h
mov [crd.y],3Ch

invoke SetConsoleCursorPosition,[console],[crd]
; ...

.data
crd COORD    


define coord struct anywhere, like so:
Code:
struct COORD
x dw ?
y dw ?
ends    

_________________
Asm For Wise Humans
Post 02 May 2019, 00:11
View user's profile Send private message Reply with quote
Nyatta



Joined: 23 Feb 2019
Posts: 3
Ah, that's great to know!
I'm employed that as follows:
Code:
format PE64 GUI 5.0
include 'win64ax.inc'

.code
  start:
        invoke  AllocConsole
        invoke  GetStdHandle, STD_OUTPUT_HANDLE
        mov     [console],      EAX
        invoke  GetConsoleWindow, 0
        mov     [window], EAX
        invoke  WriteConsole, [console], buffer, 9600, 0
        invoke  SetConsoleCursorPosition, [console], 0
        mov     EAX, [curs]
        invoke  SetConsoleCursorPosition, [console], EAX
        invoke  Sleep,-1
.end start

.data
        struct  COORD
                y       dw 3Ch
                x       dw 28h
        ends
        curs    COORD
        
        console dd 0
        window  dd 0
        buffer  db 9600 dup (" ")    

My next question, I read the structure above as defining two words, whereas before I defined a single word containing the data of the two doubles... What is the structure actually doing between defining it an moving it into a register? Assigning x and y as doubles results in only the x coordinate being entered when the coordinate is stored this way.
Post 03 May 2019, 20:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16702
Location: In your JS exploiting you and your system
To make your COORD structure more flexible you can try this:
Code:
        struct  COORD
                y       dw ?
                x       dw ?
        ends
        curs    COORD 40, 60
        curs2   COORD 30, 70
    
This is functionally the same as doing this:
Code:
curs rd 0
curs.y dw 60
curs.x dw 40
curs2 rd 0
curs2.y dw 70
curs2.x dw 30    
Then when you want to access the data do this:
Code:
mov eax,[curs]
invoke function, [curs2]
mov bx,[curs2.y]    
Post 04 May 2019, 02:47
View user's profile Send private message Visit poster's website 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-2019, Tomasz Grysztar.

Powered by rwasa.