News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

simple question from a newbie

Started by iggy 504, July 04, 2006, 04:40:11 AM

Previous topic - Next topic

iggy 504

How does the idiv command work?  I tried doing "IDIV reg32, imm16" - didnt work.  reg32, imm32 didnt work.  nothing I could think of worked.  In fact when debugging the code, at that point in the program the debugger would terminate. 

Comments/Suggestions?

iggy 504

I am trying to write a code in masm that converts a signed integer into a ascii string.  This string then needs to be converted to octal base.  I have no clue on where to even start. 

Suggestions???

Synfire

IDIV only takes one argument, the src. This instruction uses EAX as the destination for the calculation. So when you call IDIV EBX you are dividing the value in EBX by the value in EAX, the results are then stored in EAX. A good place to look this stuff up is the file '\masm32\help\opcodes.hlp' file. Here is the contents of the page on IDIV:

QuoteIDIV - Signed Integer Division
        Usage:   IDIV   src
        Modifies flags: (AF,CF,OF,PF,SF,ZF undefined)
        Signed binary division of accumulator by source.  If source is a
        byte value, AX is divided by "src" and the quotient is stored in
        AL and the remainder in AH.  If source is a word value, DX:AX is
        divided by "src", and the quotient is stored in AL and the
        remainder in DX.

                                 Clocks                 Size

        Operands         808x  286   386   486          Bytes
        reg8          101-112   17    19    19            2
        reg16         165-184   25    27    27            2
        reg32            -      -     43    43            2
        mem8     (107-118)+EA   20    22    20           2-4

        mem16    (171-190)+EA   38    30    28           2-4  (W88=175-194)
        mem32            -      -     46    44           2-4

        F6 /7 IDIV r/m8 Signed divide
        F7 /7 IDIV r/m16 Signed divide
        F7 /7 IDIV r/m32 Signed divide

If you have any trouble with any opcodes, it's best to check that reference first.

Regards,
Bryant Keller

dsouza123

Using the terms for a fraction numerator/denominator  ie 51/5  --> 10  remainder 1
32 bit value divided by a 32 bit value, denominator in either a register or a dword/sdword variable.


; ecx holds the denominator
cdq      ;sign extented into edx
mov eax, numerator
idiv ecx       

cdq
mov eax, numerator
idiv denominator

; eax will hold quotient
; edx will hold remainder

mov ecx, 5

cdq
mov eax, 51
idiv ecx

; eax == 10    51 div 5   the quotient
; edx == 1     51 mod 5   the remainder

Roger

Hi Iggy,
Quote from: iggy 504 on July 04, 2006, 05:03:58 AM
I am trying to write a code in masm that converts a signed integer into a ascii string.  This string then needs to be converted to octal base.  I have no clue on where to even start. 
Allways start by thinking a little about the problem before thinking about the code.

Converting an ASCII string from a decimal to an octal base is a lot of work. So first convert your integer to octal and then secondly convert that to a string. Converting to octal means dividing by 8 rather than by 10 and this can be done by shifting the integer 3 (binary) places to the right.

If your program appens to need both types of string it would be easier to make each seperately rather than trying to convert one to the other.

Regards Roger

Ossa

Try the octal conversion routines here

Ossa
Website (very old): ossa.the-wot.co.uk