The MASM Forum

Miscellaneous => Irvine Book Questions. => Topic started by: xcbart on May 02, 2019, 04:33:22 AM

Title: Division by using Subtraction, I need help
Post by: xcbart 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.



include Irvine32.inc

.data

firstPrompt byte "Please enter first integer : ",0
secondPrompt byte Please enter second integer : ",0

buffer_size = 10

bufferFirst byte buffer_size dup(?)
bufferSecond byte buffer_size dup(?)
firstInt dword 0
secondInt dword 0

divResult dword 0


.code

main 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 endp


div proc
pushad

mov ecx,firstInt
mov ebx,0
subtracting:
sub ebx,secondInt
loop subtracting
mov divResult,ebx
popad
ret
divt endp

getInteger proc
mov edx, offset firstPrompt
call writestring
call readint
mov firstInt, eax
mov edx,offset secondPrompt
call writestring
call readint
mov secondInt, eax
ret

getInteger endp

writeInteger proc
mov eax,edx
call writedec
ret
writeInteger endp

end main

Title: Re: Division by using Subtraction, I need help
Post by: jj2007 on May 02, 2019, 05:46:16 AM
How do you return your result?

popad   ; <<<<<<<<<<<<
ret
divt endp
Title: Re: Division by using Subtraction, I need help
Post by: tenkey on May 02, 2019, 09:17:57 AM
You need to rethink your division algorithm.  It's actually a multiplication algorithm.
Title: Re: Division by using Subtraction, I need help
Post by: jj2007 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:

include \masm32\include\masm32rt.inc
.data
firstInt dd 100   ; dividend
secondInt dd 15    ; divisor
divResult dd ?     ; quotient

.code
divt proc
mov divResult, 0
mov ebx, firstInt
subtracting:
inc divResult
sub ebx, secondInt
ja subtracting
ret
divt endp
start:
  call divt
  inkey str$(divResult), " is the result"
  exit
end start
Title: Re: Division by using Subtraction, I need help
Post by: xcbart on May 03, 2019, 03:05:34 AM
Quote from: jj2007 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:

include \masm32\include\masm32rt.inc
.data
firstInt dd 100   ; dividend
secondInt dd 15    ; divisor
divResult dd ?     ; quotient

.code
divt proc
mov divResult, 0
mov ebx, firstInt
subtracting:
inc divResult
sub ebx, secondInt
ja subtracting
ret
divt endp
start:
  call divt
  inkey str$(divResult), " is the result"
  exit
end start


Thank you