The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: xerox on February 17, 2011, 06:12:30 PM

Title: Division Problem
Post by: xerox on February 17, 2011, 06:12:30 PM
Hi guys,
           How you guys doing today. I am completely new to the assembly language and in this forum too. I hope this forum will be helpful to learn new stuff and to solve my problem.
I am trying to do division, Here I have pasted my code and output. When I run the program I got the weird answer. I don't know what's going on. Please guys help me out.

INCLUDE Irvine32.inc
.data
   a DWORD 6
   b DWORD 28
   c1 DWORD 14
   d DWORD 4
   z DWORD ?
   tempVal1 Dword ?
   tempVal2 Dword ?

.code

main PROC
    mov eax,0
    mov ecx,8
    mov edx,4
    div ecx
    call WriteInt
    call DumpRegs

    exit
main ENDP

END main


output:

  -2147483648
  EAX=80000000  EBX=7FFD3000  ECX=00000008  EDX=00000000
  ESI=00000000  EDI=00000000  EBP=0012FF94  ESP=0012FF8C
  EIP=0040102B  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0

Press any key to continue . . .


Title: Re: Division Problem
Post by: dedndave on February 17, 2011, 06:20:10 PM
you are getting the proper result
the problem is that you are using a signed routine to display an unsigned result

replace WriteInt with WriteDec, and it should work as expected

for additional info on signed/unsigned context...
http://www.masm32.com/board/index.php?topic=16110.0
Title: Re: Division Problem
Post by: jj2007 on February 17, 2011, 06:34:35 PM
See edx::eax as one 64bit register, and div edx::eax, 8 as shr edx::eax, 3:

Quote   mov eax, 0
   mov ecx, 8
   mov edx, 4   ; 10000000000000000000000000000000000b
   div ecx   ; shr3: 10000000000000000000000000000000b

The result is correct indeed.