A Conversation for Assembly Language
Mistake?
Future World Dictator (13) Started conversation Nov 23, 2000
I'm no computer programmer, I'm just a humble undergrad mathematician, but surely that program doesn't print 3^10?
It multiplies ax by itself 10 times, updating ax each time, so the result should be 3^(2^10), which has nearly 500 digits (in decimal anyway).
Of course I've never even seen assembly code before...
Mistake?
hagbard Posted Nov 23, 2000
Yes, you're quite right. And considering ax is a 16 bit register, it will already overflow in the fourth iteration.
Mistake?
Joe aka Arnia, Muse, Keeper, MathEd, Guru and Zen Cook (business is booming) Posted Nov 24, 2000
I offer my profuse apologies then... this was one of my first entries and unfortunately it shows. I stick to maths now...
*scurries off to work on differentiation article*
Mistake?
World Service Memoryshare team Posted Nov 24, 2000
Hi Eternally Confused,
I'm happy to correct the formula, but being a humble editor and not knowing too much about maths, can you let me know exactly how the sentence should read? Thanks!
Anna
Mistake?
Is mise Duncan Posted Nov 24, 2000
I personally think it should be left wrong, as an example of what a total pain it is to debug Assembler but....
mov ax, 03 ; Move the value 3 into the accumulator
mov dx, 03 ; Move the value 3 into the data register
mov cx, 0A ; Move the value 10 into the counter
power_loop: mul ax, dx ; Multiply the accumulator by the value in the data register
sub cx, 01 ; Subtract 1 from the counter
cmp cx, 00 ; Compare the counter to zero
jnz power_loop ; If the counter isn't zero then jump to 'power_loop'
print ; Print using the UCR Standard library
byte ax ; Output the result of raising 3 to the tenth power
byte 0
Although even then I think its going through the loop too many times and the comparison line should be:
cmp cx, 01 ; Compare the counter to one
Hmm - see. Ghastly language!
Compare with HLL:
debug.Print 3^10
Mistake?
hagbard Posted Nov 24, 2000
Unfortunately that still won't work because in 16 bit multiplications, dx contains the high word of the result, so after the first multiplication it will be zero.
You're right about the off-by-one error though, because by doing 'mov ax,3' you've already calculated 3^1, so only 9 iterations are needed.
I propose the following code:
mov ax, 03
mov bx, ax
mov cx, 09
power_loop: mul bx
loopnz power_loop
print ; Print using the UCR Standard library
byte ax ; Output the result of raising 3 to the tenth power
byte 0
After this dx:ax contains 0000:E6A9
It may be more esthetic (but slower) to use as initial values ax=1, bx=3, cx=10d
Mistake?
World Service Memoryshare team Posted Nov 27, 2000
Hi Everyone,
Can someone check what I've changed? I've got the feeling I've missed something out. Thanks
Mistake?
hagbard Posted Mar 23, 2001
I hate to break this to you , but your code still doesn't work. By coding "mul ax" you're calculating ax * ax -> dx:ax, whereas what you really want is ax * 3 -> dx:ax. You can fix this by using bx as shown in my previous post.
Key: Complain about this post
Mistake?
- 1: Future World Dictator (13) (Nov 23, 2000)
- 2: hagbard (Nov 23, 2000)
- 3: Joe aka Arnia, Muse, Keeper, MathEd, Guru and Zen Cook (business is booming) (Nov 24, 2000)
- 4: Is mise Duncan (Nov 24, 2000)
- 5: World Service Memoryshare team (Nov 24, 2000)
- 6: Is mise Duncan (Nov 24, 2000)
- 7: hagbard (Nov 24, 2000)
- 8: World Service Memoryshare team (Nov 27, 2000)
- 9: hagbard (Mar 23, 2001)
- 10: World Service Memoryshare team (Mar 26, 2001)
More Conversations for Assembly Language
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."