flat assembler
Message board for the users of flat assembler.

Index > Main > File Creation

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



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 14 Feb 2007, 10:20
Hi,
I normally write programs in java and have no experience with assembly or flat assembler. I need to write a program (quickly) which can create and write to files, I found the attatched segment of code at http://www.computing.dcu.ie/~ray/CA225.html#FILES%20AND%20HOW%20TO%20USE%20THEM, tried to run it but found many errors. Please could someone help me fix the problem.

Apologies if this has been covered in a different thread.


Description:
Download
Filename: createFile.ASM
Filesize: 2.4 KB
Downloaded: 893 Time(s)

Post 14 Feb 2007, 10:20
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 14 Feb 2007, 10:44
it is masm example. are you really need 16-bit executable?
-----------
here fasm win32 console example for you


Description:
Download
Filename: sw92.0.0.1.1.zip
Filesize: 2.19 KB
Downloaded: 871 Time(s)



Last edited by shoorick on 14 Feb 2007, 10:52; edited 1 time in total
Post 14 Feb 2007, 10:44
View user's profile Send private message Visit poster's website Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 14 Feb 2007, 10:50
What I need is a program that will run under Windows XP (32-bit) which is capable of creating files and writing data to files.
Post 14 Feb 2007, 10:50
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 14 Feb 2007, 10:54
get example above (i have edited post)
Post 14 Feb 2007, 10:54
View user's profile Send private message Visit poster's website Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 14 Feb 2007, 12:55
Thankyou for your help.

I also need to read sectors from the hard disk and ideally also be able to send ATA commands (such as IDENTIFY_DEVICE), please could you tell me if its possible to do these things with fasm win32 running under windows xp.
Post 14 Feb 2007, 12:55
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 14 Feb 2007, 13:13
under win32 it is possible with kernel mode drivers, but it will need enough high skill on NT system work - i have no enough experience on this to help you.
Post 14 Feb 2007, 13:13
View user's profile Send private message Visit poster's website Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 14 Feb 2007, 13:23
I've found this code (http://www.geocities.com/SiliconValley/2072/atapi.htm) that apparently sends ATA commands to the hard disk but its probably not written in fasm win32. I'd be grateful if someone could translate it for me (if possible) and let me know if it'd work with windows xp.


Description:
Download
Filename: ATA.txt
Filesize: 2.28 KB
Downloaded: 1083 Time(s)

Post 14 Feb 2007, 13:23
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 14 Feb 2007, 17:34
It will not work under XP, you will need to boot from a floppy or cd etc, to run such code.
Here some code that may help;
http://www.dex4u.com/cdpod/cdpod.zip
Post 14 Feb 2007, 17:34
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 15 Feb 2007, 05:32
it sounds like software protection attempt, so, there is not so simple I think
Post 15 Feb 2007, 05:32
View user's profile Send private message Visit poster's website Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 15 Feb 2007, 10:09
Please could you explain why it won't work under xp. I'd also like to know why the code I attatched above wouldn't compile.

Dex4u - does your code work with xp? Does the code you provided send ATA commands to the disk?
Post 15 Feb 2007, 10:09
View user's profile Send private message Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 15 Feb 2007, 10:57
Ok, I've found that I need to use the windows APIs instead of having direct access to the systems resources. I've searched the MSDN pages on the different I/O commands which can be used with the Device Input and Output Control (IOCTL).

However I haven't found a command which can read raw sectors from the disk. Does anyone know how to do this?
Is there an IOCTL command which can send ATA commands to the disk? (this is less important than being able to read raw sectors)

