flat assembler
Message board for the users of flat assembler.

Index > Windows > Windows 32 Program Issue (Easy)

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Hello, I am new to assembly, so this should be an easy fix. I am trying to have a user input letter(s), which will then be stored in register ax. Then, after the user hits enter, ax will output its data, or what the user entered. As of now, the program only opens and then closes before the user can write in it: nothing else happens. What is the error here!? Thanks in advance for your help Very Happy


Code:
format binary as "exe"               ; Tell FASM to make an exe
input:
entr=input
mov [entr],ax
message db "entr"
    
Post 05 Mar 2012, 21:24
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
Is that thing compiles?!...
I wrote a small console program as an example:
Code:
; ---------------------------------------------------------------------------
; FILE: Conio.Asm
; DATE: March 5, 2012
; ---------------------------------------------------------------------------

format  PE CONSOLE 4.0
entry   start

    include 'Win32A.Inc'

; ---------------------------------------------------------------------------
section '.data' data readable writeable

    nChars    dd 0
    invite    db '--- COMMAND: ',0
    strExit   db 'EXIT',0Dh,0Ah,0
    response  rb 32

; ---------------------------------------------------------------------------
section '.code' code readable executable

; ---------------------------------------------------------------------------
; PROGRAM ENTRY POINT
; ---------------------------------------------------------------------------
align 32
start:
    ;
    ; Load into EBX a handle to screen buffer
    ;
    invoke    GetStdHandle, STD_OUTPUT_HANDLE
    mov       ebx, eax
    ;
    ; Prompt user to enter a command using LIGHT GREEN color
    ;
.ask_again:
    invoke    SetConsoleTextAttribute, ebx, 0Ah
    invoke    WriteConsole, ebx, invite, 13, nChars, 0
    ;
    ; Now, wait for a user to type in a string and hit ENTER
    ;
    invoke    SetConsoleTextAttribute, ebx, 0Fh
    invoke    GetStdHandle, STD_INPUT_HANDLE
    mov       edi, response
    invoke    ReadConsole, eax, edi, 32, nChars, 0
    ;
    ; Terminate entered text with zero byte
    ;
    mov       eax, [nChars]
    mov       byte [edi + eax], 0
    ;
    ; Check if command entered is "EXIT".
    ; If not -- ask again!
    ;
    invoke    lstrcmpi, response, strExit
    test      eax, eax
    jnz       .ask_again
    ;
    ; "EXIT" entered, so stop monkeying around!
    ;
    invoke    ExitProcess, 0

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

    library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL'

    include 'API\Kernel32.Inc'
    include 'API\User32.Inc'
    include 'API\Gdi32.Inc'
    
Post 06 Mar 2012, 03:30
View user's profile Send private message Send e-mail Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
That is where I was heading with the program, thanks! I may use that as a code to build upon.

As for my small code, why does it not wait for an entry and then state what was entered? How can I fix that?
Post 06 Mar 2012, 15:35
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
Quote:

format binary as "exe"

This way you don't generate an executable but a binary file with file extention .exe

To run on windows you should generate a Portable Executable file format like ASMGuru did.
Post 06 Mar 2012, 16:23
View user's profile Send private message Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Okay, I will compile it that way. Is that the only issue? Or did I just write in psuedo-code?

If I did write in psuedo-code, what would be the best way to correct my coding for writing other codes?

And thanks for the responses so far!
Post 06 Mar 2012, 17:21
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
@drewtoby: when you say "other codes" -- that is too wide a definition.
You see, the programming is kind of precise science - especially in Assembler language.
So, what exactly do you mean by "other codes"?

Win API provides a way to create TWO types of EXE files:
1. Console application (which has a form I posted)
2. Window (GUI based) application. Example of which you can see in
an archive provided with FASM - it is in folder called EXAMPLES
and the name of it is a TEMPLATE if I remember correctly.
Post 06 Mar 2012, 17:37
View user's profile Send private message Send e-mail Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
I mean the needed codes that it will take to not have the console application crash from a lack of info, even if it does compile.

I tryed playing around with your posted code, and, as usual, I can get it to compile but not run (with my changes). I always get an "app crash" error when run in windows. But your code, as posted, runs just fine =)

-----------------------------------------------------------------------------------------------
I guess I am just asking how can I make a program that will not crash when I run it.
-----------------------------------------------------------------------------------------------

p.s.-I will check out the GUI application too, thanks.
Post 06 Mar 2012, 22:47
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
drewtoby wrote:


