Prompt the user and read the numbers a and b to be divided. Examine the signs of
ID: 3770511 • Letter: P
Question
Prompt the user and read the numbers a and b to be divided. Examine the signs of a and b to determine whether the result will be negative or positive. Then calculate the quotient a / b and the remainder using |a| and |b|, and print the quotient and remainder, with the appropriate signs. Put this in a loop so that the user can enter multiple pairs of numbers and perform division on them. Implementing the Division Set the quotient to 0. Shift the divisor left until it is greater than the dividend, then shift it right 1 position. This lines up the divisor under the leading digit of the dividend. Keep track of how many positions you shifted; you will use this to control the division loop. In the example shown, we "bring down" digits from the dividend until this intermediate dividend is greater than or equal to the divisor. Remember, in our code we don't "bring down" digits, instead we shift the divisor to the right at each step. For each position you shifted to line up the divisor and dividend: Shift the quotient left to add a zero to the end. If the divisor > dividend, we can't do anything. If divisor is <= dividend, this means that at this step we can subtract the divisor from the intermediate dividend and we get a 1 in the quotient. To put a 1 in the quotient, OR the quotient with 1. You have already shifted it, so you just need to change the low order digit from 0 to 1. Note that you could add 1, but OR is faster. Shift the divisor to the right to prepare for the next step. When you are done, the dividend is the remainder.
write in assembly language
Explanation / Answer
Program:
_TEXT SEGMENT
push ebp
mov ebp, esp
sub esp, 244 ; 000000f4H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-244]
mov ecx, 61 ; 0000003dH
mov eax, -858993460 ; ccccccccH
rep stosd
mov DWORD PTR _quotient$[ebp], 1
mov DWORD PTR _neg$[ebp], 1
divisor>0))
cmp DWORD PTR _dividend$[ebp], 0
jle SHORT $LN10@division
cmp DWORD PTR _divisor$[ebp], 0
jl SHORT $LN11@division
$LN10@division:
cmp DWORD PTR _dividend$[ebp], 0
jge SHORT $LN12@division
cmp DWORD PTR _divisor$[ebp], 0
jle SHORT $LN12@division
$LN11@division:
mov DWORD PTR _neg$[ebp], -1
$LN12@division:
: dividend;
cmp DWORD PTR _dividend$[ebp], 0
jge SHORT $LN15@division
mov eax, DWORD PTR _dividend$[ebp]
neg eax
mov DWORD PTR tv70[ebp], eax
jmp SHORT $LN16@division
$LN15@division:
mov ecx, DWORD PTR _dividend$[ebp]
mov DWORD PTR tv70[ebp], ecx
$LN16@division:
mov edx, DWORD PTR tv70[ebp]
mov DWORD PTR _tempdividend$[ebp], edx
divisor;
cmp DWORD PTR _divisor$[ebp], 0
jge SHORT $LN17@division
mov eax, DWORD PTR _divisor$[ebp]
neg eax
mov DWORD PTR tv73[ebp], eax
jmp SHORT $LN18@division
$LN17@division:
mov ecx, DWORD PTR _divisor$[ebp]
mov DWORD PTR tv73[ebp], ecx
$LN18@division:
mov edx, DWORD PTR tv73[ebp]
mov DWORD PTR _tempdivisor$[ebp], edx
mov eax, DWORD PTR _tempdivisor$[ebp]
cmp eax, DWORD PTR _tempdividend$[ebp]
jne SHORT $LN9@division
mov DWORD PTR ?rem@@3HA, 0 ; rem
mov eax, DWORD PTR _neg$[ebp]
jmp $LN13@division
jmp SHORT $LN4@division
$LN9@division:
mov eax, DWORD PTR _tempdividend$[ebp]
cmp eax, DWORD PTR _tempdivisor$[ebp]
jae SHORT $LN4@division
cmp DWORD PTR _dividend$[ebp], 0
jge SHORT $LN6@division
mov eax, DWORD PTR _tempdividend$[ebp]
imul eax, DWORD PTR _neg$[ebp]
mov DWORD PTR ?rem@@3HA, eax ; rem
jmp SHORT $LN5@division
$LN6@division:
mov eax, DWORD PTR _tempdividend$[ebp]
mov DWORD PTR ?rem@@3HA, eax ; rem
$LN5@division:
xor eax, eax
jmp SHORT $LN13@division
$LN4@division:
mov eax, DWORD PTR _tempdivisor$[ebp]
shl eax, 1
cmp eax, DWORD PTR _tempdividend$[ebp]
ja SHORT $LN3@division
mov eax, DWORD PTR _tempdivisor$[ebp]
shl eax, 1
mov DWORD PTR _tempdivisor$[ebp], eax
mov eax, DWORD PTR _quotient$[ebp]
shl eax, 1
mov DWORD PTR _quotient$[ebp], eax
jmp SHORT $LN4@division
$LN3@division:
cmp DWORD PTR _dividend$[ebp], 0
jge SHORT $LN2@division
mov esi, DWORD PTR _quotient$[ebp]
imul esi, DWORD PTR _neg$[ebp]
mov eax, DWORD PTR _divisor$[ebp]
push eax
mov ecx, DWORD PTR _tempdividend$[ebp]
sub ecx, DWORD PTR _tempdivisor$[ebp]
neg ecx
push ecx
call ?division@@YAHHH@Z ; division
add esp, 8
add esi, eax
mov DWORD PTR _quotient$[ebp], esi
jmp SHORT $LN1@division
$LN2@division:
mov esi, DWORD PTR _quotient$[ebp]
imul esi, DWORD PTR _neg$[ebp]
mov eax, DWORD PTR _divisor$[ebp]
push eax
mov ecx, DWORD PTR _tempdividend$[ebp]
sub ecx, DWORD PTR _tempdivisor$[ebp]
push ecx
call ?division@@YAHHH@Z ; division
add esp, 8
add esi, eax
mov DWORD PTR _quotient$[ebp], esi
$LN1@division:
mov eax, DWORD PTR _quotient$[ebp]
$LN13@division:
pop edi
pop esi
pop ebx
add esp, 244 ; 000000f4H
cmp ebp, esp
call __RTC_CheckEsp
mov esp, ebp
pop ebp
ret 0
?division@@YAHHH@Z ENDP ; division
_TEXT ENDS
PUBLIC _main
_TEXT SEGMENT
_ch$ = -29 ; size = 1
_divisor$ = -20 ; size = 4
_dividend$ = -8 ; size = 4
_main PROC ; COMDAT
push ebp
mov ebp, esp
sub esp, 228 ; 000000e4H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-228]
mov ecx, 57 ; 00000039H
mov eax, -858993460 ; ccccccccH
rep stosd
mov BYTE PTR _ch$[ebp], 121 ; 00000079H
$LN2@main:
movsx eax, BYTE PTR _ch$[ebp]
cmp eax, 121 ; 00000079H
jne $LN3@main
mov esi, esp
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
mov esi, esp
lea eax, DWORD PTR _dividend$[ebp]
push eax
call DWORD PTR __imp__scanf
add esp, 8
cmp esi, esp
call __RTC_CheckEsp
mov esi, esp
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
mov esi, esp
lea eax, DWORD PTR _divisor$[ebp]
push eax
call DWORD PTR __imp__scanf
add esp, 8
cmp esi, esp
call __RTC_CheckEsp
mov eax, DWORD PTR _divisor$[ebp]
push eax
mov ecx, DWORD PTR _dividend$[ebp]
push ecx
call ?division@@YAHHH@Z ; division
add esp, 8
mov esi, esp
push eax
mov edx, DWORD PTR _divisor$[ebp]
push edx
mov eax, DWORD PTR _dividend$[ebp]
push eax
call DWORD PTR __imp__printf
add esp, 16 ; 00000010H
cmp esi, esp
call __RTC_CheckEsp
mov esi, esp
mov eax, DWORD PTR ?rem@@3HA ; rem
push eax
mov ecx, DWORD PTR _divisor$[ebp]
push ecx
mov edx, DWORD PTR _dividend$[ebp]
push edx
call DWORD PTR __imp__printf
add esp, 16 ; 00000010H
cmp esi, esp
call __RTC_CheckEsp
mov esi, esp
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
mov esi, esp
lea eax, DWORD PTR _ch$[ebp]
push eax
call DWORD PTR __imp__scanf
add esp, 8
cmp esi, esp
call __RTC_CheckEsp
jmp $LN2@main
$LN3@main:
xor eax, eax
push edx
mov ecx, ebp
push eax
lea edx, DWORD PTR $LN9@main
call @_RTC_CheckStackVars@8
pop eax
pop edx
pop edi
pop esi
pop ebx
add esp, 228 ; 000000e4H
cmp ebp, esp
call __RTC_CheckEsp
mov esp, ebp
pop ebp
ret 0
npad 2
$LN9@main:
DD 3
DD $LN8@main
$LN8@main:
DD -8 ; fffffff8H
DD 4
DD $LN5@main
DD -20 ; ffffffecH
DD 4
DD $LN6@main
DD -29 ; ffffffe3H
DD 1
DD $LN7@main
$LN7@main:
DB 99 ; 00000063H
DB 104 ; 00000068H
DB 0
$LN6@main:
DB 100 ; 00000064H
DB 105 ; 00000069H
DB 118 ; 00000076H
DB 105 ; 00000069H
DB 115 ; 00000073H
DB 111 ; 0000006fH
DB 114 ; 00000072H
DB 0
$LN5@main:
DB 100 ; 00000064H
DB 105 ; 00000069H
DB 118 ; 00000076H
DB 105 ; 00000069H
DB 100 ; 00000064H
DB 101 ; 00000065H
DB 110 ; 0000006eH
DB 100 ; 00000064H
DB 0
_main ENDP
_TEXT ENDS
END
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.