The MASM Forum

General => The Campus => Topic started by: BlackHat on June 22, 2017, 07:28:36 PM

Title: printf working not correct in masm
Post by: 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:
Code: [Select]
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.
Title: Re: printf working not correct in masm
Post by: jj2007 on June 22, 2017, 09:27:31 PM
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...)
Title: Re: printf working not correct in masm
Post by: BlackHat on June 22, 2017, 09:48:36 PM
Well, I know how to print "Hello world", but when I move to kind print number, it got the error.
Title: Re: printf working not correct in masm
Post by: jj2007 on June 22, 2017, 09:54:38 PM
Code: [Select]
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?
Title: Re: printf working not correct in masm
Post by: TWell on June 22, 2017, 10:17:58 PM
use
Code: [Select]
   format DB "%p", 13, 10, 0when printing a local variable address.
or for value
Code: [Select]
movzx eax, BYTE ptr[something]
Title: Re: printf working not correct in masm
Post by: BlackHat on June 22, 2017, 10:26:38 PM
I'm trying to print value of this address, not the addess of variable.
Title: Re: printf working not correct in masm
Post by: aw27 on June 23, 2017, 01:09:57 AM
I'm just write a simple program forced format print integer from BYTE. But it work not correct.
Here is my code:
Code: [Select]
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.
Title: Re: printf working not correct in masm
Post by: Vortex on June 23, 2017, 05:12:25 AM
Hello,

Here is a similar version :

Code: [Select]
.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
Title: Re: printf working not correct in masm
Post by: caballero on June 23, 2017, 06:02:26 PM
If you don't need a wide range number to print, here is another example, to print it by hand. Another example (https://forum.nasm.us/index.php?action=dlattach;topic=1103.0;attach=90) that I did some years ago in nasm for dos that prints 65535*2 signed numbers in 2.56 seconds inside DosBOX
Title: Re: printf working not correct in masm
Post by: BlackHat on June 24, 2017, 03:48:31 AM
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?
Title: Re: printf working not correct in masm
Post by: hutch-- on June 24, 2017, 04:15:49 AM
You don't have to close it, it remains for reference for any other member to read.
Title: Re: printf working not correct in masm
Post by: BlackHat on June 24, 2017, 04:17:49 AM
So, have something like Solved?
Title: Re: printf working not correct in masm
Post by: HSE on June 24, 2017, 05:24:01 AM
You can modify first post, and edit the topic, adding [solved].
Title: Re: printf working not correct in masm
Post by: Vortex on June 24, 2017, 05:30:09 AM
Hi BlackHat,

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