using libc from nasm

From: Adam Bozanich (adambozanich_at_sbcglobal.net)
Date: 02/12/04


Date: Thu, 12 Feb 2004 20:38:23 +0000 (UTC)

Hi all. I am trying to learn asm with NASM on a FreeBSD system. 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

but then when I run the program:

$./use_printf
/usr/libexec/ld-elf.so.1: /lib/libc.so.5: Undefined symbol "environ"

Can somebody see where I am going wrong? This is kindof holding me back. I
added the 'extern environ' and 'extern __progname' beause I get this otherwise:

/usr/lib/libc.so: undefined reference to `environ'
/usr/lib/libc.so: undefined reference to `__progname'

Here's what I have( I think I have commented out the bsd specific stuff... ):

extern printf
extern environ
extern __progname

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
        push dword mesg
        call printf

        ; error if eax < 1 ( we should have wrote some chars )
        cmp eax , 0x1
        jl .error

        ; use write() system call to print message
; push dword mesglen
; push dword mesg
; push dword 0x1 ; stdout
; mov eax , 0x4 ; 4 == write system call
; call kernel

; ; output a newline
; push dword 1
; push dword newline
; push dword 0x1
; mov eax , 0x4
; call kernel

; mov eax , 0x1 ; exit syscall number
; push dword 0x0 ; exit status
; call kernel

.error:
; push dword errormesglen
; push dword errormesg
; push dword 0x1
; mov eax , 0x4
; call kernel

; mov eax , 0x1
; push dword 0xff
; call kernel



Relevant Pages

  • libc from asm
    ... added the 'extern environ' and 'extern __progname' beause I get this otherwise: ... ; push dword mesglen ... ; call kernel ...
    (comp.unix.programmer)
  • Re: JWasm v1.9
    ... StringOpenFailed db "Can't Open X display!", ... caption db 'A Window', 0 ... extern XDefaultRootWindow ... push dword; display handle ...
    (alt.lang.asm)
  • Re: File I/O using NASM in 32-bit mode
    ... extern WriteFile ... extern ExitProcess ... push dword num_chars ...
    (alt.lang.asm)
  • Problem with Function Call with NASM
    ... extern MessageBoxA ... push dword 0x05 ... mov dword edi, esp ... mov dword ecx, 64 ...
    (comp.lang.asm.x86)
  • Re: Problem with Function Call with NASM
    ... extern MessageBoxA ... push dword 0x05 ... sub esp, 64 ... mov dword edi, esp ...
    (comp.lang.asm.x86)