axyd icon

Display 64bit MASM values using printf call

axyd | PRO | 06/04/17 08:50:01 PM UTC | 0 ⭐ | 1562 👁️ | Never ⏰ | []
ASM (NASM) |

0 B

|

None

|

0 👍

/

0 👎

;---------------------------------------------------;
;   App: MASM printf usage                          ;
;   Version: 1.0                                    ;
;---------------------------------------------------;
;   Date: 04 June 2017                              ;
;   Author: Jose Madureira                          ;
;   License: copy at will, just give credit         ;
;---------------------------------------------------;
 
; ## Global Constants ##
NL EQU 0Dh,0Ah      ;new line
TAB EQU 09h         ;tab character
STRMAX  EQU 21      ;max string size
T64 EQU TYPE RAX    ;64 bit size (8 BYTES)
 
; ## External Prototypes ## 
includelib msvcrt.lib   ;necessary for printf
EXTRN printf:PROC   ;display formatted string with args
 
ExitProcess PROTO   ;terminate program
 
 
.data
    msgNl byte NL,0     ;simply a new line
 
    ;simple formats
    msgInt byte "%d",0
    msgFlt byte "%f",0
    msgStr byte "%s",0
 
    ;test data
    valInt qword 666
    valFlt real8 6.54321
    valStr byte "This is not a test",0
    valStrNlTab byte NL,TAB,"new lined and tabbed",0
 
.code
PUBLIC Main
 
Main PROC
    ;display an integer
    mov rax,1
    mov rcx,offset msgInt   ;"%d"
    mov rdx,valInt          ;some integer value
    call PrintfMe64
 
    ;display new line using PrintfMe64
    mov rax,1
    mov rcx,offset msgStr   ;"%s"
    mov rdx,offset msgNl    ;crlf
    call PrintfMe64
    
    ;display a float
    mov rax,1
    mov rcx,offset msgFlt   ;"%f"
    mov rdx,valFlt          ;some float value
    call PrintfMe64
 
    ;display new line using PNL -> PrintfMe64
    call PNL
 
    ;display a string
    mov rax,1
    mov rcx,offset msgStr   ;"%s"
    mov rdx,offset valStr   ;some string
    call PrintfMe64
 
    ;display a string with new line and tab before it
    mov rax,1
    mov rcx,offset msgStr   ;"%s"
    mov rdx,offset valStrNlTab ;new line -> tab -> string
    call PrintfMe64
 
    
 
finish:
    call PNL
    call PNL
    call ExitProcess
 
Main ENDP
 
 
; *******************************************************************
PrintfMe64 PROC uses RAX
; * Purpose: display a 'c' style formated message and arguments     *
; *-----------------------------------------------------------------*
; * IN : RAX, number of arguments                                   *
; *      RCX, message with format flags                             *
; *      RDX...R15, arguments for formatted message                 *
; * OUT: none                                                       *
; *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
; * implementation idea:                                            *
; *     http://cx20.main.jp/blog/hello/2012/01/04/hello-masm-world  *
; *******************************************************************
    ;prevent disaster
    push RBP
    mov RBP, RSP
 
    imul RAX, 8         ;allocate additional 8 bytes per argument 
    add RAX, 64         ;(8*args) + 64 bytes
    mov [RBP], RAX      ;save total allocated local space   
    sub RSP, RAX        ;allocate local stack space
 
    call printf         ;display formated message
 
    add RSP, [RBP]      ;clean the stack
    pop RBP
 
    ret
PrintfMe64 ENDP
 
; *******************************************************************
PNL PROC uses RAX RCX RDX
; * Purpose: displays a new line                                    *
; *-----------------------------------------------------------------*
; * IN : none                                                       *
; * OUT: none                                                       *
; *******************************************************************
    mov rax,1
    mov rcx,offset msgStr       ;"%s"
    mov rdx,offset msgNl        ; cr lf
    call PrintfMe64
 
ret
PNL ENDP
 
END

Comments