how to fill file with 0

Programming, for all ages and all languages.
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

how to fill file with 0

Post by david »

using MASM,
The size of complied file is 3K, I want to extend file to 32K, and fill file with 0.
how to do ?
Just For Fun
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

Under which operating system?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

MASM - can only be windows.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

Well, I meant which environment david targets, ie. a userspace Windows/Linux/DOS/whatever application or soem freestanding stuff... The hosting environment is not really important.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

I have no idea about doing it in a low language like that, but i know in C# you can read the bytes of the file, then increase the size of bytes, and re-make a replica file but with the extension.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: how to fill file with 0

Post by inflater »

david wrote:using MASM,
The size of complied file is 3K, I want to extend file to 32K, and fill file with 0.
how to do ?
Simple as it gets, add this to the end:

Code: Select all

times 32768-($-$$) db 0
Don't know if it will work in MASM, if not, try DB 0 DUP (...).
JamesM wrote:MASM - can only be windows.
You're wrong, it can be DOS too.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

maybe
The DUP Operator
You can also declare an array with the DUP operator. This operator works with any of the data allocation directives described in "Allocating Memory for Integer Variables" in Chapter 4. In the syntax

count DUP (initialvalue [[, initialvalue]]...)
the count value sets the number of times to repeat all values within the parentheses. The initialvalue can be an integer, character constant, or another DUP operator, and must always appear within parentheses. For example, the statement
barray BYTE 5 DUP (1)
allocates the integer 1 five times for a total of 5 bytes.

The following examples show various ways to allocate data elements with the DUP operator:
array DWORD 10 DUP (1) ; 10 doublewords
                                              ;   initialized to 1
buffer  BYTE    256 DUP (?)                   ; 256-byte buffer

masks   BYTE    20 DUP (040h, 020h, 04h, 02h) ; 80-byte buffer
                                              ;   with bit masks
three_d DWORD   5 DUP (5 DUP (5 DUP (0)))     ; 125 doublewords
                                              ;   initialized to 0

Code: Select all

ORG 510    ; Make the file 512 bytes long
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

Post by david »

thank you, I know .

Code: Select all

org 32767                   ; 32 * 1024 - 1
db 0
The exe file is 0.5K more than bin file.

But i find a problem.
It is impossible to create file which size is 64K.

Code: Select all

.model tiny
.code
.386
start:
    xor ax, ax

    org 65535                        ; 64 * 1024 -1
    db 0

end start
if the file name is test.asm

Code: Select all

masm test.asm
link test.obj
exe2bin test.exe test.bin
the compile message is :

Code: Select all

F:\EzNet>masm test.asm
Microsoft (R) MASM Compatibility Driver
Copyright (C) Microsoft Corp 1993.  All rights reserved.

 Invoking: ML.EXE /I. /Zm /c /Ta test.asm

Microsoft (R) Macro Assembler Version 6.11c
Copyright (C) Microsoft Corp 1981-1994.  All rights reserved.

 Assembling: test.asm

F:\EzNet>link test.obj

Microsoft (R) Segmented Executable Linker  Version 5.31.009 Jul 13 1992
Copyright (C) Microsoft Corp 1984-1992.  All rights reserved.

Run File [test.exe]:
List File [nul.map]:
Libraries [.lib]:
Definitions File [nul.def]:
test.obj(test.asm) : warning L4020: _TEXT : code-segment size exceeds 64K-36
LINK : warning L4021: no stack segment

F:\EzNet>exe2bin test.exe test.bin
Insufficient memory
The size of test.exe is 64.5K.
exe2bin can't change test.exe to test.bin.

I need the file's size is 64K.

so, I think it is impossilbe to create bin file which size is 64k using MASM.

who know other good idea.

I know it maybe done using NASM.

But i must using MASM in my company .

Thanks.
Last edited by david on Tue May 06, 2008 8:34 am, edited 1 time in total.
Just For Fun
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

david wrote:But i must using MASM in my company.
For what company are you working that enforces this assembler, and assembly in general. Especially with this kind of dated software.

If there's a good reason for using these tools, then you should be able to ask your fellow colleagues during a coffee break how they would solve such a problem.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Code: Select all

#include <stdio.h>

#define FILE_SIZE 64*1024   // 64KB

int main (char argc, char **argv)
{
  FILE *in = fopen(argv[1], "rb");
  FILE *out = fopen(argv[2], "wb");

  if (!in || !out)
  {
    fprintf(stderr, "File open error!\n");
    exit(1);
  }

  unsigned long i;
  char buffer;
  for (i = 0; i < FILE_SIZE; i++)
  {
    if (!fread(&buffer, 1, 1, in))
      buffer = '\0';
    fwrite(&buffer, 1, 1, out);
  }  

  exit(0);
}

Code: Select all

$ gcc -o test test.c
test.c: In function ‘main’:
test.c:18: warning: incompatible implicit declaration of built-in function ‘exit’
test.c:38: warning: incompatible implicit declaration of built-in function ‘exit’

$ echo "Howay the lads" >test.in

$ ./test test.in test.out

$ cat test.out
Howay the lads

$ ls -l test.out
-rw-r--r-- 1 james james 65536 2008-05-05 10:48 test.out
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

[quote]
org 65535 ; 64 * 1024 -1
db 0
[quote]

Wouldn't this place a zeroed byte after the 64k file? That would make the on-disk size 64.5k, whereas:

Code: Select all

   org 65534
   db 0
Should create a 64k file (disclaimer: I don't know MASM).

Cheers,
Adam
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

I think JamesM is right. Don't pad the binary object -- pad the executable file. Just append a bunch of junk to the end of a compiled executable until it is exactly 64K.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

What does this assemble too ?

Code: Select all

.model tiny
.code
.386
start:
    xor ax, ax
    org 1024
    org 64511                        
    db 0
If it will allow it.
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

Post by david »

Dex wrote:What does this assemble too ?

Code: Select all

.model tiny
.code
.386
start:
    xor ax, ax
    org 1024
    org 64511                        
    db 0
If it will allow it.
org 64511
db 0
The byte's offset is 64511, not (64511 + 1024).
Just For Fun
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

Post by david »

to JamesM

I have made a tool to extend a file to 64K if the file's size is not 64K.
But I don't want to use it after I make the binary file everytime.

I want to make the binary file like this:
1 program
2 compile
3 link
then, the binary file's size is 64K.

Perhaps I am lazy,

Thanks!
Last edited by david on Tue May 06, 2008 8:32 am, edited 1 time in total.
Just For Fun
Post Reply