Hmmm... I don't see how you put 2 in cx and claim to be adding ten numbers!
But okay, looks good otherwise. The array of numbers you use won't overflow ax, but another array of numbers might. What would happen? "add ax, [bx]" would "wrap around" - 65535 + 1 = 0. This is "correct", we're doing modular arithmetic, but it probably isn't what the user expects. The carry flag is set if this happens, so "jc overflow" after the add would solve the problem. Or... we could "keep going" - "adc dx, 0" to make a 32-bit number in dx:ax (printing it is a PITA). A 32-bit number could overflow, too, so we'd better "jc really_bad_overflow" after the adc. Or we could keep going... "adc cx, 0"... oops, we're using cx... well, use memory when we run out of registers. This is how a "bignum" routine works, in fact. Unlike the C concept of "int", "the integers" are an infinite set of numbers, so you'll run out of memory before you run out of numbers... so we need to be prepared to print "overflow, sorry" at some point, no matter what we do.
The example I posted, assuming we were adding bytes to a word "sum" is the same thing in small scale. Another example (from the same file, as it happens - *that* was easy to find!
)... suppose we wanted to display a number in binary - I don't think printf has an option for this! (kinda useless - once we're comfortable with hex, we can "see" where the bits are without having 'em printed out...) In this case, the carry flag isn't being set by an "overflow", but is set/cleared by having a bit from the "number" shifted (or rotated) into it, and the "adc" is used to bump the character '0' to the character '1', if it's set...
;--------------------------
ax2bin:
push cx
push dx
mov cx, 16
.top
rcl ax, 1 ; rotate and set/clear carry
mov dl, '0'
adc dl, 0 ; make it '1' if carry set
push ax
mov ah, 2 ; print it
int 21h
pop ax
loop .top
pop dx
pop cx
ret
;----------------------------
"sbb" (subtract with borrow) is the "opposite" instruction - the "borrow flag" is the same thing as the "carry flag". There are some "tricks" involving sbb that can be used to eliminate conditional jumps (no examples on me, at the moment). The most common(?) use of adc/sbb is probably in "bignum" routines.
The best(?) tutorial using Nasm syntax is probably Dr. Paul Carter's
http://www.drpaulcarter.com/pcasm Several other posters seem to be using it (in a class, apparently). (I don't know if they're having trouble in spite of having read the material, or because they haven't read the material.
If you joined in, you'd have company, at least. In a way, it would be a step backwards for you - he provides a "print_int" routine that does what you're doing now. When the time comes to ditch the "training wheels", you'll be prepared! We've got a guy struggling with octal in a nearby thread - similar problems crop up... A possible disadvantage - if you don't already have a C compiler installed, you'll need one.
Jeff Duntemann's latest edition of "Assembly Language Step by Step" is out. It uses Nasm syntax, but Linux. His second edition uses dos, mostly, if you want to stick to dos. The first edition is Masm syntax. He's makin' progress!
I also like the "low level" approach in Jonathan Bartlett's "Programming From the Ground Up". Gas (AT&T) syntax, and Linux. Possibly not what you're looking for.
Randy Hyde's old, obsolete, 16-bit dos version of "Art of Assembly" is probably the most extensive tutorial you'll find. Masm syntax. The new AoA uses HLA as an assembler. Very different syntax. Not my cup 'o tea, but some people like it. Pity I don't like the syntax, because it's an excellent tutorial - portable between Windows, Linux, BSD, MacOSX... Randy provides his own library for all these, rather than "just call printf" (which is how Dr. Carter does it). Disclaimer: when the going gets tough, Randy does call libc in a few spots. His latest work, "HOWL" ("I saw the best minds of my generation destroyed by madness!" - sorry, couldn't help it), the HLA Object Windows Library looks... interesting... although I don't think OOP is "my thing", either. Massive amout of work has gone into all this! Won't hurt you to look at it...
http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/and his new tutorial, if Windows is your thing:
http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/Win32Asm/index.htmlWash your hands after.
I don't mean to rush you to abandon dos - it isn't a bad way to start learning assembly language - but you'll want to "graduate" to 32-bit code soon. It's a lot easier, in some ways. We all need to get into 64-bit code sometime - only a few "pioneers" have delved into it - not including me. It doesn't look any easier to me. But the world changes, and we have to keep up (or not). When you're ready...
Best,
Frank