flat assembler
Message board for the users of flat assembler.

Index > Main > Where to start?

Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Author
Thread Post new topic Reply to topic
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 10 Apr 2012, 18:14
Can I increase an address of a "pointer" permanently? I have a piece of code, which is quite elegant and works well.



Code:
;=== ACQUIRE THE WINDOW HANDLES ================
  mov          ecx, 20                            ; Init the counter with 20
  xor          edx, edx                           ; Zero-out the helper index

.GetHandles:                                      ; Get all the handles
  invoke       GetStdHandle, -11                  ; -11 for STD_OUTPUT_DEVICE
  mov          [_textModeBuffer+edx], eax         ; Update the labels
  add          edx, 4
  loop         .GetHandles
    


But would it be possible to simply increase the address where _textModeBuffer points to? Could I do it like

Code:
  ;=== ACQUIRE THE WINDOW HANDLES ================
  mov          ecx, 20                            ; Init the counter with 20
  xor          edx, edx                           ; Zero-out the helper index

.GetHandles:                                      ; Get all the handles
  invoke       GetStdHandle, -11                  ; -11 for STD_OUTPUT_DEVICE
  lea          edx, [_textModeBuffer]             ; Load the effective address
  add          edx, 4                             ; Increase the address by 4 <=====
  mov          _textModeBuffer, edx               ; Load the modified address
  mov          [_textModeBuffer], eax             ; Update the labels
  loop         .GetHandles
    


I know it doesn't make any sense in this instance, but there are times when I really wish I could. Can it be done? I seem to misunderstand the lea instruction. I thought it loads the &_textModeBuffer into edx, and then increases that address.
Post 10 Apr 2012, 18:14
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 10 Apr 2012, 20:10
Hi there,

If I'm understanding you right, you're asking if you can fill an array (some consecutive values) with data, based on a pointer to the start of the array. If that's what you want, it would be something like this:

Code:
array rd 8 ; reserve 8 32-bit integers

...

mov ebx, array ; ebx is address of first item (you could also use lea ebx, [array])
mov ecx, 8 ; ecx is number of items

loop_start:

push ecx
call some_function ; some value is placed in eax
pop ecx

mov [ebx], eax ; mov eax into "what ebx points to"
add ebx, 4 ; increase ebx by size of dword (so it points to next element)
dec ecx ; decrease counter by 1
cmp ecx, 0 ; compare counter to 0
jnz loop_start ; if it's still non-zero, repeat

    


I wrote that from memory, I don't have access to a 32-bit windows. Hopefully it'll work.
Post 10 Apr 2012, 20:10
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 10 Apr 2012, 20:25
The question is why call it 20 times and put all the return values into array?
It will return the same value all 20 times, because you always pass -11.

The better way to put these values is to use EDI register and STOSD instruction, because this instruction stores EAX at EDI and adds 4 to EDI at the same time.
Post 10 Apr 2012, 20:25
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 10 Apr 2012, 20:28
Guru: You must've misunderstood the code. I get a new StdHandle each time I call it. -11 is a code for STD_OUTPUT_DEVICE.
I acquire an array of StdHandles and then set different colors for each of them. When I do it this way, I can actually have multiple colors in a console window. Yes, I see where you're coming from, but I barely got my feet wet and I can't really use stosd yet. Smile





Prophet: Actually, that's exactly it! It just got me confused for a little while. Thanks a lot Smile
Post 10 Apr 2012, 20:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 10 Apr 2012, 21:16
Inagawa wrote:
Code:
;=== ACQUIRE THE WINDOW HANDLES ================
  mov          ecx, 20                            ; Init the counter with 20
  xor          edx, edx                           ; Zero-out the helper index

.GetHandles:                                      ; Get all the handles
  invoke       GetStdHandle, -11                  ; -11 for STD_OUTPUT_DEVICE
  mov          [_textModeBuffer+edx], eax         ; Update the labels
  add          edx, 4
  loop         .GetHandles
    
This code can fail badly. STDCALL convention states that only registers EDI, ESI, EBX and EBP are preserved across API calls. You are using ECX and EDX and these registers can be clobbered by the invoke call to the API.
Post 10 Apr 2012, 21:16
View user's profile Send private message Visit poster's website Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 10 Apr 2012, 21:23
You've caught me with my pants down Embarassed I'll go fix it Smile

