How to print only a certain number of bytes with C-printf() with Nasm assembler?

By : olmanqj
Source: Stackoverflow.com
Question!

I'm trying to print only a certain numbers of bytes of a string with printf of C. I'm working with Nasm assembler. The problem is that i can't use for example "%.5s", because the number of bytes I need to print changes.
I have something like this:

extern printf

section .data     
format:   db "%s",10,0      
msg:      db    "0123456789ABCDEF"

section .text   
    global main    
    main:

push msg       
push format                 
call printf           
add esp,12      

mov eax,1           
mov ebx,0        
int 80h
By : olmanqj


Answers

"*printf("(%.s)", n, string);" in assembly:

...
format: db "%.*s",10,0
n:      dd 5
...
push msg
push dword [n]
push format
call printf
add esp,12     ; 4 * count of pushes

Consider the "reverse" order of the pushes.

BTW: You forgot the last 0 in your msg.

By : rkhb


This video can help you solving your question :)
By: admin