NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: nobody on August 22, 2005, 09:23:02 AM

Title: use printf in asm
Post by: nobody on August 22, 2005, 09:23:02 AM
i want to use the c-function printf to print a number on screen. in the nasm manual i found this code:

extern  _printf

push    word [myint]
push    word mystring
call    _printf
add     sp,byte 4          

segment _DATA

myint         dw    1234
mystring      db    'This number -> %d <- should be 1234',10,0

can sb tell me how i get this code running? how do i have to compile it?
thx
Title: Re: use printf in asm
Post by: Frank Kotler on August 22, 2005, 10:07:05 AM
Well, looks like you've got 16-bit code there, so "nasm -f obj myprog.asm". Then you'll need to link the resulting "myprog.obj", using a 16-bit linker, against a 16-bit C library. What C tools are you using? Do you really want 16-bit code, in this day and age?

Good info on interfacing Nasm with (32-bit) C here:

http://www.drpaulcarter.com/pcasm/ (http://www.drpaulcarter.com/pcasm/)

Best,
Frank

P.S. Here's my Linux version:

; nasm -f elf hwso.asm
; ld -I/lib/ld-linux.so.2 -lc -s -o hwso hwso.o

global _start
extern printf

section .text
_start:

fld qword [anumber]
    sub esp, 8
    fstp qword [esp]
    push msg
    push fmt
    call printf
    add esp, 16

mov eax, 1
    xor ebx, ebx
    int 80h

;section .data
    fmt db "%s%f", 10, 0
    msg db "The answer is: ", 0
    anumber dq 42.0
Title: Re: use printf in asm
Post by: nobody on August 22, 2005, 09:27:42 PM
well, i also think that 32-bit code is what i want. i got that example for linux and it works very well. it is also possible to run it under windows?
thx for your help!
Title: Re: use printf in asm
Post by: M. David Johnson on September 07, 2005, 05:16:19 PM
Well, I'm brand new to NASM although I have many years of experience with MASM.

My first program was an attempt at _printf too.

Using MS Visual C++ 6's linker at the command prompt:

LINK myprog.obj mycprog.obj

where myprog.obj was obtained from:

nasmw myprog.asm -fwin32

and mycprog.obj was obtained from the VC++6 IDE compile of the single line mycprog.c which was:

#include

myprog.exe worked from the command prompt, exactly as expected.

Hope this helps.

M. David Johnson