Advertisement
axyd

Display 64bit MASM values using printf call

Jun 4th, 2017
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;---------------------------------------------------;
  2. ;   App: MASM printf usage                          ;
  3. ;   Version: 1.0                                    ;
  4. ;---------------------------------------------------;
  5. ;   Date: 04 June 2017                              ;
  6. ;   Author: Jose Madureira                          ;
  7. ;   License: copy at will, just give credit         ;
  8. ;---------------------------------------------------;
  9.  
  10. ; ## Global Constants ##
  11. NL EQU 0Dh,0Ah      ;new line
  12. TAB EQU 09h         ;tab character
  13. STRMAX  EQU 21      ;max string size
  14. T64 EQU TYPE RAX    ;64 bit size (8 BYTES)
  15.  
  16. ; ## External Prototypes ##
  17. includelib msvcrt.lib   ;necessary for printf
  18. EXTRN printf:PROC   ;display formatted string with args
  19.  
  20. ExitProcess PROTO   ;terminate program
  21.  
  22.  
  23. .data
  24.     msgNl byte NL,0     ;simply a new line
  25.  
  26.     ;simple formats
  27.     msgInt byte "%d",0
  28.     msgFlt byte "%f",0
  29.     msgStr byte "%s",0
  30.  
  31.     ;test data
  32.     valInt qword 666
  33.     valFlt real8 6.54321
  34.     valStr byte "This is not a test",0
  35.     valStrNlTab byte NL,TAB,"new lined and tabbed",0
  36.  
  37. .code
  38. PUBLIC Main
  39.  
  40. Main PROC
  41.     ;display an integer
  42.     mov rax,1
  43.     mov rcx,offset msgInt   ;"%d"
  44.     mov rdx,valInt          ;some integer value
  45.     call PrintfMe64
  46.  
  47.     ;display new line using PrintfMe64
  48.     mov rax,1
  49.     mov rcx,offset msgStr   ;"%s"
  50.     mov rdx,offset msgNl    ;crlf
  51.     call PrintfMe64
  52.    
  53.     ;display a float
  54.     mov rax,1
  55.     mov rcx,offset msgFlt   ;"%f"
  56.     mov rdx,valFlt          ;some float value
  57.     call PrintfMe64
  58.  
  59.     ;display new line using PNL -> PrintfMe64
  60.     call PNL
  61.  
  62.     ;display a string
  63.     mov rax,1
  64.     mov rcx,offset msgStr   ;"%s"
  65.     mov rdx,offset valStr   ;some string
  66.     call PrintfMe64
  67.  
  68.     ;display a string with new line and tab before it
  69.     mov rax,1
  70.     mov rcx,offset msgStr   ;"%s"
  71.     mov rdx,offset valStrNlTab ;new line -> tab -> string
  72.     call PrintfMe64
  73.  
  74.    
  75.  
  76. finish:
  77.     call PNL
  78.     call PNL
  79.     call ExitProcess
  80.  
  81. Main ENDP
  82.  
  83.  
  84. ; *******************************************************************
  85. PrintfMe64 PROC uses RAX
  86. ; * Purpose: display a 'c' style formated message and arguments     *
  87. ; *-----------------------------------------------------------------*
  88. ; * IN : RAX, number of arguments                                   *
  89. ; *      RCX, message with format flags                             *
  90. ; *      RDX...R15, arguments for formatted message                 *
  91. ; * OUT: none                                                       *
  92. ; *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  93. ; * implementation idea:                                            *
  94. ; *     http://cx20.main.jp/blog/hello/2012/01/04/hello-masm-world  *
  95. ; *******************************************************************
  96.     ;prevent disaster
  97.     push RBP
  98.     mov RBP, RSP
  99.  
  100.     imul RAX, 8         ;allocate additional 8 bytes per argument
  101.     add RAX, 64         ;(8*args) + 64 bytes
  102.     mov [RBP], RAX      ;save total allocated local space  
  103.     sub RSP, RAX        ;allocate local stack space
  104.  
  105.     call printf         ;display formated message
  106.  
  107.     add RSP, [RBP]      ;clean the stack
  108.     pop RBP
  109.  
  110.     ret
  111. PrintfMe64 ENDP
  112.  
  113. ; *******************************************************************
  114. PNL PROC uses RAX RCX RDX
  115. ; * Purpose: displays a new line                                    *
  116. ; *-----------------------------------------------------------------*
  117. ; * IN : none                                                       *
  118. ; * OUT: none                                                       *
  119. ; *******************************************************************
  120.     mov rax,1
  121.     mov rcx,offset msgStr       ;"%s"
  122.     mov rdx,offset msgNl        ; cr lf
  123.     call PrintfMe64
  124.  
  125. ret
  126. PNL ENDP
  127.  
  128. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement