Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

newbie - simple program adding three numbers

RodeoBurnzRodeoBurnz Member Posts: 47
hello, i am new and I am trying to write this code that adds three numbers. the problem i am having is that it only adds the last two numbers. my code is below(i am using MASM 6.11), please help. Thank you.

.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

INCLUDE io.h ; header file for input/output

cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed

.STACK 4096 ; reserve 4096-byte stack

.DATA ; reserve storage for data
number1 DWORD ?
number2 DWORD ?
number3 DWORD ?
prompt1 BYTE "Enter first number: ", 0
prompt2 BYTE "Enter second number: ", 0
prompt3 BYTE "Enter third number: ", 0
string BYTE 40 DUP (?)
label1 BYTE cr, Lf, "The sum is "
sum BYTE 11 DUP (?)
BYTE cr, Lf, 0

.CODE ; start of main program code
_start:
output prompt1 ; prompt for first number
input string, 40 ; read ASCII characters
atod string ; convert to integer
mov number1, eax ; store in memory

output prompt2 ; repeat for second number
input string, 40
atod string
mov number2, eax

output prompt3 ;repeat for third number
input string, 40
atod string
mov number3, eax

mov eax, number1 ; first number to EAX
add eax, number2 ; add second number
mov eax, number2 ; second number to EAX
add eax, number3 ; add third number
mov eax, number3 ; third number to EAX
dtoa sum, eax ; convert to ASCII characters
output label1 ; output label and sum

INVOKE ExitProcess, 0 ; exit with return code 0

PUBLIC _start ; make entry point public

END ; end of source code


Comments

  • jeffleydajeffleyda Member Posts: 390
    : hello, i am new and I am trying to write this code that adds three numbers. the problem i am having is that it only adds the last two numbers. my code is below(i am using MASM 6.11), please help. Thank you.

    ...

    :
    : mov eax, number1 ; first number to EAX
    : add eax, number2 ; add second number

    your result of number1+number2 is now in eax. So far so good.

    : mov eax, number2 ; second number to EAX

    whoops, you just nuked your value in eax because you just put number2 there.

    : add eax, number3 ; add third number
    : mov eax, number3 ; third number to EAX
    : dtoa sum, eax ; convert to ASCII characters
    : output label1 ; output label and sum


    try it like this:

    : mov eax, number1 ; first number to EAX
    : add eax, number2 ; add second number
    : add eax, number3 ; add third number
    : dtoa sum, eax ; convert to ASCII characters
    : output label1 ; output label and sum



  • RodeoBurnzRodeoBurnz Member Posts: 47
    Thank you so much. I appreciate it!!!

    : : hello, i am new and I am trying to write this code that adds three numbers. the problem i am having is that it only adds the last two numbers. my code is below(i am using MASM 6.11), please help. Thank you.
    :
    : ...
    :
    : :
    : : mov eax, number1 ; first number to EAX
    : : add eax, number2 ; add second number
    :
    : your result of number1+number2 is now in eax. So far so good.
    :
    : : mov eax, number2 ; second number to EAX
    :
    : whoops, you just nuked your value in eax because you just put number2 there.
    :
    : : add eax, number3 ; add third number
    : : mov eax, number3 ; third number to EAX
    : : dtoa sum, eax ; convert to ASCII characters
    : : output label1 ; output label and sum
    :
    :
    : try it like this:
    :
    : : mov eax, number1 ; first number to EAX
    : : add eax, number2 ; add second number
    : : add eax, number3 ; add third number
    : : dtoa sum, eax ; convert to ASCII characters
    : : output label1 ; output label and sum
    :
    :
    :
    :

  • Andre YoungAndre Young USAMember Posts: 0

    _________ ( http://forcoder.org ) free ebooks and video tutorials about / Python, Delphi, Java, C, Objective-C, Assembly, C++, PL/SQL, C#, Swift, Visual Basic, Go, R, PHP, Ruby, Scratch, MATLAB, Visual Basic .NET, JavaScript, Perl Apex, ABAP, Lisp, Julia, Prolog, Crystal, Dart, Logo, Ada, FoxPro, Erlang, Transact-SQL, Alice, Awk, Fortran, Clojure, Bash, Rust, ML, Hack, Scala, Lua, Kotlin, LabVIEW, F#, D, SAS, COBOL, VBScript, Scheme / ____________

Sign In or Register to comment.