A Conversation for Assembly Language

Mistake?

Post 1

Future World Dictator (13)

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?

Post 2

hagbard

Yes, you're quite right. And considering ax is a 16 bit register, it will already overflow in the fourth iteration.


Mistake?

Post 3

Joe aka Arnia, Muse, Keeper, MathEd, Guru and Zen Cook (business is booming)

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?

Post 4

Is mise Duncan

See - in a HLL you would have spotted that straight away smiley - winkeye


Mistake?

Post 5

World Service Memoryshare team

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!

smiley - smiley Anna


Mistake?

Post 6

Is mise Duncan

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?

Post 7

hagbard

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?

Post 8

World Service Memoryshare team

Hi Everyone,

Can someone check what I've changed? I've got the feeling I've missed something out. Thanks smiley - smiley


Mistake?

Post 9

hagbard

I hate to break this to you smiley - sadface, 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.


Mistake?

Post 10

World Service Memoryshare team

Thanks for letting me know. I'll look into it smiley - smiley


Key: Complain about this post