×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

simple question

simple question

simple question

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

sum dw ?

.code
extrn Writeint:proc, Crlf:proc

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

L1:
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 ?

RE: simple question

if I understood correctly:

make dx=1 and subtract from ax then add 2 to dx then add to ax.

pseudo code-ish:

ax=0h        ;start value=0h
dx=01h       ;start adjuster=01h
cx=01h       ;number of results
loop:        ;start of loop
sub ax,dx    ;perform first calculation in loop
call printax ;print result
dec cx       ;decrease number of results required
cmp cx,0h    ;if more results=0
je loopend   ;then jump to end
add dx,02h   ;adjust dx
add ax,dx    ;perform second calculation in loop
call printax ;print result
dec cx       ;decrease number of results required
cmp cx,0h    ;if more results=0
je loopend   ;then jump to end
add dx,02h   ;adjust dx
jmp loop     ;jump to start of loop
loopend:     ;no more results required, exit loop

i think something like this should produce then results you require. notice on each calculation 2 is added to dx and alternately a sub then an add is performed on ax.
i have used cx as the number of results required.
although the dec operation sets the zero field in flags i have put a compare operation for clarity.

i hope this helps.

"People who have nothing to say, say it too loud and have little knowledge. It's the quiet ones you need to worry about!"

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close