Sorry for asking so many questions which are probably obvious to all of you but its important that i find a way of doing this and i'm running out of time.
Post 15 Feb 2007, 10:57
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 15 Feb 2007, 11:07
it would be clear if you were know some basics on cpu and os architecture. simply say, there are two modes of cpu: "real" and "protected". real present after cpu reset and let you use any instructions in your code. such mode can be found in bare ms dos. but this mode is unfriendly for multytasking, so, modern oses turn cpu into protected mode after loading. in this mode it is possible to assign security level to the excuted code and restrict some memory (read/write/execute) and i/o operations for each level. usualy system has "kernel" level ("0") and user applications have "user" ("3") level. it is impossible provide direct i/o operations from user level, to do this usually used intermediate drivers, which run in kernel mode.
Post 15 Feb 2007, 11:07
View user's profile Send private message Visit poster's website Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 15 Feb 2007, 11:36
One more question (hopefully) - Using the ReadFile method described in the windows API (http://msdn2.microsoft.com/en-us/library/aa365467.aspx) is it possible to start reading the file, or disk, from a specific location. If so, how?

e.g If i wanted to read 512 bytes from the hard disk and write them to a file, then read the next 512 bytes and write them to the end of the file, etc. How would i use the readFile method to do it (you don't need to describe how to write to the file, i now know how to do this - sort of)

Thanks for all yourr help so far
Post 15 Feb 2007, 11:36
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 15 Feb 2007, 11:53
there is SetFilePointer function to set initial pointer position in file.
if you read 512 bytes from file next same reading will read next 512 bytes.
Post 15 Feb 2007, 11:53
View user's profile Send private message Visit poster's website Reply with quote
tantrikwizard



Joined: 13 Dec 2006
Posts: 142
tantrikwizard 15 Feb 2007, 14:31
assemblyNewbie wrote:
One more question (hopefully) - Using the ReadFile method described in the windows API (http://msdn2.microsoft.com/en-us/library/aa365467.aspx) is it possible to start reading the file, or disk, from a specific location. If so, how?

e.g If i wanted to read 512 bytes from the hard disk and write them to a file, then read the next 512 bytes and write them to the end of the file, etc. How would i use the readFile method to do it (you don't need to describe how to write to the file, i now know how to do this - sort of)

Thanks for all yourr help so far


What youre trying to do is not so simple. IOCTL is the proper function to work directly with the HAL (hardware abstraction layer) and thus read absolute HDD sectors (if it is even possible). Use the standard file IO API to read & write files, not disk sectors. http://msdn2.microsoft.com/en-us/library/aa365470.aspx You cannot use IOCTL to read & write files (I dont think) IOCTL is for direct hardware interaction and the file system is an extra layer above the hardware.
Post 15 Feb 2007, 14:31
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 15 Feb 2007, 15:55
assemblyNewbie wrote:

Dex4u - does your code work with xp? Does the code you provided send ATA commands to the disk?

No you can not use it under XP and yes that code can send ATA commands.
I found this our to write to sectors in xp
http://www.codeguru.com/Cpp/W-P/system/misc/article.php/c5765/

With out a OS you can do it like this, to read/write to hdd:
Code:
;       Reading the harddisk using ports!;       +-------------------------------+   by qark;;;  This took me months to get working but I finally managed it.;;  This code only works for the 286+ so you must detect for 8088's somewhere;  in your code.;;  Technical Information on the ports:;      Port    Read/Write   Misc;     ------  ------------ -------------------------------------------------;       1f0       r/w       data register, the bytes are written/read here;       1f1       r         error register  (look these values up yourself);       1f2       r/w       sector count, how many sectors to read/write;       1f3       r/w       sector number, the actual sector wanted;       1f4       r/w       cylinder low, cylinders is 0-1024;       1f5       r/w       cylinder high, this makes up the rest of the 1024;       1f6       r/w       drive/head;                              bit 7 = 1;                              bit 6 = 0;                              bit 5 = 1;                              bit 4 = 0  drive 0 select;                                    = 1  drive 1 select;                              bit 3-0    head select bits;       1f7       r         status register;                              bit 7 = 1  controller is executing a command;                              bit 6 = 1  drive is ready;                              bit 5 = 1  write fault;                              bit 4 = 1  seek complete;                              bit 3 = 1  sector buffer requires servicing;                              bit 2 = 1  disk data read corrected;                              bit 1 = 1  index - set to 1 each revolution;                              bit 0 = 1  previous command ended in an error;       1f7       w         command register;                            commands:;                              50h format track;                              20h read sectors with retry;                              21h read sectors without retry;                              22h read long with retry;                              23h read long without retry;                              30h write sectors with retry;                              31h write sectors without retry;                              32h write long with retry;                              33h write long without retry;;  Most of these should work on even non-IDE hard disks.;  This code is for reading, the code for writing is the next article.        mov     dx,1f6h         ;Drive and head port        mov     al,0a0h         ;Drive 0, head 0        out     dx,al        mov     dx,1f2h         ;Sector count port        mov     al,1            ;Read one sector        out     dx,al        mov     dx,1f3h         ;Sector number port        mov     al,1            ;Read sector one        out     dx,al        mov     dx,1f4h         ;Cylinder low port        mov     al,0            ;Cylinder 0        out     dx,al        mov     dx,1f5h         ;Cylinder high port        mov     al,0            ;The rest of the cylinder 0        out     dx,al        mov     dx,1f7h         ;Command port        mov     al,20h          ;Read with retry.        out     dx,alstill_going:        in      al,dx        test    al,8            ;This means the sector buffer requires                                ;servicing.        jz      still_going     ;Don't continue until the sector buffer                                ;is ready.        mov     cx,512/2        ;One sector /2        mov     di,offset buffer        mov     dx,1f0h         ;Data port - data comes in and out of here.        rep     insw;   ------        mov     ax,201h         ;Read using int13h then compare buffers.        mov     dx,80h        mov     cx,1        mov     bx,offset buffer2        int     13h        mov     cx,512        mov     si,offset buffer        mov     di,offset buffer2        repe    cmpsb        jne     failure        mov     ah,9        mov     dx,offset readmsg        int     21h        jmp     good_exitfailure:        mov     ah,9        mov     dx,offset failmsg        int     21hgood_exit:        mov     ax,4c00h        ;Exit the program        int     21h        readmsg db      'The buffers match.  Hard disk read using ports.$'        failmsg db      'The buffers do not match.$'buffer  db      512 dup ('V')buffer2 db      512 dup ('L');;       Writing to the hard disk using the ports!     by qark;       +---------------------------------------+;;  The only differences between reading and writing using the ports is;  that 30h is sent to the command register, and instead of INSW you;  OUTSW.  ;        mov     dx,1f6h         ;Drive and head port        mov     al,0a0h         ;Drive 0, head 0        out     dx,al        mov     dx,1f2h         ;Sector count port        mov     al,1            ;Write one sector        out     dx,al        mov     dx,1f3h         ;Sector number port        mov     al,1           ;Wrote to sector one        out     dx,al        mov     dx,1f4h         ;Cylinder low port        mov     al,0            ;Cylinder 0        out     dx,al        mov     dx,1f5h         ;Cylinder high port        mov     al,0            ;The rest of the cylinder 0        out     dx,al        mov     dx,1f7h         ;Command port        mov     al,30h          ;Write with retry.        out     dx,aloogle:        in      al,dx        test    al,8            ;Wait for sector buffer ready.        jz      oogle        mov     cx,512/2        ;One sector /2        mov     si,offset buffer        mov     dx,1f0h         ;Data port - data comes in and out of here.        rep     outsw           ;Send it.;    ------------        mov     ax,201h                 ;We'll read in sector 1 using        mov     bx,offset buffer2       ;int13h and see if we are successful.        mov     cx,1        mov     dx,80h        int     13h        mov     cx,512        mov     si,offset buffer        mov     di,offset buffer2        repe    cmpsb                   ;Compare the buffers.        jne     failure        mov     ah,9        mov     dx,offset write_msg        int     21h        jmp     w_exitfailure:        mov     ah,9        mov     dx,offset fail        int     21hw_exit:        mov     ax,4c00h        ;Exit the program        int     21h        write_msg       db      'Sector one written to using the ports, OH NO! there goes XP.$'        fail            db      'Writing using ports failed.$'buffer  db      512 dup ('A')buffer2 db      512 dup ('D')    

NOTE: THE ABOVE CODE WILL OVER WRITE YOUR MBR, so do not run with above settings, and the dos bits would be replaced by none dos functions.
Post 15 Feb 2007, 15:55
View user's profile Send private message Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 16 Feb 2007, 12:20
I'm not interested in using the IOCTL to read/write files as I'll use standard C/C++ functions to do this.

I found the attatched code which apparantly reads sectors directly from the hard disk. Its written in C or C++ but I haven't managed to compile it yet - as i only have a c compiler.

I tried the example code on the MSDN website which should display information about the disk geometry. This doesn't compile either , please could someone tell me if its a problem with the code or if its something simple like the programs written in C++ not C.


Description:
Download
Filename: read sectors.txt
Filesize: 1.16 KB
Downloaded: 878 Time(s)

Post 16 Feb 2007, 12:20
View user's profile Send private message Reply with quote
assemblyNewbie



Joined: 14 Feb 2007
Posts: 12
Location: England
assemblyNewbie 16 Feb 2007, 12:28
The code mentioned above is attatched here


Description:
Download
Filename: driveInfo.txt
Filesize: 1.64 KB
Downloaded: 904 Time(s)

Post 16 Feb 2007, 12:28
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 16 Feb 2007, 12:44
yes, it reads physical disks or partitions (running with admin privilege)
----------
you can run tt from attach in this way:

tt \\.\c: d.txt


Description:
Download
Filename: tt.0.0.1.1.zip
Filesize: 4.92 KB
Downloaded: 848 Time(s)

Post 16 Feb 2007, 12:44
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1905
DOS386 17 Feb 2007, 01:16
Quote:
Dex4u - does your code work with xp?


NO. Why ? The IN and OUT instructions are crippled off in XP.
(Except with some Admin/Driver deals maybe Confused, not present is this code)
INT $13 does'n work eihter Shocked in XP

Quote:
Does the code you provided send ATA commands to the disk?


I hope yes, but ONLY in DOS or on no OS at all.

Are you sure that you need to read sectors in XP ? You can't do much
with the NTFS data anyway, except intentionally corrupt it Laughing

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 17 Feb 2007, 01:16
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.