flat assembler
Message board for the users of flat assembler.

Index > Windows > Beginnier problem with input.

Author
Thread Post new topic Reply to topic
mmajewski



Joined: 15 Apr 2014
Posts: 10
mmajewski
I am kind of new to assembly programming. I have chosen FASM, to do my projects. I started writing simple application that will take input from user save it to "buff" and that creates file "test.txt" including "buff". I do not understand why am I getting the error: "value out of range"? Is it better way to save file than this set of instructions (this is the only simple example I found on the Internet) ? I will appreciate any tips or advices, links, and so on.
Code:
format pe console 4.0
include 'WIN32AX.INC'
 
.data
        bufsize = 20
        byteswritten dd ?
        buff db 20, 20 dup(0)
.code
 main:
        push cs
        pop ds
        mov ah,0ah
        mov dx,buff
        int 21h

        invoke  CreateFile, 'test.txt', GENERIC_WRITE, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
        invoke  WriteFile, eax, buff, bufsize, byteswritten, 0
        invoke  ExitProcess, 0
.end main         
Post 15 Apr 2014, 12:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17247
Location: In your JS exploiting you and your system
revolution
You can't use the DOS "int 21h" or change the segment registers CS or DS in a protected mode 32-bit program running in Windows.

For keyboard input you can open a handle to STD_INPUT_HANDLE and read bytes from there.

BTW: The value out or range error is because dx is too small to hold a 32-bit address 0x004XXXXX.
Post 15 Apr 2014, 13:03
View user's profile Send private message Visit poster's website Reply with quote
mmajewski



Joined: 15 Apr 2014
Posts: 10
mmajewski
@revolution
Thank you for answering. I know understand. Is it a way to create a file and write something in it in DOS mode?
Post 15 Apr 2014, 13:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17247
Location: In your JS exploiting you and your system
revolution
You are almost there. Just get the input handle (GetStdHandle) and read the keyboard buffer (ReadFile) then write it out to the target file name (CreateFile, WriteFile, CloseHandle).
Post 15 Apr 2014, 13:28
View user's profile Send private message Visit poster's website Reply with quote
mmajewski



Joined: 15 Apr 2014
Posts: 10
mmajewski
@revolution
How to use ReadFile?
I can not find any links that shows me how to use this functions in assembly.
So far I created:
Code:
format pe console 4.0
include 'WIN32AX.INC'
 
.data
        bufsize = 20
        byteswritten dd ?
        buff dd ?
.code
main:
        invoke  GetStdHandle, STD_INPUT_HANDLE
        mov [buff], eax
        invoke  CreateFile, 'test.txt', GENERIC_WRITE, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
        invoke  WriteFile, eax, buff, bufsize, byteswritten, 0
        invoke  ExitProcess, 0
.end main            
Post 15 Apr 2014, 13:54
View user's profile Send private message Reply with quote
mmajewski



Joined: 15 Apr 2014
Posts: 10
mmajewski
But that programs is closing immediately and i have no chance to input anything.
Post 15 Apr 2014, 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: 17247
Location: In your JS exploiting you and your system
revolution
mmajewski wrote:
How to use ReadFile?
I can not find any links that shows me how to use this functions in assembly.
The second link from google search for "ReadFile" returns the MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx

You use it in assembly in the same way as C would use it.
Post 15 Apr 2014, 13:57
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17247
Location: In your JS exploiting you and your system
revolution
mmajewski wrote:
But that programs is closing immediately and i have no chance to input anything.
Because you are not reading from the input. After opening the handle you then read from it into a buffer.
Post 15 Apr 2014, 14:01
View user's profile Send private message Visit poster's website Reply with quote
mmajewski



Joined: 15 Apr 2014
Posts: 10
mmajewski
@revolution
Thank you very much I finally managed to do this. Such a joy.
Code:
format pe console 4.0
include 'WIN32AX.INC'
 
.data
        bufsize = 4
        byteswritten dd ?
        buff dd ?
.code
main:

        invoke  GetStdHandle, STD_INPUT_HANDLE
        invoke  ReadFile, eax, buff, bufsize, 0,0
        invoke  CreateFile, 'test.txt', GENERIC_WRITE, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
        invoke  WriteFile, eax, buff, bufsize, byteswritten, 0
        invoke  ExitProcess, 0
.end main          
Post 15 Apr 2014, 14:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17247
Location: In your JS exploiting you and your system
revolution
Good, but you forgot about CloseHandle.
Post 15 Apr 2014, 14:30
View user's profile Send private message Visit poster's website Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 237
Location: Poland
macgub
Just one question. How to correctly close handle? I have similar problem.
Post 01 Sep 2014, 08:17
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17247
Location: In your JS exploiting you and your system
revolution
macgub wrote:
Just one question. How to correctly close handle? I have similar problem.
The API takes only one input value, the handle of the object.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211%28v=vs.85%29.aspx
Post 01 Sep 2014, 08:28
View user's profile Send private message Visit poster's website Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 237
Location: Poland
macgub
So something like that will be OK ?
Code:
    invoke  GetStdHandle, STD_OUTPUT_HANDLE
    mov  [file_handle],eax
    invoke  CreateFile,file_name, GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
    invoke  WriteFile,eax,file_buffer,[file_size], byteswritten, 0
    invoke  CloseHandle,[file_handle]

    
Post 01 Sep 2014, 09:11
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-2020, Tomasz Grysztar.

Powered by rwasa.