flat assembler
Message board for the users of flat assembler.

Index > Windows > scanf troubles

Author
Thread Post new topic Reply to topic
DStevens5833



Joined: 20 May 2011
Posts: 2
DStevens5833
Hello everyone! To be honest, I'm completely new to assembly programming; I've spend the last few days reading documentation and source code, and it has been flowing pretty well. So keep in mind that I'm new, and try not to make too much fun! Razz

Anyways, I'm having issues with the following code:
Code:
format PE console
entry start

include 'win32a.inc'

proc Addition stdcall uses ecx edx, a:DWORD, b:DWORD
     mov ecx, [a]
     mov edx, [b]
     add ecx, edx
     mov eax, ecx

     ret
endp

section '.data' data readable writeable

        _RequestInput   db      "Enter two numbers to add: ", 0
        _Result         db      "%d + %d = %d", 0
        _Input1         dd      "%d", 0
        _Input2         dd      "%d", 0

        _Number1        dd      ?
        _Number2        dd      ?

section '.text' code readable executable

        start:
                push _RequestInput
                call [printf]

                push _Number1
                push _Input1
                call [scanf]

                push _Number2
                push _Input2
                call [scanf]

                push _Number1
                push _Number2
                call Addition

                push eax
                push _Number1
                push _Number2
                push _Result
                call [printf]

                push 0
                call [ExitProcess]

section '.idata' import data readable

        library msvcrt, 'MSVCRT.DLL', \
                kernel, 'KERNEL32.DLL'

        import msvcrt, \
                       printf, 'printf', \
                       scanf, 'scanf'

        import kernel, \
                       ExitProcess, 'ExitProcess'   
    


Results:
Code:
Enter two numbers to add: 3
4
4202556 + 4202552 = 8405108
    

Obviously 3 + 4 != 8405108. Please, someone shed some light. Also, advice regarding a learning path is welcome and appreciated! Thank you!
Post 24 May 2011, 18:53
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
Hi! These should be changed:
Code:
push [_Number1]
push [_Number2]
call Addition

push eax
push [_Number1]
push [_Number2]
push _Result
call [printf] 
    
Post 24 May 2011, 19:42
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Code:
                push _Number1 
                push _Number2    


You're pushing the addresses of your variables, not their values.

Code:
                push [_Number1]
                push [_Number2]
                call Addition 

                push eax 
                push [_Number1]
                push [_Number2]
                push _Result 
                call [printf]      

_________________
This is a block of text that can be added to posts you make.
Post 24 May 2011, 19:44
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
DStevens5833



Joined: 20 May 2011
Posts: 2
DStevens5833
Such a simple mistake! It works now; thank you both for your help!
Post 24 May 2011, 19:52
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
In this particular case nothing will blow up but you should use "add esp, total_size_of_arguments" after each call to msvcrt functions (and wsprintf), where total_size_of_arguments in all your cases is the number of PUSHes multiplied by 4. (Using cinvoke would do this automatically for you as long as all the parameters are specified in the cinvoke call and not some of them pre pushed.)
Post 24 May 2011, 20:51
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.