Pelles C forum

Assembly language => Assembly discussions => Topic started by: Vortex on October 10, 2026, 08:50:06 PM

Title: Magic division
Post by: Vortex on October 10, 2026, 08:50:06 PM
Here is an example of an alternative division method :

.386
.model flat,stdcall
option casemap:none

printf PROTO C :DWORD,:VARARG

ExitProcess PROTO :DWORD

.data

msg         db '4500 / 3 = %u',0

.code

start:

    mov     eax,4500
    mov     edx,0AAAAAAABh
    mul     edx
    shr     edx,1
    invoke  printf,ADDR msg,edx

    invoke  ExitProcess,0

END start
Title: Re: Magic division
Post by: Vortex on October 10, 2026, 09:52:09 PM
Here is the signed version :

.386
.model flat,stdcall
option casemap:none

printf PROTO C :DWORD,:VARARG

ExitProcess PROTO :DWORD

.data

msg         db '-15000 / 3 = %d',0

.code

start:

    mov     eax,-15000
    mov     ecx,eax
    mov     edx,055555556h
    imul    edx
    sar     ecx,31
    sub     edx,ecx
    invoke  printf,ADDR msg,edx

    invoke  ExitProcess,0

END start