News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

printf working not correct in masm

Started by BlackHat, June 22, 2017, 07:28:36 PM

Previous topic - Next topic

BlackHat

I'm just write a simple program forced format print integer from BYTE. But it work not correct.
Here is my code:

ExitProcess PROTO : DWORD
printf PROTO C :VARARG  ; The secret sauce.. a prototype of printf

.data
   format DB "%d", 13, 10, 0
   sum DWORD ?
.code
main PROC
   LOCAL something : BYTE
   mov al, 1
   mov BYTE ptr [something], al
   add BYTE ptr [something], 65
   lea eax, BYTE ptr[something]
   push eax
   lea eax, format
   push eax
   call printf

   xor eax, eax
   push eax
   call ExitProcess
main ENDP
END main


- I was debug and saw variable named "something", its value is correct.

jj2007

include \masm32\include\masm32rt.inc

.data
HelloW$      db "Hello World", 13, 10, 0

.code
start:
  printf(addr HelloW$)                  ; option A: the printf macro
  invoke crt_printf, addr HelloW$       ; option B: invoke crt_somefunction
  exit

end start


(and read the forum rules regarding black hat stuff...)

BlackHat

Well, I know how to print "Hello world", but when I move to kind print number, it got the error.

jj2007

include \masm32\include\masm32rt.inc

.data
HelloW$ db "Hello World #%i", 13, 10, 0
TheNumber dd 42


.code
start:
  printf(addr HelloW$, 42) ; option A: the printf macro
  invoke crt_printf, addr HelloW$, TheNumber ; option A: invoke crt_somefunction
  exit

end start


"when I move to kind print number" is google translate, I suppose?

TWell

use   format DB "%p", 13, 10, 0when printing a local variable address.
or for valuemovzx eax, BYTE ptr[something]

BlackHat

I'm trying to print value of this address, not the addess of variable.

aw27

Quote from: BlackHat on June 22, 2017, 07:28:36 PM
I'm just write a simple program forced format print integer from BYTE. But it work not correct.
Here is my code:

ExitProcess PROTO : DWORD
printf PROTO C :VARARG  ; The secret sauce.. a prototype of printf

.data
   format DB "%d", 13, 10, 0
   sum DWORD ?
.code
main PROC
   LOCAL something : BYTE
   mov al, 1
   mov BYTE ptr [something], al
   add BYTE ptr [something], 65
   lea eax, BYTE ptr[something]
   push eax
   lea eax, format
   push eax
   call printf

   xor eax, eax
   push eax
   call ExitProcess
main ENDP
END main


- I was debug and saw variable named "something", its value is correct.

For a start and even if you can't explain the error you get, probably because your English is too short for that, at least clean the stack when returning from a cdecl call.

Vortex

Hello,

Here is a similar version :

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc

printf      PROTO C :DWORD,:VARARG

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\msvcrt.lib

.data

format      db 'Address = %X , Value = %d',13,10,0

.code

start:

    call    main
    invoke  ExitProcess,0   

main PROC

LOCAL x:DWORD

    lea     edx,x
    mov     DWORD PTR [edx],0
    mov     al,1
    mov     BYTE PTR [edx],al
    add     BYTE PTR [edx],65

    push    DWORD PTR [edx]
    push    edx
    push    OFFSET format
    call    printf
    add     esp,3*4
    ret

main ENDP

END start

avcaballero

If you don't need a wide range number to print, here is another example, to print it by hand. Another example that I did some years ago in nasm for dos that prints 65535*2 signed numbers in 2.56 seconds inside DosBOX

BlackHat

I tried and finally find my ERROR is push the address of variable and print it. Thanks guy very much.
How can I close this topic?

hutch--

You don't have to close it, it remains for reference for any other member to read.

BlackHat


HSE

You can modify first post, and edit the topic, adding [solved].
Equations in Assembly: SmplMath

Vortex

Hi BlackHat,

You don't need to modify or add anything. The thread can stay as it is.