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
dancho



Joined: 06 Mar 2011
Posts: 74
dancho 15 Apr 2012, 17:50
ok,

Code:
; data
PosX dw 10
; clear ecx 
xor ecx,ecx 
; copy data to low word
mov cx,[PosX]
; print data
cinvoke printf,<'%u'>,ecx
; although we are using only cx but func expect dword size we had to clear high word of ecx register not to affect data we are printing...
    
Post 15 Apr 2012, 17:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 15 Apr 2012, 22:25
Code:
movzx ecx,word[PosX]
;...    
Post 15 Apr 2012, 22:25
View user's profile Send private message Visit poster's website Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 16 Apr 2012, 15:51
Yes, I do understand this now. We have a 32 bit variable from which only the low word is relevant. We have to make the high word
zero, else it affects what is printed in the console, since printf reads both words and interprets them as a single value. I just had big
trouble understanding that even though I use CX, the mess in high word is still there and making my output weird.
Thanks a lot for making me understand.
Post 16 Apr 2012, 15:51
View user's profile Send private message Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 19 Apr 2012, 15:44
Hello guys. I have a problem, but I cannot find where exactly the problem is. I have this code (In my code I have commented every last single line of code, so you really see the whole program here):

Code:
format PE console

entry Main

;=============================================================================
include 'WIN32AXP.inc'

;=============================================================================
section '.data' data readable writeable

  _yesWindowHandle           dd ?
  _testHandle                dd ?

;=============================================================================
section '.text' code executable readable

Main:

  ;=== INITIALIZATION ============================
  stdcall      !Window.Create, _yesWindowHandle

  mov          [_testHandle], eax
  cinvoke      printf, <'TEST: %p', 10>, [_testHandle]
  cinvoke      printf, <'YES :%p', 10>, [_yesWindowHandle]

  ;=== KEEP THE CONSOLE OPEN =====================
  invoke       GetStdHandle, STD_INPUT_HANDLE
  invoke       ReadConsoleA, eax, _inBuffer, 1, _ioTemp, 0 ; Leave console open
  invoke       ExitProcess, 0

proc !Window.Create, Handle:DWORD

  invoke       VirtualAlloc, 0, 80, MEM_COMMIT+MEM_RESERVE, PAGE_READWRITE
  mov          [Handle], eax

  ret
endp

;============================================================================
section '.idata' import data readable writeable

     library kernel32,'KERNEL32.DLL',\
             user32,'USER32.DLL',\
             gdi32,'GDI32.DLL',\
             advapi32,'ADVAPI32.DLL',\
             comctl32,'COMCTL32.DLL',\
             comdlg32,'COMDLG32.DLL',\
             shell32,'SHELL32.DLL',\
             wsock32,'WSOCK32.DLL',\
             msvcrt, 'MSVCRT.DLL'

     import_kernel32
     import_user32
     import_gdi32
     import_advapi32
     import_comctl32
     import_comdlg32
     import_shell32
     import_wsock32

     import msvcrt,\
            printf, 'printf'
    


What am I overlooking? _testHandle has a value, but _yesWindowHandle is always zeroed-out. Confused

What I think is that I'm actually using the adress of stack that gets later thrashed, instead of an address of the value I wanted to. Am I right? How to fix this? How do I return the address?
Post 19 Apr 2012, 15:44
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1417
Location: Piraeus, Greece
Picnic 19 Apr 2012, 17:02
Inagawa wrote:
What am I overlooking?


Code:
  mov edx, [Handle]
  mov [edx], eax   
    
Post 19 Apr 2012, 17:02
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1709
Location: Toronto, Canada
AsmGuru62 19 Apr 2012, 17:05
If all these are global variables -- just write straight into the variables.
Post 19 Apr 2012, 17:05
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 19 Apr 2012, 17:06
Thanks a lot! I have tried it by using lea, but it doesn't work.. Why? Shouldn't it do the same thing?

Guru: I cannot. The system is dynamic and I would have no way to know the name of the variable.
Post 19 Apr 2012, 17:06
View user's profile Send private message Reply with quote
dancho



Joined: 06 Mar 2011
Posts: 74
dancho 19 Apr 2012, 17:48
it is just like Picnic said...

Code:
; in data section
yesWindowHandle dd 0
;
; test before
mov edx,[yesWindowHandle]
;
; argument is address in memory to some data
stdcall WindowCreate,yesWindowHandle 
;
;test after
mov edx,[yesWindowHandle]

;proc definition
proc WindowCreate,handleYesNo
;
;copy address to eax
mov eax,[handleYesNo]
;eax is now pointer to that data
;and to write to that memory address we need 
;to place eax between brackets
mov dword[eax],10
;this is how fasm handles all pointers...
ret
endp

    


btw if some data is global you dont pass it as argument,really no need...
so in your example :

Code:
proc !Window.Create, Handle:DWORD 
invoke VirtualAlloc, 0, 80, MEM_COMMIT+MEM_RESERVE, PAGE_READWRITE
; CHECK return value !!! 
mov edx,[Handle]
mov [edx],eax 
  ret 
endp    


and this is how I would do:
Code:
; global var
pMem dd 0
; array size 
MEM_SIZE = 20
; element size dword ( 4 bytes )
invoke VirtualAlloc,0,MEM_SIZE*4,MEM_COMMIT or MEM_RESERVE,PAGE_READWRITE
.if eax=0
        ; print some error msg
      ; ret some error code
       ret
