### Author Topic: Dividing  (Read 11287 times)

#### RagingGrim

• Jr. Member
• Posts: 28
##### Dividing
« on: December 24, 2014, 04:28:47 PM »
Code: [Select]
`BITS 32extern __cprintfextern __getchglobal mainsection .dataIntFormat db "%i",0RemainderFormat db ".%i",0ah,0AnInt1 dd 0AnInt2 dd 0 section .textmain:;DIV performs unsigned integer division. The explicit operand provided is the divisor; the dividend and destination operands are implicit, in the following way:;For DIV r/m8, AX is divided by the given operand; the quotient is stored in AL and the remainder in AH.;For DIV r/m16, DX:AX is divided by the given operand; the quotient is stored in AX and the remainder in DX.;For DIV r/m32, EDX:EAX is divided by the given operand; the quotient is stored in EAX and the remainder in EDX.mov dx, 0mov ax, 64mov bx, 17div bx     ;ax.dx is the answermov [AnInt1] , axpush dword [AnInt1]push IntFormatcall __cprintfadd esp 8mov [AnInt2], dxpush dword [AnInt2]push RemainderFormatcall __cprintfadd esp,8call __getch`
So this is my code :/ Windows 7 Ultimate 64 bit.

I was just wondering why this doesn't work. The remainder is never correct :/
Could someone help me out?

Regarding something I noticed on another post (http://forum.nasm.us/index.php?topic=809.0/index.php?topic=809.0).
Shifting to the right with a value of one is the equivalent of dividing by two only this is much faster ? Although this wouldn't work on floating points am I correct ?

Thanks for the help !

#### encryptor256

• Full Member
• Posts: 250
• Country:
• Win64 .
##### Re: Dividing
« Reply #1 on: December 24, 2014, 05:25:15 PM »
Maybe try to save them just after division.

Code: [Select]
`...mov dx, 0mov ax, 64mov bx, 17div bx     ;ax.dx is the answermov [AnInt1] , axmov [AnInt2] , dx...`
EDIT:

I bet that the result and remainder after division is always correct.
There are human errors, that might be:
1. Division result or remainder is not saved at right time. (Like, just after division)
2. Division result or remainder is not displayed in appropriate way. (Display, Output, Print mistake)

Well... I don't know, try something.
« Last Edit: December 24, 2014, 05:35:03 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

#### RagingGrim

• Jr. Member
• Posts: 28
##### Re: Dividing
« Reply #2 on: December 24, 2014, 05:31:46 PM »
Code: [Select]
`BITS 32extern __cprintfextern __getchglobal mainsection .dataIntFormat db "%i",0RemainderFormat db ".%i",0AnInt1 dd 0AnInt2 dd 0 section .textmain:;DIV performs unsigned integer division. The explicit operand provided is the divisor; the dividend and destination operands are implicit, in the following way:;For DIV r/m8, AX is divided by the given operand; the quotient is stored in AL and the remainder in AH.;For DIV r/m16, DX:AX is divided by the given operand; the quotient is stored in AX and the remainder in DX.;For DIV r/m32, EDX:EAX is divided by the given operand; the quotient is stored in EAX and the remainder in EDX.mov edx,0mov eax, 13mov ebx, 2div ebx     ;ax.dx is the answermov [AnInt1] , eaxmov [AnInt2], edxpush dword [AnInt1]push IntFormatcall __cprintfpush dword [AnInt2]push RemainderFormatcall __cprintfcall __getch`
yields 6.1

Although now atleast if I divide something without a remainder the result is correct.

What am i doing wrong ? ^^

I realise the e.x registers are 32 bit and I don't need to use them for such small divisions but i was too lazy to edit them XD

I am however fiddling with the code waiting for a reply !

#### RagingGrim

• Jr. Member
• Posts: 28
##### Re: Dividing
« Reply #3 on: December 24, 2014, 05:38:52 PM »
I tested the output already and it's not that. I did save the values earlier and the result as I said is better but not yet correct.
I'll keep trying ^^

#### encryptor256

• Full Member
• Posts: 250
• Country:
• Win64 .
##### Re: Dividing
« Reply #4 on: December 24, 2014, 05:41:04 PM »
Code: [Select]
`mov edx,0 ; Better way to zero out register -> xor edx,edxmov eax, 13mov ebx, 2div ebx     ;ax.dx is the answermov [AnInt1] , eaxmov [AnInt2], edx`
EAX should be 6 and EDX should be 1.
Encryptor256's Investigation \ Research Department.

#### RagingGrim

• Jr. Member
• Posts: 28
##### Re: Dividing
« Reply #5 on: December 24, 2014, 05:41:40 PM »
Oh wait... I completely forgot what i was doing XDDD
your post was like a bloody smack in the face XD

Thank you so much!
Can someone delete this ?XD

for the record though XD I am not english and hence the mistake XD I was confusing remainder with the .thingy XD what do you call that ? XD Like 1.x
what is x ?
« Last Edit: December 24, 2014, 05:43:48 PM by RagingGrim »

#### encryptor256

• Full Member
• Posts: 250
• Country:
• Win64 .
##### Re: Dividing
« Reply #6 on: December 24, 2014, 05:43:23 PM »
Oh wait... I completely forgot what i was doing XDDD
your post was like a bloody smack in the face XD

Thank you so much!
Can someone delete this ?XD

No! We will keep it!
Encryptor256's Investigation \ Research Department.

#### RagingGrim

• Jr. Member
• Posts: 28
##### Re: Dividing
« Reply #7 on: December 24, 2014, 05:44:10 PM »
Ahahaha . I hate you .

jk jk XD thanks for the help though ^^