ok so i was playing around with asm and did this simple program:
.model small
.stack 100h

sum dw ?

extrn Writeint:proc, Crlf:proc

main proc
mov ax,@data
mov ds,ax
mov dx,1
mov bx,10
mov ax,0

add ax,dx
add dx,dx
mov sum,ax
call Writeint
call Crlf
loop L1

mov ax,4c00h
int 21h

main endp
end main

....it adds 1+2+4+8+16+32.........+1024

im just wondering how would i change this code to do

-1 + 2 -3 +4 -5 + 6 .......+ (-1 )^n N ?
Posted on 2002-05-19 22:28:32 by asmbler
too tired to do anything truly useful, but...
-1 + 2 -3 +4 -5 + 6 .......+ (-1 )^n N ?

=(-1+2) + (-3+4)... + [(-1^n)*N]

where x is either 1 (if N is even) or x is -N (if N is odd)

my code might be slightly off, but i hope you get the point...
;assume ebx contains N
mov eax,ebx
shr eax,1 ;Divide eax by 2
jnc @F ;check to see if the number was odd. jump if it wasn't
sub eax,ebx
dec eax ;balances off for fall-through
inc eax

i'm assuming there are many more efficient ways to do this, but that should work...
Posted on 2002-05-20 00:23:55 by jademtech
I think this question got answered already in this thread < here >
Posted on 2002-05-20 02:27:46 by JimmyClif
don't double-post / cross-post.
I deleted your other Thread in Algorithms & Source Code.

Posted on 2002-05-20 03:59:40 by bazik
The problem with answering "school" problems is that there are usually much better solutions that your professer doesn't want you to use because they don't teach structure (which when learing is infinitly more valuable than clever algorithm deduction).

Your original problem shows exactly this problem too.
The sum of powers of 2 is equal to (2^(N+1) - 1).

Calculating powers of 2 is very easy, and you should be able to work it out...
Try working it out, and posting your idea here if you really are capable of programming yourself... If you don't I guess we can all assume you are just leeching brain time off others :rolleyes:

Posted on 2002-05-20 04:19:01 by Mirno