Re: using libc from nasm
From: Frank Kotler (fbkotler_at_comcast.net)
Date: 02/13/04
- Previous message: Chewy509: "Re: printing bitmaps under dos"
- In reply to: Adam Bozanich: "using libc from nasm"
- Next in thread: Ulf Andersson: "Re: using libc from nasm"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 13 Feb 2004 09:55:19 +0000 (UTC)
Adam Bozanich wrote:
> Hi all. I am trying to learn asm with NASM on a FreeBSD system.
Hi Adam,
I've never run FreeBSD. I know it's different from Linux, but I'll take
a guess anyway! :)
> I really need
> to debug my programs while I learn, so I want to use printf. This is what I am
> using to assemble and link:
>
> nasm -f elf use_printf.asm
> ld -s -o use_printf use_printf.asm -lc
This has gotta be "use_printf.o", not "use_printf.asm", right? I forget
what the error message is if you try to link your .asm file, but I know
from experience it doesn't work. :)
> /usr/lib/libc.so: undefined reference to `environ'
> /usr/lib/libc.so: undefined reference to `__progname'
I don't think declaring these "extern" in your file is going to help.
I'm not sure, but I suspect these variables must be declared in the C
startup code - just guessing from their names. Try linking with gcc
instead of directly with ld. I've been getting away with linking
straight with ld in Linux, although some people have warned me that it
might not always work - maybe this is one of these times. See if:
gcc -s -o use_printf use_printf.o -lc
helps any. You'll have to change your entrypoint to "main", too -
"global main", and "main" where you've got "_start"... If it works, you
may be able to see what the command line to ld is (add a "-v" switch?)
and copy it to go back to linking direct with ld, if you want to.
> Here's what I have( I think I have commented out the bsd specific stuff... ):
>
> extern printf
No underscore for ELF - you got that right! Printf is *used* in your
program, but the function is elsewhere - that's what you want "extern" for.
> extern environ
> extern __progname
These *don't* appear in your program, so declaring them "extern" won't
do any good. I think the problem is that they're declared "extern'
somewhere else - maybe ld-elf.so? - and wherever they actually exist
isn't being linked in.
>
> section .data
> mesg db 'the number is %d\n',0
> mesglen equ $-mesg
>
> errormesg db 'libc error',0ah,0
> errormesglen equ $-errormesg
>
> newline db 10
> number dw 0x10
>
> ;kernel:
> ; int 80h
> ; ret
>
> align 4
> section .text
> global _start
> _start:
>
> push dword number
Number's only declared as a word, so you're pushing some garbage besides
your number. Actually, you're pushing the address of the number, not the
number itself. Try changing the number to "dd 0x10" instead of "dw
0x10", and "push dword [number]"...
> push dword mesg
> call printf
Probably want to "add esp, 8" here - C expects the caller to clean up
stack. This hasn't got anything to do with the problem you're having,
but once you get it to link, I think this'll cause a seg-fault somewhere
down the line.
You wanted to use "printf" 'cause it's "easier" than displaying the
number yourself, right? Maybe for floating point numbers it is...
Best,
Frank
- Previous message: Chewy509: "Re: printing bitmaps under dos"
- In reply to: Adam Bozanich: "using libc from nasm"
- Next in thread: Ulf Andersson: "Re: using libc from nasm"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|