Code:
;=== ACQUIRE THE WINDOW HANDLES ================
  mov          ecx, 20                            ; Init the counter with 20
  mov          ebx, _textModeBuffer

.GetHandles:                                      ; Get all the handles
  push         ecx
  invoke       GetStdHandle, -11                  ; -11 for STD_OUTPUT_DEVICE
  mov          [ebx], eax                         ; Store the handle into [EBX]
  add          ebx, 4                             ; Add 4 bytes
  pop          ecx
  loop         .GetHandles
    


This should be okay, right?
Post 10 Apr 2012, 21:23
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 10 Apr 2012, 22:17
It is good -- ECX is OK now.
You should use debugger to check your code.
It is easy - try the following code:
Code:
invoke GetStdHandle, -11
mov edi, eax
invoke GetStdHandle, -11
mov esi, eax
invoke GetStdHandle, -11
mov edx, eax
int3
;
; At this point look in debugger at the registers: ESI, EDI, EDX, EAX.
; They all should contain the same value.
;
    

Run this code.
When debugger will stop at INT3 instruction -- look at the registers.
Post 10 Apr 2012, 22:17
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 10 Apr 2012, 22:18
del
Post 10 Apr 2012, 22:18
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 11 Apr 2012, 07:39
Well, my reasoning was that I simply get a handle. I never really thought about what happens.

Code:
;=== ACQUIRE THE WINDOW HANDLES ================
  mov          edx, _textModeBuffer
  invoke       GetStdHandle, -11                  ; -11 for STD_OUTPUT_DEVICE
  mov          ecx, 20                            ; Init the counter with 20

.GetHandles:                                      ; Get all the handles
  mov          [edx], eax                         ; Store the handle into [EDX]
  add          edx, 4                             ; Add 4 bytes
  loop         .GetHandles
    


You are probably pointing out that it's totally useless to call GetStdHandle 20 times if I can simply copy eax into the buffer 20 times instead. Am I right? You are completely right, I just never thought of it that way. Smile I'll go fix it now.

By the way, am I doing the color stuff right? I experimented and this is the only way that I know of to have multiple colors. Is there by any chance a better way?
Post 11 Apr 2012, 07:39
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 11 Apr 2012, 10:31
Well, you need only one handle to write to console.
Put it into a global variable and use it in all calls to API.
I am not sure why do you need 20 handles.

1. invoke SetConsoleCursorPosition and set the X,Y for the text to be printed.
2. invoke SetConsoleTextAttribute and set the color for the text to be printed.
3. invoke WriteConsole and this will print the text on screen at the X,Y
specified in step #1 and with color specified in step #2.

For all these calls you just need one global variable holding the handle returned by GetStdHandle API.
Make some more small functions to do all that.
For example, if you need to write part of text with one color and then
another part with different color - you do not need to set X,Y - you
want to continue writing text at the same location where the previous text ended.

I would written functions like:
GotoXY - to set the coordinates
PutText - to print text
SetColor - to set the color

Now, can you tell me what kind of a game you're planning?
Is that a full screen game? Cool stuff btw.
How do you plan to take input from the user?
Do you type the text into some command panel?
Do you use mouse to input the game controlling commands?
Post 11 Apr 2012, 10:31
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 11 Apr 2012, 10:47
Inagawa wrote:
By the way, am I doing the color stuff right? I experimented and this is the only way that I know of to have multiple colors. Is there by any chance a better way?


Hi Inagawa,

You need only one global handle like AsmGuru62 wrote.
One method is the following.
Code:
                format PE CONSOLE 4.0

                include "win32axp.inc"

.data
            macro enum [arg] {
                common
                count = 0
                forward
                arg = count
                count = count+1
                }

                enum BLACK, BLUE, GREEN, CYAN, RED, PURPLE, YELLOW,\
                     SYSTEM, GRAY, BRIGHTBLUE, BRIGHTGREEN, BRIGHTCYAN,\
                     BRIGHTRED, BRIGHTPURPLE, BRIGHTYELLOW, WHITE

                hOut dd ?
                dwTemp dd ?

.code
main:
                invoke GetStdHandle, STD_OUTPUT_HANDLE
                mov [hOut], eax

                stdcall setColor, BLUE, BRIGHTYELLOW
                ; print text
                stdcall setColor, RED, WHITE
                ; print text
                stdcall setColor, BLACK, SYSTEM
                ; print text

                invoke ExitProcess, 0



