CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Division

  1. #1
    Join Date
    Sep 2005


    I guys. New to assembly. I have listed my code below an the code solves the folowing problem. A*B -(A+B)/ (A-B). My code works until i get to the division part. Any suggestions would be very helpful. Thanks for reading!


    ExitProcess PROTO NEAR32 stdcall, dwExitCodeWORD

    INCLUDE io.h ; file for input/output

    cr EQU 0dh ; carriage return character
    Lf EQU 0ah ; line feed character

    .STACK 4096 ; 4096-byte stack is reserved

    .DATA ; storage for data reserved
    numberA DWORD ?
    numberB DWORD ?
    promptA BYTE "Enter first number: ", 0
    promptB BYTE "Enter second number: ", 0
    string BYTE 40 DUP (?)
    result BYTE cr, Lf, "The answer is "
    answer BYTE 11 DUP (?)
    BYTE cr, Lf, 0

    .CODE ; beginning of main program code
    output promptA ; prompts for the 1st number
    input string, 40 ; reads ASCII characters
    atod string ; converts to integer
    mov numberA, eax ; stores 1st number (A) in memory

    output promptB ; prompts for the 2nd number
    input string, 40 ; reads ASCII characters
    atod string ; converts to integer
    mov numberB, eax ; adds 2nd number to the 1st (A+B)

    mov eax, numberA ; puts 1st number in eax register (A)
    add eax, numberB ; adds 2nd number to the first (A+B)

    mov ebx, numberA ; puts 1st number in ebx register
    imul ebx, numberB ; multiplies 2nd and 1st number together
    sub ebx, eax ; subtracts (A+B) from (A*B)

    mov ecx, numberA ; puts 1st number in ecx register
    sub ecx, numberB ; subtracts 2nd number from 1st number (A-B)
    cwd ; preparing the divisor
    idiv ecx ; dividing by (A-B)

    dtoa answer, ebx ; converts to ASCII characters
    output result ; outputs result label and answer to problem

    INVOKE ExitProcess, 0 ; exits with return code 0

    PUBLIC _start ; make entry point public

    END ; end of code

  2. #2
    Join Date
    Jun 2005

    Re: Division

    "idiv ECX" is a 32-bit operation where the dividend is in EDX:EAX, and the divisor is in ECX.
    The previous line, "cwd" converts the 16-bit value in the AX register into an equivalent signed value in the DX:AX register pair, by duplicating the sign bit to the left. I'm not sure this is really useful to you. It seems that EAX is already holding a correct value. You only need to clear the EDX register, with "xor edx, edx".
    But, maybe there are other things I have not seen.

    Edit: Rather than "cdw" (or my "xor edx, edx"), it is "cdq" which is needed. But I read that they have the same opcode (99), they are giving the same results, because they act on AX/DX or on EAX/EDX depending whether it is a 16- or 32-bit segment, and whether there are any operand-size override prefixes.
    Last edited by olivthill; September 27th, 2005 at 03:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.