Author Topic: Dos box - division 64 bit by 64 bit  (Read 6975 times)

uni.stop

  • Guest
Dos box - division 64 bit by 64 bit
« on: June 21, 2013, 04:56:10 AM »
Hello,
I'm new here and I don't know if it is the right place to post my problem.

How can I implement 64 bit by 64 bit division in Assembly 8086?
I have already enabled extendeed registers with .386 directive.

Thanks!

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Dos box - division 64 bit by 64 bit
« Reply #1 on: June 21, 2013, 06:54:32 AM »
well - when you multiply a 32-bit value by another 32-bit value, you end up with as many as 64 bits
the opposite holds true when you divide - sort of   :P

but - the easy answer is to use the FPU
80-bit reals (extended reals) have 64-bits of precision

Ray has a nice tutorial to get you started, as well as a lib and some examples...

http://www.ray.masmcode.com/

nidud

  • Member
  • *****
  • Posts: 1980
    • https://github.com/nidud/asmc
Re: Dos box - division 64 bit by 64 bit
« Reply #2 on: June 21, 2013, 10:38:38 PM »
Code: [Select]
push ebx ; edx:eax / ecx
push esi
push edi
mov ebx,ecx
test edx,edx
jz DIV_00
test ebx,ebx
jnz DIV_01
DIV_00: div ebx
mov ebx,edx
xor edx,edx
jmp DIV_05
DIV_01: mov ecx,64
xor esi,esi
xor edi,edi
DIV_02: shl eax,1
rcl edx,1
rcl esi,1
rcl edi,1
cmp edi,0
jb DIV_04
ja DIV_03
cmp esi,ebx
jb DIV_04
DIV_03: sub esi,ebx
sbb edi,0
inc eax
DIV_04: dec ecx
jnz DIV_02
mov ebx,esi
DIV_05: mov ecx,ebx
pop edi
pop esi
pop ebx
ret

jcfuller

  • Member
  • **
  • Posts: 187
Re: Dos box - division 64 bit by 64 bit
« Reply #3 on: June 22, 2013, 12:41:26 AM »
Am I missing something here?
The topic says dosbox so I would assume 16bit assembly ???

James

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Dos box - division 64 bit by 64 bit
« Reply #4 on: June 22, 2013, 12:46:19 AM »
James,

Am I missing something here?
The topic says dosbox so I would assume 16bit assembly ???

the entire thing is a bit more tricky, because the thread is inside the 64 bit forum. Let's see what the thread starter has to say.

Gunther
Get your facts first, and then you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Dos box - division 64 bit by 64 bit
« Reply #5 on: June 22, 2013, 01:34:36 AM »
you can use 32-bit instructions in 16-bit code   :P

the real question is - what size is the divisor
larger dividends/quotients is easy enough

nidud

  • Member
  • *****
  • Posts: 1980
    • https://github.com/nidud/asmc
Re: Dos box - division 64 bit by 64 bit
« Reply #6 on: June 22, 2013, 02:47:27 AM »
the entire thing is a bit more tricky, because the thread is inside the 64 bit forum.

 :biggrin:

Code: [Select]
test rcx,rcx ; rdx:rax / rcx:rbx
jnz DIV_01
test rdx,rdx
jz DIV_00
test rbx,rbx
jnz DIV_01
DIV_00: div rbx
mov rbx,rdx
xor rdx,rdx
mov rcx,rdx
ret
DIV_01: push rbp
push rsi
push rdi
mov rbp,rcx
mov rcx,128
xor rsi,rsi
xor rdi,rdi
DIV_02: shl rax,1
rcl rdx,1
rcl rsi,1
rcl rdi,1
cmp rdi,rbp
jb DIV_04
ja DIV_03
cmp rsi,rbx
jb DIV_04
DIV_03: sub rsi,rbx
sbb rdi,rbp
inc rax
DIV_04: dec rcx
jnz DIV_02
mov rcx,rdi
mov rbx,rsi
pop rdi
pop rsi
pop rbp
ret

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Dos box - division 64 bit by 64 bit
« Reply #7 on: June 22, 2013, 05:16:33 AM »

Code: [Select]
    mov     rax,qwDividend
    xor     rdx,rdx
    div     qwDivisor

 :P

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Dos box - division 64 bit by 64 bit
« Reply #8 on: June 22, 2013, 05:34:43 PM »
This seems like an unreasonably difficult assignment for anyone new to programming. The Microsoft RTL function that does this was apparently coded in inline assembly in a naked function, using ~70 32-bit instructions.
Well Microsoft, here’s another nice mess you’ve gotten us into.

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Dos box - division 64 bit by 64 bit
« Reply #9 on: June 22, 2013, 10:30:37 PM »
long division is a good assembler exercise   :P