flat assembler
Message board for the users of flat assembler.

Index > Windows > [SOLVED] Division With Negative Numbers

Author
Thread Post new topic Reply to topic
Apos



Joined: 11 Jan 2012
Posts: 17
Location: Paris, France (I'm not from France though.)
Apos
I am doing something wrong but I'm not sure what...

Code:
format PE console
entry start
include 'C:\fasm\include\win32ax.inc'
section '.idata' import data readable
 library kernel,'kernel32.dll', msvcrt,'msvcrt.dll', User32,'User32.dll'
       import kernel, ExitProcess,'ExitProcess'
  import msvcrt, printf,'printf'
    import User32, wsprintf,'wsprintfA'
section '.data' data readable writeable
  strInteger db '%d', 13, 10 , 0
    strBuffer rb 64
     a_msg db 'a: ', 0
section '.code' code readable executable
start:
 mov eax, -22
        mov ebx, 10
 xor edx, edx
        div ebx
     mov esi, eax
        ccall [wsprintf], strBuffer, strInteger, esi
        ccall [printf], a_msg
       ccall [printf], strBuffer
   stdcall [ExitProcess],0
    

Basically, I am doing an integer division: (-22) / 10
I would expect the result to be (-2) but instead, I get 429'496'727
The division works correctly with positive numbers.

_________________
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry


Last edited by Apos on 19 Feb 2012, 17:22; edited 1 time in total
Post 16 Feb 2012, 16:27
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Use idiv instruction.
[edit]And cdq instead of "xor edx, edx"
Post 16 Feb 2012, 16:31
View user's profile Send private message Reply with quote
Apos



Joined: 11 Jan 2012
Posts: 17
Location: Paris, France (I'm not from France though.)
Apos
That was fast!

Very interesting. I'll have to look into cdq, it looks like idiv doesn't give the right result without it.
Post 16 Feb 2012, 16:35
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
IDIV works with EDX:EAX and you set EDX to zero - EDX must have sign extended over all its bits.
CDQ does just that - it takes the bit sign from EAX (bit #31) and sets all bits in EDX to that bit value.
Post 16 Feb 2012, 17:33
View user's profile Send private message Send e-mail Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Post 16 Feb 2012, 21:39
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.