IDIV - x86 vs. x64

Bare metal programming in PureBasic, for experienced users
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

IDIV - x86 vs. x64

Post 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!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: IDIV - x86 vs. x64

Post by wilbert »

Little John wrote:Is the code correct (for Windows, Mac, and Linux)?
It looks fine to me.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
idle
Always Here
Always Here
Posts: 5096
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: IDIV - x86 vs. x64

Post by idle »

looks right to me, works fine x64 linux
Windows 11, Manjaro, Raspberry Pi OS
Image
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: IDIV - x86 vs. x64

Post by Little John »

Many thanks, wilbert and idle!
Post Reply