-----------------------------------------------------------------------------------------------
I guess I am just asking how can I make a program that will not crash when I run it.
-----------------------------------------------------------------------------------------------


Don't beat yourself down we all came from there
Post 07 Mar 2012, 01:29
View user's profile Send private message Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Okay, thanks for your kind words Smile I'll keep trying and will post again soon, I am sure!!!!
Post 07 Mar 2012, 02:22
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
Can you post the 'crashing' code?
Post 07 Mar 2012, 15:09
View user's profile Send private message Send e-mail Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Code:
format  PE CONSOLE 4.0



include 'E:/fasmw16940/INCLUDE/Win32A.Inc'



;----------------------------------------------------------------------
        section '.data' data readable writeable

filename  db 'test.txt',0
RichEditDLL    db 'riched20.DLL', 0
hRichEditDLL   dd ?
vv             dd ?
Input         dd 0
welcome       db 'Welcome. Please input letters to be written to test.txt ' ,0
response  rb 32



;-----------------------------------------------------------------------
        section '.code' code readable executable
Start:


invoke    GetStdHandle, STD_OUTPUT_HANDLE  ;calls libaries
invoke LoadLibrary, RichEditDLL
mov    [hRichEditDLL],eax
invoke GetModuleHandle,0
mov    [vv],eax

align 32

    invoke    SetConsoleTextAttribute, ebx, 0Ah 
    invoke    WriteConsole, ebx, welcome, 13, Input, 0
    ; 
    ; Now, wait for a user to type in a string and hit ENTER 
    ; 
    invoke    SetConsoleTextAttribute, ebx, 0Fh 
    invoke    GetStdHandle, STD_INPUT_HANDLE 
    mov       edi, response 
    invoke    ReadConsole, ecx, edi, 32, Input, 0



;--------------------------------------------------------------------------
proc StreamIn   Input   ;write input to test.txt

        invoke  WriteFile,[Input],filename,0

 endp

;-------------------------------------------------------------------8-------

 mov       eax, [Input]  ;clears command/text
 mov       byte [edi + ecx], 0

;---------------------------------------------------------------------------


Exit:

invoke    ExitProcess,0




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

    library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL'
    include 'API\Kernel32.Inc'
    include 'API\User32.Inc' 
    include 'API\Gdi32.Inc'
    


It's a mix of your code above and a posted rich edit code. It will open, then windows will post app crash =(
Post 08 Mar 2012, 01:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17472
Location: In your JS exploiting you and your system
revolution
You left out the setting of ebx
Code:
    invoke    GetStdHandle, STD_OUTPUT_HANDLE
    mov       ebx, eax ;<--- you need this line, else the handle in eax is lost.    
