flat assembler
Message board for the users of flat assembler.

Index > DOS > file I/O

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



Joined: 26 Sep 2006
Posts: 92
2
I have been using interrupt 21h to print characters to the screen,but
how do I print it to a file instead? I've tried reading other things online,but
they give all these examples that include libraries.
What is the shortest way to print "Hello File!" to a file called Hello.txt ?

The idea behind this is that eventually I want to start writing all kinds of
weird characters to a file.

_________________
There are 10 kinds of people in the world.
Those who know binary and those who haven't met me.
Post 05 Oct 2006, 17:41
View user's profile Send private message Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
ah=3Ch for create file
ah=3dh for open file for write - collect handle from ax
ah=40h for write byte(s) from memory to the file
ah=3eh for close the file

You must get an interrupt list to get the details. There are a few on the web.

Chris
Post 05 Oct 2006, 21:55
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
I have ralph browns interrupt list,but I can't understand a thing!
I first learned about int 21h from the tutorial in the downloads section
of flatassembler.net but that tutorial isn't finished yet.

I learn best from looking at and trying examples.
How would I get "Hello File" inside Hello.txt ?
Post 06 Oct 2006, 03:56
View user's profile Send private message Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie
This should serve as an example. You must manually create a file called "hello.txt" for this to work. Why don't you try modifying the code to create the file automatically.

Code:
      org 100h

      mov ah,3Dh       ; open file for write
      mov dx,filename
      mov al,1         ; 1 = write mode
      int 21h
      jc error
      mov [handle],ax

      mov ah,40h       ; write bytes from hello
      mov cx,13         ; write 13 bytes
      mov dx,hello
      mov bx,[handle]
      int 21h

      mov ah,3Eh       ; close file
      mov bx,[handle]
      int 21h
      jmp exithello
                        
  error:
      mov ah,09h       ; display error msg
      mov dx,errormsg
      int 21h

  exithello:
      mov   ax,4C00h   ; exit to DOS
      int   21h
  
  hello    db 'Hello File!',0Dh,0Ah,0    ; what we are writing to the file
  filename db "hello.txt",0              ; the name of the file
  handle   dw ?                          ; handle for the opened file
  errormsg db "Open error$",0            ; error message                 


Now you should be able to follow Ralf Brown's list to do lots of other things!

Chris
Post 06 Oct 2006, 09:45
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
also read this for some understanding: www.decard.net/article.php

unfinished too, but it could clear some things for you
Post 06 Oct 2006, 10:33
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
OK,thanks a lot guys!
Post 06 Oct 2006, 18:58
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
I did what I was aiming for! Try this out!
The com should be 49 bytes and the source is about 196 bytes.

Code:
org 100h
mov cx,0
mov ah,60 
mov dx,f
int 33
mov bx,ax
l1:
mov ah,64
mov cx,1
mov dx,b
inc [b]
int 33
cmp [b],255
jb l1
mov bx,ax
mov ah,62
int 33
int 32
f db "db.bin",0
b db -1
    
