Re: Writing a simple echo program



Rob Hoelz wrote:
The content of this program isn't so important, but rather the
resulting executable. Here's the program:

section .text

extern putchar

print:
push ebp
mov ebp, esp

push ebx

mov ebx, [ebp+8]
print_loop:
mov eax, [ebx]
cmp al, 0
jz print_loop_end

push eax
call putchar
pop eax

inc ebx
jmp print_loop
print_loop_end:

mov eax, 0x20
push eax
call putchar pop eax

pop ebx

leave
ret

global _start
_start:
mov eax, [esp+8]
push eax
call print
pop eax

mov eax, 1
mov ebx, 0
int 0x80

So here's what I run to build the executable:
$ nasm -f elf test.s
$ ld -s -o test test.o

I've also tried it with ld -o test test.o. The problem is that when
I do this:

$ ./test

I get an interesting message:

$ ./test
bash: ./test: No such file or directory

Uh...what's going on? I've verified that test exists, and that it has
the right permissions, so I don't know what could be wrong. Any hints?

Your program is not finding "/lib/ld-linux.so.1", which is what ld tries to use as a dynamic linker by default. (*When* will "they" get this fixed???) Add "-I /lib/ld-linux.so.2" ("-I" == "--dynamic-linker") to ld's command-line (and the "-lc" you mention in a later post), and I think your problems will vanish.

As Chris points out, "test" is the name of an existing utility, which can cause amusing effects, but I don't think it's the problem here.

(I think your program will crash if not given a command-line - might want to be sure [esp] is at least 2 before attempting to print what might not exist...)

Best,
Frank

.



Relevant Pages