Post 08 Mar 2012, 02:06
View user's profile Send private message Visit poster's website Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Corrected and still crashes =( Thanks though!

Code:
format  PE CONSOLE 4.0



include 'E:/fasmw16940/INCLUDE/Win32A.Inc'



;----------------------------------------------------------------------
        section '.data' data readable writeable

filename  db 'test.txt',0
RichEditDLL    db 'riched20.DLL', 0
hRichEditDLL   dd ?
vv             dd ?
Input         dd 0
welcome       db 'Welcome. Please input letters to be written to test.txt ' ,0
response  rb 32



;-----------------------------------------------------------------------
        section '.code' code readable executable
Start:


invoke    GetStdHandle, STD_OUTPUT_HANDLE  ;calls libaries
mov       ebx, eax ;<--- you need this line, else the handle in eax is lost.
invoke LoadLibrary, RichEditDLL
mov    [hRichEditDLL],ebx
invoke GetModuleHandle,0
mov    [vv],eax

align 32

    invoke    SetConsoleTextAttribute, ebx, 0Ah 
    invoke    WriteConsole, ebx, welcome, 13, Input, 0
    ; 
    ; Now, wait for a user to type in a string and hit ENTER 
    ; 
    invoke    SetConsoleTextAttribute, ebx, 0Fh 
    invoke    GetStdHandle, STD_INPUT_HANDLE 
    mov       ebx, eax ;<--- you need this line, else the handle in eax is lost.
    mov       edi, response
    invoke    ReadConsole, ecx, edi, 32, Input, 0



;--------------------------------------------------------------------------
proc StreamIn   Input   ;write input to test.txt

        invoke  WriteFile,[Input],filename,0

 endp

;-------------------------------------------------------------------8-------

 mov       eax, [Input]  ;clears command/text
 mov       byte [edi + ecx], 0

;---------------------------------------------------------------------------


Exit:

invoke    ExitProcess,0




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

    library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL'
    include 'API\Kernel32.Inc'
    include 'API\User32.Inc' 
    include 'API\Gdi32.Inc'
    
Post 08 Mar 2012, 02:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17472
Location: In your JS exploiting you and your system
revolution
You changed this line:
Code:
mov    [hRichEditDLL],eax ;You need the returned value from LoadLibrary in eax here    
Post 08 Mar 2012, 03:02
View user's profile Send private message Visit poster's website Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Changed: still app crash...

Code:
format  PE CONSOLE 4.0



include 'E:/fasmw16940/INCLUDE/Win32A.Inc'



;----------------------------------------------------------------------
        section '.data' data readable writeable

filename  db 'test.txt',0
RichEditDLL    db 'riched20.DLL', 0
hRichEditDLL   dd ?
vv             dd ?
Input         dd 0
welcome       db 'Welcome. Please input letters to be written to test.txt ' ,0
response  rb 32



;-----------------------------------------------------------------------
        section '.code' code readable executable
Start:


invoke    GetStdHandle, STD_OUTPUT_HANDLE  ;calls libaries
mov       ebx, eax ;<--- you need this line, else the handle in eax is lost.
invoke LoadLibrary, RichEditDLL
mov    [hRichEditDLL],eax ;You need the returned value from LoadLibrary in eax here
invoke GetModuleHandle,0
mov    [vv],eax

align 32

    invoke    SetConsoleTextAttribute, ebx, 0Ah 
    invoke    WriteConsole, ebx, welcome, 13, Input, 0
    ; 
    ; Now, wait for a user to type in a string and hit ENTER 
    ; 
    invoke    SetConsoleTextAttribute, ebx, 0Fh 
    invoke    GetStdHandle, STD_INPUT_HANDLE 
    mov       ebx, eax ;<--- you need this line, else the handle in eax is lost.
    mov       edi, response
    invoke    ReadConsole, ecx, edi, 32, Input, 0



;--------------------------------------------------------------------------
proc StreamIn   Input   ;write input to test.txt

        invoke  WriteFile,[Input],filename,0

 endp

;-------------------------------------------------------------------8-------

 mov       eax, [Input]  ;clears command/text
 mov       byte [edi + ecx], 0

;---------------------------------------------------------------------------


Exit:

invoke    ExitProcess,0




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

    library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL'
    include 'API\Kernel32.Inc'
    include 'API\User32.Inc' 
    include 'API\Gdi32.Inc'
    
Post 08 Mar 2012, 13:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17472
Location: In your JS exploiting you and your system
revolution
Code:
    invoke    ReadConsole, ecx, edi, 32, Input, 0     
ECX is undefined in the last line. Perhaps you meant to use EAX from the previous 'invoke' three lines above?

BTW: Even after fixing this line there are still lots more errors that you will find.
Post 08 Mar 2012, 14:04
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
@drewtoby:
There are way too many problems in your code.

1. Why did you put a 'proc' in the middle of the code.
2. 'WriteFile' is used without actually opening a file.
3. 'WriteFile' has incorrect # of parameters.

I suggest this source to study:
http://msdn.microsoft.com/en-us/library/aa364232(v=vs.85).aspx

You'll need at least 3 functions from there:

CreateFile
WriteFile
CloseHandle
Post 08 Mar 2012, 15:13
View user's profile Send private message Send e-mail Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Okay, will continue to work on and post again in a day if my re-write fails. Will use your resource.
Post 08 Mar 2012, 17:02
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
your code doesn´t define an EntryPoint
Code:
format  PE CONSOLE 4.0 
    

start is the label name, it could be what ever name you want, but it should be a Valid Label from where your code start to execute.

Check the AsmGuru example
Post 08 Mar 2012, 19:38
View user's profile Send private message Reply with quote
drewtoby



Joined: 05 Mar 2012
Posts: 19
drewtoby
Okay. One last noob question: How can I include:

Code:
include 'E:\PellesC\Lib\Win64\Kernel32.lib'
include 'E:/PellesC/Include/win/Windows.h'  
    


So I can use the OpenFile/CreateFile function!?

NOTE: I have tried #include<> too, but neither work.
Post 08 Mar 2012, 21:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.