flat assembler
Message board for the users of flat assembler.

Index > Windows > last digit of big exponentations

Author
Thread Post new topic Reply to topic
alessandro95



Joined: 24 Mar 2013
Posts: 62
alessandro95
Dealing with project euler i wrote this program to find the last digit of a big exponentation:

Code:
format PE GUI 4.0
entry start

section 'code' code readable executable
start:
   mov eax,3 ;3=base
   xor ecx,ecx
   mov ebx,1
_loop:
   inc ecx
   push eax
   mul ebx
   mov edi,100000000
   div edi
   mov ebx,edx
   pop eax
   cmp ecx,50 ;50=exponent
   jb _loop

int3
retn
    


and when it finish running ebx contains 88770249 that actually are the last eight digits of 3^50 (=17897987691852588770249).
My problem is that i need the last 10 digits but 10000000000 is too big for a 32-bit register, is there an easy solution? should i rewrite it using 64 bit registers? and how does mul and div instructions work in long mode?

another question, why (in the loop):
Code:
   mov edi,100000000
   div edi
    

works fine,

Code:
   mov edx,100000000
   div edx
    

cause a division by zero and

Code:
   div 100000000
    

cannot even be compiled?

thank in advice for the answers!
Post 06 Apr 2013, 09:56
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2940
Location: vpcmipstrm
bitRAKE
Quote:
My problem is that i need the last 10 digits but 10000000000 is too big for a 32-bit register, is there an easy solution? should i rewrite it using 64 bit registers? and how does mul and div instructions work in long mode?
Larger size operations are definitely needed - as you know. Can either use 64-bit registers in longmode, or multi-part operations in 32-bit mode*. Longmode use of MUL/DIV support up to RDX:RAX and work similar to 32-bit mode.

DIV does not support an immediate value. As for division by zero - watch in debugger and it will be very clear. EDX:EAX is divided by EDX - in the loop EDX probably gets smaller and smaller until zero.

*Search the board for examples, and there is a tutorial on x86asm.net.

_________________
¯\(°_o)/¯ unlicense.org
Post 06 Apr 2013, 10:56
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
you maybe can to that in floating point.

fp numbers supports up to 19 digits numbers.

here, you cannot use edx as a divisor maybe cause it is the high part of the dividende also, it appears that even if it is overwriten, the edx value can be 0 during the division.


you don't need to reset the edi registerto 100000000 every loop.

what can be interresting is to try to read the edx value in the DIV#0! interupt.
if it is 100000000, it can be a cool hardware bug...
Post 06 Apr 2013, 19:15
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.