Simple join computing

Source: Internet
Author: User

Implement the sum of N1 +... + N2 for example: 43 + 44 +... + 63

 

Algorithm: N1 +... + n2 = (N1 + N2) * (n2-n1 + 1)/2

 

 

 

. 386
. Model stdcall, flat
Option Casemap: None

Includelib msvcrt. Lib
Printf proto C: DWORD,: vararg
Scanf proto C: DWORD,: vararg

. Data?
Num1 dd?
Num2 dd? ; // Result = num1 +... + num2
. Data
Szmsg dB 'Please input two num (for example: 43,63): ', 0
Szres dB '% d, % d', 0
Szfmt dB '% d +... + % d = % d', 0dh, 0ah, 0
. Code
Start:

Invoke printf, offset szmsg
Invoke scanf, offset szres, offset num1, offset num2

 

MoV eax, num1
Add eax, num2

MoV edX, num2
INC edX
Sub edX, num1
 
Mul edX
SAR eax, 1

Invoke printf, offset szfmt, num1, num2, eax
 
RET
End start

 

 

; /// Ml/C/coff my. ASM
; // Link/subsystem: Console My. OBJ

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.