Reputation: 7110
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.
Upvotes: 2
Views: 1284
Reputation: 61703
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).
Upvotes: 4
Reputation: 84159
Try running strace <your prog>
- you'll see what shell, linker, etc. all have to do to execute event this tiny program.
Upvotes: 1
Reputation: 2599
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.
Upvotes: -1
Reputation: 56391
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.
Upvotes: 8
Reputation:
I think the echo command maybe build into the shell, so there is no overhead of loading a new program on each call
Upvotes: 6
Reputation: 63126
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.
Upvotes: 2
Reputation: 103589
where your program needs to be started 1000 times, echo may be built in thus no startup overhead.
Upvotes: 3
Reputation: 308121
"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.
Upvotes: 14