Post 07 Oct 2006, 03:04
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Okay... so from just reading the source code, my intuition tells me that you are opening a flat-binary file to write to, looping through b and incrementing it's value each time, putting the new value each time into the file, then closing it.
Congrats on understanding it! (And I am proud of myself too for actually being able to understand someone elses ASM code fully for once.

But one question pops into my mind... why do it?
Post 07 Oct 2006, 22:45
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
rhyno_dagreat wrote:
Okay... so from just reading the source code, my intuition tells me that you are opening a flat-binary file to write to, looping through b and incrementing it's value each time, putting the new value each time into the file, then closing it.
Congrats on understanding it! (And I am proud of myself too for actually being able to understand someone elses ASM code fully for once.

But one question pops into my mind... why do it?


Yes,that's what I'm doing. Also I tried making the code readable.
I thought you'd never ask why! Well it's quite simple.

A 256 byte file with the characters 00 to FF is the ultimate perfect file.
Here's why.

1.It contains every possible byte value there is.
2.The offset and the value are the same.
3.Useful for the file comparing tool I wrote in DJGPP.
4.Small enough that sending it is never a problem.

but your question of WHY I did it is another story.
Well,I had written a program in C that did the same thing,but there is 1
problem with that. The EXE was over 5KB ! That is stupid!

I knew that the only way I could ever get a program smaller than 256 bytes
was by learning assembly,so began my quest to make REALLY small
programs. The DOS COM files were so small that I'm very impressed.
Would you believe I started ASM in the middle of September?
I've come pretty far and I must thank the people at this forum as well
as the makers of FASM. I love making things in ASM and it's just
so much fun that I actually prefer using it instead of C if I have the
knowledge required to do what I want.

BTW,you haven't seen anything yet! Check out this program!
It's a rewrite of the other and it makes every possible 3 byte value.
The total size of the output file is 48.0 MB (50,331,648 bytes) .
Not bad considering the COM file is 58 bytes!

Code:
org 100h
mov cx,0
mov ah,60 
mov dx,f
int 33
mov bx,ax
l1:
mov ah,64
mov cx,3
mov dx,b
inc [b]
int 33
cmp [b],16777215
jb l1
mov bx,ax
mov ah,62
int 33
int 32
f db "d3b.bin",0
b dd -1
    


I dare you to try emailing or uploading that somewhere. You just can't
find that kind of space! And yet,with a little ASM code,it's possible for
somebody else to get the same file from either assembling the source
or you send them the tiny COM file!

but still,I have the question,why would THEY want it?!

_________________
There are 10 kinds of people in the world.
Those who know binary and those who haven't met me.
Post 08 Oct 2006, 06:26
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Hmmm... maybe if it's a hacker, putting it on a server for websites, disguising it as an actual website (with the .com ending) and fooling the server owner into thinking it's a website, when it's just there wasting their bandwidth. Twisted Evil

But... y'never know...









Twisted Evil
Post 09 Oct 2006, 03:46
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
It could be used for some weird purposes. Maybe you shouldn't ask why.
Somebody will find a use for just about anything.

BUT,can you find a use for this?!
I have yet to let this program finish running.
It would result in a 16GB file! I don't even have a partition of that size!

Code:
org 256
mov cx,0
mov ah,60 
mov dx,f
int 33
mov bx,ax
l1:
mov ah,64
mov cx,4
mov dx,b
int 33
inc [b]
cmp [b],0
ja l1
mov bx,ax
mov ah,62
int 33
int 32
f db "dd.bin",0
b dd 0
    
Post 10 Oct 2006, 01:00
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Lol, sweetness.
Post 10 Oct 2006, 01:41
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
Hey,can you imagine what would happen if somebody wrote that to the screen instead of a file?!


I once had to reboot my PC because I had an
infinite jmp.
Post 10 Oct 2006, 20:51
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Eek. What type of PC are you running?
Post 11 Oct 2006, 02:17
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
2: you can save one byte in source with replacing "org 100h" with "org 256" Laughing
Post 11 Oct 2006, 08:06
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
Using decimal for memory addresses and dos system call numbers is just needless confusion. It won't affect the binary at all, and who cares how big the source is? If you wanted the smallest possible source, use some high level language because it will almost always be smaller than in assembly.
Post 11 Oct 2006, 17:06
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
Quote:

2: you can save one byte in source with replacing "org 100h" with "org 256"


Yes,my latest versions do use org 256. I Think vid has caught on to
my plans.

Quote:

Using decimal for memory addresses and dos system call numbers is just needless confusion. It won't affect the binary at all, and who cares how big the source is? If you wanted the smallest possible source, use some high level language because it will almost always be smaller than in assembly.


I'm very picky,I do want small source,but want small COM files.
I save a few bytes by using decimal. I don't know what 'needless
confusion' you're talking about. I am so well versed in the ways of
number bases that I didn't use a calculator to convert from hex to
decimal. I have it in my head. If it confuses you then ASM isn't your
thing! I use to write things in binary all the time,but that got kinda
large. I wish I could use hexidecimal without using the 0x or h .
Hexidecimal SHOULD be the default base. If it was,I wouldn't be using
decimal!
Post 11 Oct 2006, 18:27
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
2 wrote:
I don't know what 'needless
confusion' you're talking about. I am so well versed in the ways of
number bases that I didn't use a calculator to convert from hex to
decimal. I have it in my head. If it confuses you then ASM isn't your
thing!


Okay. Without using a calculator or anything, how long does it take for you to figure out what call this code is doing:
mov ax,19968
int 33
Post 12 Oct 2006, 00:08
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
2 wrote:
I wish I could use hexidecimal without using the 0x or h .
Hexidecimal SHOULD be the default base. If it was,I wouldn't be using
decimal!


Some assemblers do allow this (e.g., ArrowASM, MASM, A86, etc.): .radix 16.

Code:
.radix 16
code segment
assume cs:code,ds:code,es:code
org 100

Komenco:
        mov ax,10
        mov ax,16
        mov ax,44
Fino:
        int 20

code ends
end Komenco
    


Then verify it yourself (it works for me):

asm.exe blah,,;
edit blah.lst
Post 12 Oct 2006, 01:16
View user's profile Send private message Visit poster's website Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2
Quote:

Okay. Without using a calculator or anything, how long does it take for you to figure out what call this code is doing:
mov ax,19968
int 33


19968 0
09984 0
04992 0
02496 0
01248 0
00624 0
00312 0
00156 0
00078 0
00039 1
00019 1
00009 1
00004 0
00002 0
00001 1
00000 final

reading from bottom to top should give

100111000000000

Which can be separated into hex digits by grouping them in 4s.

0100 1110 0000 0000

Which would be hex 4E00 .

So you loaded 0x4E00 in ax
and I know that int 33 is int 0x21

I did that only in this forum. No calculator. Couldn't quite remember
all the shifting in my head,but that's the method I used.
I read that off of Wikipedia awhile back.

Goplat,look what you made me do! I'll never forgive you! LOL
Well,anyway,I wanna post this and go check my work with a calculator!
I must know if I did it right!
Post 12 Oct 2006, 07:22
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.