Author Topic: Division by using Subtraction, I need help  (Read 2212 times)

xcbart

• Regular Member
• Posts: 8
Division by using Subtraction, I need help
« on: May 02, 2019, 04:33:22 AM »

Hi,
My program's supposed to perform division by using subtraction. However, I'm getting nonsense results as output. Could someone help me what's wrong with it? Thank you.

Code: [Select]
`include Irvine32.inc.datafirstPrompt byte "Please enter first integer : ",0secondPrompt byte Please enter second integer : ",0buffer_size = 10bufferFirst byte buffer_size dup(?)bufferSecond byte buffer_size dup(?)firstInt dword 0secondInt dword 0divResult dword 0.codemain proc call getInteger call divt mov edx,firstInt call writeInteger mov al,'/' call writechar mov edx,secondInt call writeInteger mov al,'=' call writechar mov eax,divResult call writeDec call crlf    myexit proc       mov eax, white+16*black       call settextcolor       call waitmsg       ret    myexit endp    main endpdiv proc pushad mov ecx,firstInt mov ebx,0 subtracting: sub ebx,secondInt loop subtracting mov divResult,ebx popad retdivt endpgetInteger proc mov edx, offset firstPrompt call writestring call readint mov firstInt, eax mov edx,offset secondPrompt call writestring call readint mov secondInt, eax retgetInteger endpwriteInteger proc mov eax,edx call writedec retwriteInteger endpend main`

jj2007

• Member
• Posts: 11552
• Assembler is fun ;-)
Re: Division by using Subtraction, I need help
« Reply #1 on: May 02, 2019, 05:46:16 AM »
How do you return your result?

Code: [Select]
` popad   ; <<<<<<<<<<<< retdivt endp`

tenkey

• Regular Member
• Posts: 39
Re: Division by using Subtraction, I need help
« Reply #2 on: May 02, 2019, 09:17:57 AM »
You need to rethink your division algorithm.  It's actually a multiplication algorithm.

jj2007

• Member
• Posts: 11552
• Assembler is fun ;-)
Re: Division by using Subtraction, I need help
« Reply #3 on: May 02, 2019, 09:36:48 AM »
I've had a second look, here is a version that works, using the initial idea to subtract the divisor result times from dividend:

Code: [Select]
`include \masm32\include\masm32rt.inc.datafirstInt dd 100   ; dividendsecondInt dd 15    ; divisordivResult dd ?     ; quotient.codedivt proc mov divResult, 0 mov ebx, firstInt subtracting: inc divResult sub ebx, secondInt ja subtracting retdivt endpstart:  call divt  inkey str\$(divResult), " is the result"  exitend start`

xcbart

• Regular Member
• Posts: 8
Re: Division by using Subtraction, I need help
« Reply #4 on: May 03, 2019, 03:05:34 AM »
I've had a second look, here is a version that works, using the initial idea to subtract the divisor result times from dividend:

Code: [Select]
`include \masm32\include\masm32rt.inc.datafirstInt dd 100   ; dividendsecondInt dd 15    ; divisordivResult dd ?     ; quotient.codedivt proc mov divResult, 0 mov ebx, firstInt subtracting: inc divResult sub ebx, secondInt ja subtracting retdivt endpstart:  call divt  inkey str\$(divResult), " is the result"  exitend start`

Thank you