Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple question

Status
Not open for further replies.

elscorcho

IS-IT--Management
Mar 15, 2009
3
0
0
NO
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 ?

 
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!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top