Author Topic: Division by using Subtraction, I need help  (Read 591 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

.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


jj2007

  • Member
  • *****
  • Posts: 9802
  • Assembler is fun ;-)
    • MasmBasic
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   ; <<<<<<<<<<<<
ret
divt endp

tenkey

  • Regular Member
  • *
  • Posts: 19
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: 9802
  • Assembler is fun ;-)
    • MasmBasic
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
.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

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
.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