ALINK and msvcrt.lib

Hi, I've just started with Assembler. I'm using NASM and ALINK.
I want to use printf function which is located in msvcrt.lib
I wrote something like this:
[code]
extern printf

global _start:
start:
do something...
push data to stack...
call printf
[/code]
but i got linker error: unresolved external printf

[code]nasm -fwin32 -oprog.obj prog.asm
alink -oPE -subsys con -entry _start -o prog.exe prog.obj
[/code]
How to use printf?

Comments

  • I don't have NASM. Personally, I use FASM.

    Anyway, try putting printf in brackets since you want the value at that location.

    [code]

    call [printf]

    [/code]

    Also, make sure you clean up the stack afterward since C functions use the __cdecl calling convention and require the caller to clean up the stack properly. In 32 bit, the stack is aligned in 4 bytes. So if I push 2 parameters on the stack, I need to add that many * 4 back to the stack pointer after the call to the function.

    [code]

    push para 2 ;parameters pushed on stack in reverse
    push para 1 ;pushed 8 bytes in total
    call [printf]
    add esp, 8 ;restore stack

    [/code]

  • Yes, I know and I solved my problem.
    It's [code]extern __cprintf[/code]
  • But I have a problem with printing numbers
    [code]extern __cprintf
    extern __cexit

    segment .code
    global _start
    _start:

    mov ebx, liczba
    call near printStr

    call near exit

    ret

    printWynik:
    mov ebx, eax
    call near printStr

    printStr:
    mov edx, strCd
    push edx
    push ebx
    call __cprintf
    pop edx
    pop ebx
    mov ebx, newLine
    push edx
    push ebx
    call __cprintf
    pop edx
    pop ebx
    xor edx, edx
    xor ebx, ebx
    ret

    strCd db "%p",0 ; if "%d" is here it doesn't work too
    newLine db 0Dh,0Ah,0

    exit:
    push 0
    call __cexit
    pop eax
    ret

    segment .data
    msg db "Hello world!",0
    msg2 db "Hello world second line",0
    liczba dq 1125[/code]
    It prints something like this: "e?".
    Why?
  • What are you trying to do? You didn't comment your program.

    You have "%p". This prints out the pointer address. And "dq"? Isn't that define quad, ie 8 bytes, and it is defined as a constant?

    Also, if you push 2 parameters:

    [code]
    push edx
    push ebx ;pushing 8 bytes total
    [/code]

    You need to do this after the function call
    [code]
    add esp, 8 ;add 8 bytes back to stack for 2 parameters
    [/code]

    Another thing, if you push, you pop in reverse order.
    [code]
    push edx
    push ebx

    pop ebx
    pop edx
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories