flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > 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.)
[SOLVED] Division With Negative Numbers
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 kernelExitProcess,'ExitProcess'
  import msvcrtprintf,'printf'
    import User32wsprintf,'wsprintfA'
section '.data' data readable writeable
  strInteger db '%d'1310 , 0
    strBuffer rb 64
     a_msg db 'a: '0
section '.code' code readable executable
start:
 mov eax, -22
        mov ebx10
 xor edxedx
        div ebx
     mov esieax
        ccall [wsprintf], strBufferstrIntegeresi
        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: 4641
Location: Argentina
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.)
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: 1219
Location: Toronto, Canada
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: 1953
Location: Usono (aka, USA)
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 cannot download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2013, Tomasz Grysztar.