tags:

views:

361

answers:

8

I have the following assembly program which displays the letter 'z' and then exits:

mov dl, 'z'
mov ah, 2h
int 21h
mov ah, 4Ch
int 21h

I assembled it with NASM and the resulting file only contains those instructions. (10 bytes) I put 1000 calls to this program in a batch file, and then 1000 calls to

echo z

and the echos run about 10x faster. Does anyone know what would be causing this program to run so slowly? Thanks in advance.

+13  A: 

"echo" is a command that's built into the command interpreter; no code needs to be loaded to execute the command. Your program, small as it is, needs to be read into memory and initialized every time it is called. Before it even gets to that point, the command interpreter will search the PATH to find the program, which takes a significant amount of time.

Mark Ransom
+3  A: 

where your program needs to be started 1000 times, echo may be built in thus no startup overhead.

KM
+2  A: 

Echo is a command that is running inside the context of your command line batch script. No external process is being executed, so it is very quick to execute.

Each of your executions of the assembly program requires a start and stop of the application, there is a certain overhead for that operation.

Mitchel Sellers
+6  A: 

I think the echo command maybe build into the shell, so there is no overhead of loading a new program on each call

+7  A: 

Likely this has less to do with your code and more to do with the underlying operating system.

Echo is a command recognized by the command interpreter immediately. As such, calling echo does not initiate a new process; the echo occurs in the scope of the command interpreter.

On the other hand, initiating your small assembly program involves creating a new process and all the overhead that implies.

Randolpho
A: 

It's possible that the invocation of a program requires that program to be loaded from outside of cache (perhaps echo already was in cache?) and a number of other intricacies. Also, you're invoking userland code whereas the echo command may have more priority, etc, etc.

Brandon Pelfrey
I don't think there's any difference in priority between built-in commands and user programs.
Mark Ransom
+1  A: 

Try running strace <your prog> - you'll see what shell, linker, etc. all have to do to execute event this tiny program.

Nikolai N Fetissov
+4  A: 

Your program uses the DOS API. On a modern OS it has to run on a virtual machine, e.g. NTVDM or DOSbox. This is probably the main reason that makes it slow.

You can create a native executable with this code, which should be faster:

bits 32
global main

extern putchar


section .text
main:
        push 'z'
        call putchar
        pop ecx

        xor eax, eax
        ret

On Unix, you can compile and execute it with these commands:

nasm file.asm -f elf
gcc file.o -o file
./file

On Windows, replace -f elf with -f win32. If you use Visual Studio's compiler and linker, try using cl file.o in VS's command prompt (untested).

Bastien Léonard