.endif
; else
; save base addres for later use
mov [pMem],eax
;
; LATER in some function
;
; lets say I need to write to data element 10.
; remember we have here zero based array
; save ebx
push ebx
; index to the array
mov ecx,9
mov ebx,[pMem]
; save data 
; dword size
mov dword[ebx+ecx*4],1
; balance stack
pop ebx

; etc
    


btw you may ask why did I zeroed pMem,well simple really,when your program close and if you had failed at allocation some memory and you have only something like this :
Code:
invoke VirtualFree,[pMem],0,MEM_RELEASE
    

program will crash because [pMem] is zero and no memory is allocated at the first place,so you do some checking first:
Code:
.if [pMem]<>0
invoke VirtualFree,[pMem],0,MEM_RELEASE
.endif
    

so if allocation failed for any reason do nothing,
else free that memory...
Post 19 Apr 2012, 17:48
View user's profile Send private message Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 19 Apr 2012, 23:03
Seems like a neat trick, I'll use it to destroy the windows, thanks.

My windows are 'fully' functional. They can be moved and resized in all four directions. Making it into eight directions as was the original plan would be extremely tedious at this point and I would learn nothing. Program is only code controlled now.

I don't think I'll be doing mouse controls, maybe I might. I am really sick of the console as you can imagine by now, but at least I completed a big chunk of the work without bailing out.

Here's a screen:

Image
Post 19 Apr 2012, 23:03
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1709
Location: Toronto, Canada
AsmGuru62 20 Apr 2012, 13:52
8 directions? Are we in alternate reality?!.. Smile
Post 20 Apr 2012, 13:52
View user's profile Send private message Send e-mail Reply with quote
dancho



Joined: 06 Mar 2011
Posts: 74
dancho 20 Apr 2012, 16:24
@Inagawa
my advice would be to drop console programming , download psdk and start with Iczelion's tutorials ( there is a link to fasm port somewhere in forum ,
though masm version is easily readable too )...
start with 'Tutorial 3: A Simple Window ',read it carefully,look for function explanation in sdk and dont be afraid to ask questions...

lol I wrote this and THEN I read this
nvm going to that one...
Post 20 Apr 2012, 16:24
View user's profile Send private message Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 04 May 2012, 20:43
I'll post my question here, so I don't needlessly start another thread. How do I output 64bit variables?
Let's say I have a code like this:


testNum dq 0

mov DWORD[testNum], 2000000000
adc DWORD[testNum+4], 2000000000

How would I output it?
Post 04 May 2012, 20:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 05 May 2012, 00:43
Output it to where? In what format? Do you mean printing the decimal value in ASCII?
Post 05 May 2012, 00:43
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1417
Location: Piraeus, Greece
Picnic 05 May 2012, 06:01
probably the easiest way is to use wsprintf (supports the 64-bit modifiers in Windows XP or above)

Code:
        ; "%I64d" for signed 64-bit integers
        ; "%I64u" for unsigned 64-bit integers

        cinvoke wsprintf, lpBuffer, "%I64u",  double [testNum]  
    
Post 05 May 2012, 06:01
View user's profile Send private message Visit poster's website Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 05 May 2012, 08:25
Rev: Yes, exactly. Printing a 64bit variable into console with the floating point (e.g. 12.1248)

Thanks, Pic - I have a problem though.

Code:
  
  __Average                 dd 0
  __Frequency               dq 0 
  __TimeMicro               dq 0 
  __TimeBuffer              dq 0

  ;
  ; I have an FPU code like this
  ;
  finit
  fild         [__Average]
  fild         [__Frequency]
  mov          DWORD[__TimeMicro], 1000
  fild         DWORD[__TimeMicro]
  fdivp
  fdivp
  fstp         DWORD[__TimeMicro]

  cinvoke      wsprintfA, __TimeBuffer, "%I64u", DWORD [__TimeMicro]
  cinvoke      printf, __TimeBuffer

    


When FISTP-ing into the TimeMicro, I get 12. When FSTP-ing, I get 1094291185. Even if the floating point was present, 10.9 is a different result. Could you please show me how to fix this? I need to store the double float in memory and then output it.

Maybe it's my FPU code that is at fault? I wouldn't be really surprised, I haven't had much time to look into doing floating point yet.
Post 05 May 2012, 08:25
View user's profile Send private message Reply with quote
dancho



Joined: 06 Mar 2011
Posts: 74
dancho 05 May 2012, 08:34
Post 05 May 2012, 08:34
View user's profile Send private message Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 05 May 2012, 09:02
Thanks for this, I'll read through it. But from what I can tell so far, if FISTP stores 12, then FSTP should store something like 11.4897 or some other value. OllyDbg actually shows me that ST0 has the value 11.5973 at the end. I don't know, I'll go read through it, maybe there's the answer just waiting for me
Post 05 May 2012, 09:02
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1709
Location: Toronto, Canada
AsmGuru62 05 May 2012, 10:45
FISTP rounds the value.
Post 05 May 2012, 10:45
View user's profile Send private message Send e-mail Reply with quote
Inagawa



Joined: 24 Mar 2012
Posts: 153
Inagawa 05 May 2012, 10:48
But why does FSTP store 1094291185? What is that?
Post 05 May 2012, 10:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 05 May 2012, 10:57
Inagawa wrote:
But why does FSTP store 1094291185? What is that?
The format of floating point numbers is different from integers.
Post 05 May 2012, 10:57
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:  
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.