News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Printf and Scanf in assembly

Started by g6g6, June 24, 2014, 06:08:36 AM

Previous topic - Next topic

g6g6

Hello, This is my code:

.386
.model flat,stdcall

option casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\msvcrt.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\masm32.lib
includelib \masm32\lib\msvcrt.lib
includelib \masm32\lib\kernel32.lib
.data

menss db "digita um numero:", 0
saida db "%d",0
saida2 db "x = %d",0

.data?

salvar dd ?

.code
start:
push offset menss
call crt_printf
push offset salvar ;push salvar
push offset saida
call crt_scanf
mov eax,offset salvar
push eax
push offset saida2
call crt_printf
; invoke crt_printf,addr saida2,salvar
push 0
call ExitProcess

end start

I want to digit a number, and my program will print this number.. But when I compile and run it:

C:\masm32\bin>ddd
digita um numero:3
x = 4206620

What is wrong?

Vortex

printf is a C function. If you don't use invoke to call the this function, you need to balance the stack manually.

g6g6

Tks vortex.But, when I use this code:
     invoke crt_printf,addr menss
     invoke crt_scanf, addr saida, addr salvar
     invoke crt_printf,addr saida2,addr salvar

The result is the same, just works when I use this: invoke crt_printf,addr menss
     invoke crt_scanf, addr saida, addr salvar
     invoke crt_printf,addr saida2,salvar

Why?

qWord

For printf integral types are passed by value. Also remarks that "%d" correspond to a DWORD sized integer value.
MREAL macros - when you need floating point arithmetic while assembling!

g6g6

Sorry, I continue not understanding... I should put " addr salvar". Because the integer is located in the addr of salvar.

qWord

Pass by value means to push the actual value on the stack rather than a pointer:
push salvar
push offset saida2
call crt_printf
add esp,2*8
MREAL macros - when you need floating point arithmetic while assembling!

g6g6


dedndave

it should work
try....
    push dword ptr salvar

g6g6

Works, but what is ptr? I tried search, but I did not find meaning

dedndave

PTR may be used in several ways (pointer)
however, in this case, it is a "size override" or "size specifier"
it simply tells the assembler what size the item is to PUSH