Author Topic: Dividing  (Read 4206 times)

Offline RagingGrim

  • Jr. Member
  • *
  • Posts: 28
Dividing
« on: December 24, 2014, 04:28:47 PM »
Code: [Select]
BITS 32
extern __cprintf
extern __getch
global main

section .data
IntFormat db "%i",0
RemainderFormat db ".%i",0ah,0
AnInt1 dd 0
AnInt2 dd 0

section .text
main:
;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, 0
mov ax, 64
mov bx, 17
div bx     ;ax.dx is the answer
mov [AnInt1] , ax
push dword [AnInt1]
push IntFormat
call __cprintf
add esp 8
mov [AnInt2], dx
push dword [AnInt2]
push RemainderFormat
call __cprintf
add esp,8
call __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 ! :D

Offline encryptor256

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

Code: [Select]
...
mov dx, 0
mov ax, 64
mov bx, 17
div bx     ;ax.dx is the answer
mov [AnInt1] , ax
mov [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.

Offline RagingGrim

  • Jr. Member
  • *
  • Posts: 28
Re: Dividing
« Reply #2 on: December 24, 2014, 05:31:46 PM »
Code: [Select]
BITS 32
extern __cprintf
extern __getch
global main

section .data
IntFormat db "%i",0
RemainderFormat db ".%i",0
AnInt1 dd 0
AnInt2 dd 0

section .text
main:
;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,0

mov eax, 13
mov ebx, 2
div ebx     ;ax.dx is the answer
mov [AnInt1] , eax
mov [AnInt2], edx
push dword [AnInt1]
push IntFormat
call __cprintf
push dword [AnInt2]
push RemainderFormat
call __cprintf

call __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 !

Offline 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 ^^

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
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,edx
mov eax, 13
mov ebx, 2
div ebx     ;ax.dx is the answer
mov [AnInt1] , eax
mov [AnInt2], edx


EAX should be 6 and EDX should be 1.
Encryptor256's Investigation \ Research Department.

Offline 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 »

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
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! :D
Encryptor256's Investigation \ Research Department.

Offline 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 ^^