Author Topic: printf working not correct in masm  (Read 7137 times)

BlackHat

  • Regular Member
  • *
  • Posts: 6
printf working not correct in masm
« 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.

jj2007

  • Member
  • *****
  • Posts: 13951
  • Assembly is fun ;-)
    • MasmBasic
Re: printf working not correct in masm
« Reply #1 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...)

BlackHat

  • Regular Member
  • *
  • Posts: 6
Re: printf working not correct in masm
« Reply #2 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.

jj2007

  • Member
  • *****
  • Posts: 13951
  • Assembly is fun ;-)
    • MasmBasic
Re: printf working not correct in masm
« Reply #3 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?

TWell

  • Member
  • ****
  • Posts: 743
Re: printf working not correct in masm
« Reply #4 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]

BlackHat

  • Regular Member
  • *
  • Posts: 6
Re: printf working not correct in masm
« Reply #5 on: June 22, 2017, 10:26:38 PM »
I'm trying to print value of this address, not the addess of variable.

aw27

  • Guest
Re: printf working not correct in masm
« Reply #6 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.

Vortex

  • Member
  • *****
  • Posts: 2791
Re: printf working not correct in masm
« Reply #7 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

caballero

  • Member
  • *****
  • Posts: 2158
  • Matrix - Noah
    • abre ojos ensamblador
Re: printf working not correct in masm
« Reply #8 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 that I did some years ago in nasm for dos that prints 65535*2 signed numbers in 2.56 seconds inside DosBOX
The logic of the error is hidden among the most unexpected lines of the program

BlackHat

  • Regular Member
  • *
  • Posts: 6
Re: printf working not correct in masm
« Reply #9 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?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: printf working not correct in masm
« Reply #10 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

BlackHat

  • Regular Member
  • *
  • Posts: 6
Re: printf working not correct in masm
« Reply #11 on: June 24, 2017, 04:17:49 AM »
So, have something like Solved?

HSE

  • Member
  • *****
  • Posts: 2499
  • AMD 7-32 / i3 10-64
Re: printf working not correct in masm
« Reply #12 on: June 24, 2017, 05:24:01 AM »
You can modify first post, and edit the topic, adding [solved].
Equations in Assembly: SmplMath

Vortex

  • Member
  • *****
  • Posts: 2791
Re: printf working not correct in masm
« Reply #13 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.