Author Topic: (Windows) printf/puts x64 macro  (Read 3249 times)

Offline shaynox

  • Full Member
  • **
  • Posts: 117
  • Country: fr
  • use Windows 10 x64
(Windows) printf/puts x64 macro
« on: January 15, 2015, 11:14:45 PM »
Hello, this sample code use macro for simplify the using of printf, i hope it's help you:

Code: [Select]
32_ dd 32.0

Code: [Select]
;==========================================================================
; printf_
;==========================================================================
; %0 = number of parameters received
; %1 = 1st parameter received
%macro printf_ 1-*
  %assign i  0

[section .data use64]
%%str db %1, 10, 0
[section .code use64]

sub rsp, 32
mov rcx, %%str ; printf.string

%if  %0 >= 2
mov rdx, %2 ; printf.1st element
%endif

%if  %0 >= 3
mov r8, %3 ; printf.2nd element
%endif

%if  %0 >= 4
mov r9, %4 ; printf.3nd element
%endif

%if  %0 >= 5
  sub rsp, ((%0 - 4) * 8) + 8 ; ((nbr_param - 4(param_nostack)) * 8(byte)) + 8(fill_last_element)
%rep (%0 - 4) ; Reapet block of code until all element are store in stack

mov rax, %5 ; Begin loop by 5th element.
mov [rsp + (32 + (i * 8))], rax

%assign i (i + 1)
%rotate 1 ; Rotate element right to left for scanning all element parsing in macro.
%endrep
  add rsp, ((%0 - 4) * 8) + 8
%endif

call    printf
add rsp, 32

%endmacro
;==========================================================================
; /printf_
;==========================================================================

;==========================================================================
; puts_
;==========================================================================
; %0 = number of parameters received
; %1 = 1st parameter received
%macro puts_ 1-*

[section .data use64]
%%str db %1, 10, 0
[section .code use64]

sub rsp, 32
mov rcx, %%str ; puts.string
call    puts
add rsp, 32

%endmacro
;==========================================================================
; /puts_
;==========================================================================


Code: [Select]
vcvtss2sd xmm0, [32_] ; float32 -> float64
vmovq rax, xmm0
printf_ "float = %f", rax

printf_ "int   = %d", 52

puts_ "blabla"

This macro allow to pass multiple parameters:

Code: [Select]
printf_ "blabla", ..., ...

Enjoy

PS: for understand those macro system, read http://www.nasm.us/doc/nasmdoc4.html#section-4.1.7.
« Last Edit: March 15, 2015, 06:49:53 PM by shaynox »
Basic asm standard:
reg(lowercase) - instruction(lowercase) -> instruction     dest, src

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Re: printf macro
« Reply #1 on: January 16, 2015, 04:39:56 PM »
You should state that this macro is specific to Windows x64 only.  It is not valid on any other system.