Page 1 of 1

IDIV - x86 vs. x64

Posted: Sun Jan 25, 2015 8:47 pm
by Little John
Hi,

I started ASM programming with 16 bit CPUs, later I wrote some small 32 bit ASM codes, and now I don't actually have a clue about 64 bit ASM. :-(

Inspired by a 32 bit ASM code by Fred, I wrote the following code:

Code: Select all

; PB 5.31

Procedure.i AsmDiv (x.i, n.i)
   CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      ! MOV  eax, [p.v_x]
      ! MOV  ecx, [p.v_n]
      ! CDQ
      ! IDIV ecx
      ; ! MOV  eax, edx       ; uncomment this to make it REMAINDER instead of DIV
   CompilerElse
      ! MOV  rax, [p.v_x]
      ! MOV  rcx, [p.v_n]
      ! CQO
      ! IDIV rcx      
      ; ! MOV  rax, rdx       ; uncomment this to make it REMAINDER instead of DIV
   CompilerEndIf
   
   ProcedureReturn            ; result is in eax or rax, respectively 
EndProcedure


Debug AsmDiv( 17,  5)
Debug AsmDiv(-17,  5)
Debug AsmDiv( 17, -5)
Debug AsmDiv(-17, -5)
Is the code correct (for Windows, Mac, and Linux)?
Thanks in advance!

Re: IDIV - x86 vs. x64

Posted: Mon Jan 26, 2015 6:11 am
by wilbert
Little John wrote:Is the code correct (for Windows, Mac, and Linux)?
It looks fine to me.

Re: IDIV - x86 vs. x64

Posted: Mon Jan 26, 2015 6:16 am
by idle
looks right to me, works fine x64 linux

Re: IDIV - x86 vs. x64

Posted: Mon Jan 26, 2015 8:08 am
by Little John
Many thanks, wilbert and idle!