Author Topic: use printf in asm  (Read 23917 times)

nobody

  • Guest
use printf in asm
« 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

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: use printf in asm
« Reply #1 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/

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

nobody

  • Guest
Re: use printf in asm
« Reply #2 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!

M. David Johnson

  • Guest
Re: use printf in asm
« Reply #3 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