flat assembler
Message board for the users of flat assembler.

Index > Main > idiv -- drops an application down

Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Hi
It is strange to understand why this happens.
If I don't clear rdx before idiv, the next command will never run. The application drops with an error. But why? If there is something it must be divided as high qword in pair of rdx:rax, just arithmetic operation isn't it? Why Windows kills the process? Could someone explain?
Post 10 Jun 2021, 23:41
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Ups, wrong place I post it ))
Post 10 Jun 2021, 23:44
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18222
Location: In your JS exploiting you and your system
revolution
We don't know what you have done. Show some code.
Post 11 Jun 2021, 00:03
View user's profile Send private message Visit poster's website Reply with quote
Walter



Joined: 26 Jan 2013
Posts: 148
Walter
Isn't it usual to use the "cdq" instruction immediately before the idiv?
Post 11 Jun 2021, 01:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18222
Location: In your JS exploiting you and your system
revolution
There are two types of idiv instruction. Which is why I asked to see the code so we can see what is being done.

I expect the error reported is division by zero, but the OP doesn't say, so we can't know for sure. Division by zero is generated whenever the result can't fit into the destination. So it happens for more cases than just a zero divisor.
Post 11 Jun 2021, 01:46
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
I experimenting with some audio filters and forgot to clear rdx, that how I found it. There is non Zero for sure. Anyway, why OS deals with it?


Description:
Filesize: 36.81 KB
Viewed: 2164 Time(s)

Capture.PNG


Post 11 Jun 2021, 03:08
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Actually there have to be pair of idiv r12 idiv r13, I tried to optimise them to one divide. Anyway this filter is too slowly for me and I have better idea.
Post 11 Jun 2021, 03:33
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18222
Location: In your JS exploiting you and your system
revolution
You are using the single operand version of idiv. That means it takes rdx and rax as the numerator.

What is your error? It is division by zero, right? You forgot to say.

If the absolute value of rdx is greater than or equal to r12 then you will get a DivZero error. The result would be greater than 64 bits.

For example if we assume that each register is a single decimal digit:
30 / 3 needs two digits for the result and can't fit a single register.
Post 11 Jun 2021, 03:47
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Is that means "division by zero" where result doesn't feet to single register? Interesting...
Why OS then?
Post 11 Jun 2021, 03:55
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18222
Location: In your JS exploiting you and your system
revolution
The OS receives all CPU generated traps. It is how the CPU works.
Post 11 Jun 2021, 04:05
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Where to read about another implementation of idiv? I know only one. Don't you mistake with imul instruction?
Post 11 Jun 2021, 04:06
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Quote:

Isn't it usual to use the "cdq" instruction immediately before the idiv?

You right, cqo for rdx
Post 11 Jun 2021, 06:59
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1647
Furs
Overclick wrote:
If I don't clear rdx before idiv, the next command will never run.
You realize idiv takes rdx:rax as input, right? (or edx:eax if 32-bit operands)

Not clearing rdx means you have garbage values there. Even if it didn't crash, your results would be totally wrong.
Post 11 Jun 2021, 12:50
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Quote:

Even if it didn't crash, your results would be totally wrong

As I said I forgot to do that. Just interested why it can't be processed by developer as usual flag changing operation.
Post 11 Jun 2021, 15:43
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18222
Location: In your JS exploiting you and your system
revolution
Overclick wrote:
Just interested why it can't be processed by developer as usual flag changing operation.
Div0 is a trap, so it can't be detected through the flags.

If you use the FPU then you can set the control register to generate infinity. But no such equivalent exists for the integer core.

If you need to avoid the trap then you can compare rdx to the denominator and skip the division.
Code:
; for positive numbers
  cmp rdx, r12
  jae .too_large
  idiv r12
  ;...
.too_large:
  ;...    
But note that the code gets a bit more tricky if you have negative numbers.
Post 12 Jun 2021, 00:47
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 394
Location: Ukraine
Overclick
Too slowly all of that. Sadly there is not much options to divide integer, no one for SSE. Only bit shifting can help a little. I return to float, it working slower but need less operations to realise all I want. Classical FPU is absolutely slowmo. 10 times slower than integer when SSE just twice for single dword. Fully packed is ok.
Post 12 Jun 2021, 04:47
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
I noticed the same (in 32-bit):

Code:
0:  A1 04 10 40 00          MOV    EAX,DS:0x401004
5:  31 D2                   XOR    EDX,EDX
7:  BB 02 00 00 00          MOV    EBX,0X2
C:  F7 FB                   IDIV   EBX
E:  A3 04 10 40 00          MOV    DS:0x401004,EAX    


Generated for my own language:
Code:
LET b= 20
b /= 2    
(Assume "b" is located at DS:0x401004

Have to clear EDX. Thanks to this thread, now I understand far better.
Post 12 Jun 2021, 12:53
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18222
Location: In your JS exploiting you and your system
revolution
If you use IDIV then you might want to consider using CDQ instead of XOR EDX,EDX

Or alternatively if EAX is unsigned, use DIV, and keep the XOR EDX,EDX
Post 12 Jun 2021, 12:57
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 GitHub, YouTube, Twitter.

Website powered by rwasa.