Page 1 of 2

how to fill file with 0

Posted: Sun May 04, 2008 3:34 am
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 ?

Posted: Sun May 04, 2008 4:46 am
by bluecode
Under which operating system?

Posted: Sun May 04, 2008 4:52 am
by JamesM
MASM - can only be windows.

Posted: Sun May 04, 2008 8:02 am
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.

Posted: Sun May 04, 2008 10:52 am
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.

Re: how to fill file with 0

Posted: Sun May 04, 2008 11:04 am
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.

Posted: Sun May 04, 2008 12:16 pm
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

Posted: Sun May 04, 2008 9:34 pm
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.

Posted: Mon May 05, 2008 12:38 am
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.

Posted: Mon May 05, 2008 3:50 am
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

Posted: Mon May 05, 2008 10:16 am
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

Posted: Mon May 05, 2008 12:00 pm
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.

Posted: Mon May 05, 2008 1:00 pm
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.

Posted: Mon May 05, 2008 7:27 pm
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).

Posted: Mon May 05, 2008 7:39 pm
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!