proc setColor uses ecx edx, BGColor, FGColor

                mov eax, [BGColor]
                shl eax, 4
                or eax, [FGColor]

                invoke SetConsoleTextAttribute, [hOut], eax
                ret
endp

.end main   
    


Idea There is a simple and clean example of a Win32 console application skeleton inside fresh. File->New application->Win32 console
Post 11 Apr 2012, 10:47
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 11 Apr 2012, 14:35
Great stuff, Picnic!
I think macro is better to combine colors:
Code:
macro SetColor bkColor, txColor
{
  pusha
       invoke  SetConsoleTextAttribute, [glb_HOutput], (bkColor shl 4) or txColor
  popa
}
    
Post 11 Apr 2012, 14:35
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 11 Apr 2012, 16:01
I have incorporated all of the code you have posted here into my program (which I am completely rewriting for the third time, since until Picnic posted the code, I had no idea how useful procedures are!), except the macro, because I absolutely don't understand macros, so I didn't want to complicate the code.
But guys, I am so thankful for the help, with these snippets of code you've helped me tremendously, I've learned lots of things from them. I am now able to print colors with one handle, just as you've shown me - excellent.

To Guru
Quote:
Now, can you tell me what kind of a game you're planning?

I have no idea what kind of a game. I need to be able to print actual graphics instead of the ASCII characters into the console. Also, the console seems to be limited in width, that is very bad and limiting. Maybe the graphics mode doesn't have that? I tried to use it, but the only way I found is via interrupts and that crashed the program.

Quote:
Is that a full screen game? Cool stuff btw.

Depends.. I have no idea how console behaves in fullscreen and how does it cope with the width limit of 79. And thanks Razz

Quote:
How do you plan to take input from the user?

Same as before. If I have the graphics mode, I'll make it mouse and shortcuts controlled.


Quote:
Do you type the text into some command panel?

What do you mean? If the player inputs his decisions into a "terminal"? Well that was the original thought.

I beg you to help me with the graphics mode, guys.. If you could check on youtube the game "Dwarf Fortress" (Link), is it made with console and graphics mode? Or rather, could I make a game like that in console's graphics mode? I've never seen how it looks, so I have no idea what to expect. (Or maybe it would be easier to just use OpenGL?)
Post 11 Apr 2012, 16:01
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 11 Apr 2012, 18:31
Ah... I see... the console is for text characters only.
To do what you want (???-CRAFT type of game) -- you'll need to
create a full blown Windows GUI application and that will allow for
some sprites and graphics to be painted.

Wow, that is like 25 times more to learn than in console mode.
I'll advise to read some Windows API tutorials first - if you never coded for Windows (I presume you never did).

You know, if I have some time -- I could write you a small sample of what I think you're trying to do.
Post 11 Apr 2012, 18:31
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 11 Apr 2012, 19:05
Ouch, 25 times more painful than so far doesn't sound good. If you could write me a sample I'd be
glad, but I don't think I will be able to really use it to create anything at my current skill level. So if you
think I won't be able to use it, please don't bother with it.

It's just that coding for console is really just to learn something and the fact that you can't really make
a game is kinda demotivating. Well a game sure, but certainly not a game that I'd like to work on for
the next couple of years as I learn assembly. Very Happy
Post 11 Apr 2012, 19:05
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 11 Apr 2012, 20:38
I have a good coding style.
Smile
Post 11 Apr 2012, 20:38
View user's profile Send private message Send e-mail Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 12 Apr 2012, 14:56
Here is an example of how to get console input if still interested in doing console:

http://files.sys5.se/huvaligen.zip

Image

_________________
This is a block of text that can be added to posts you make.
Post 12 Apr 2012, 14:56
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 12 Apr 2012, 16:41
Of course I am still interested, only a little discouraged. Thanks a lot, I will look into it ASAP.
Post 12 Apr 2012, 16:41
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 12 Apr 2012, 17:47
So, how exactly you can add graphics elements into a console?
Post 12 Apr 2012, 17:47
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 12 Apr 2012, 18:31
If you are asking me, this is the Thread that even gave me the idea that it can be done. I am not able to make it work, though.
Post 12 Apr 2012, 18:31
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next

< 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.