Please do not copy the other answers they do not work, thanks X86 Assembly, usin
ID: 3822517 • Letter: P
Question
Please do not copy the other answers they do not work, thanks
X86 Assembly, using visual studios
Write a subroutine named ShiftMultiplication that multiplies any unsigned 32-bit integer byEAX:
-Useonly shifts and addition instructions.
-Pass the integer to the subroutine in the EBX register, and return the product in the EAX register.
-Assume that the product is never larger than 32 bits.
-You are only allowed to use a loop(do-while)to shift the multiplier to the right, keeping track of the number of shifts that occur.
-The resulting shift count can then be applied to the SHL instruction, using the multiplicand as the destination operand.
-Then, the same process must be repeated until you find the last 1 bit in the multiplier.
-You are not allowed to use the loop instruction and are not allowed to use the factoring method.
-Run your program using the debugger to verify your answers.
-The main program that calls the subroutine and displays the product is given to you as shown below:
oSubmit the following:
Lastname3.asm
INCLUDE Irvine32.inc
.data
p1 BYTE "Enter the multiplicand: ",0
p2 BYTE "Enter the multiplier: ",0
p3 BYTE "The product is ",0
.codemain PROC
call Clrscr;
Input the multiplicand
mov edx,OFFSET p1
call WriteString
call ReadDec ;ReadDec stores the first input (multiplicand) in eax
mov ebx,eax ;save eax in ebx so the next ReadDec will not overwrite it
;
Input the multiplier
mov edx, OFFSET p2
call WriteString
call ReadDec ;ReadDec stores the second input (multiplier) in eax
call Crlf
; multiply EBX by EAX, producing EAX
call Multiply
; Display the product in EAX
mov edx,OFFSET p3
call WriteString
call WriteDec
call Crlf
Here is a picture
3. (5 points Write a subroutine named ShiftMultiplication that multiplies any unsigned 32-bit integer by EAX: Use only shifts and addition instructions Pass the integer subroutine in the EBX register, and return the product in the EAX register. Assume that the product is never larger than 32 bits. You are only allowed to use a loop (do-while to shift the multiplier to the right, keeping track of the number of shifts that occur. The resulting shift count can then be applied to the SHL instruction, using the multiplicand as the destination operand. Then, the same process must be repeated until you find the last 1 bit in the multiplier. You are not allowed to use the loop instruction and are not allowed to use the factoring method. Run your program using the debugger to verify your answers. The main program that calls the subroutine and displays the product is given to you as shown below: o Submit the following Lastname asm INCLUDE Irvine 32 inc data pl BYTE "Enter the multiplicand: ",0 p2 BYTE "Enter the multiplier: ",0 p3 BYTE "The product is ",0 code main PROC call Cliscr Input the multiplicand mov edx,OFFSET p1 call WriteString call Read Dec ;ReadDec stores the first input (multiplicand) ineax mov ebx,eax. ;save eax in ebx so the next ReadDec will not overwrite it Input the multiplier mov edx, OFFSET p2 call Write String call Read Dec Read Dec stores the second input (multiplier in eax call Crlf multiply EBX by EAN, producing EAN call Multiply Display the product in EAX movedx, OFFSET p3 call Write String call Write Dec call Crif exit main ENDPExplanation / Answer
Answer:
Assembly Language Code:
push rbp
mov rbp, rsp
sub rsp, 64
mov QWORD PTR [rbp-56], rdi
mov DWORD PTR [rbp-60], esi
mov QWORD PTR [rbp-48], 0
mov QWORD PTR [rbp-40], 0
mov QWORD PTR [rbp-32], 0
mov QWORD PTR [rbp-24], 0
mov DWORD PTR [rbp-48], 1
mov DWORD PTR [rbp-4], 0
.L3:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-60]
jge .L2
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-56]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-56]
add rax, rdx
mov eax, DWORD PTR [rax]
lea edx, [rax+1]
mov eax, edx
sar eax, 31
shr eax, 31
add edx, eax
and edx, 1
sub edx, eax
mov eax, edx
mov DWORD PTR [rcx], eax
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov edx, DWORD PTR [rbp-60]
lea rcx, [rbp-48]
mov rax, QWORD PTR [rbp-56]
mov rsi, rcx
mov rdi, rax
call add(int*, int*, int)
nop
leave
ret
add(int*, int*, int):
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov DWORD PTR [rbp-36], edx
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-4], 0
.L8:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-36]
jge .L9
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov edx, DWORD PTR [rbp-4]
movsx rdx, edx
lea rcx, [0+rdx*4]
mov rdx, QWORD PTR [rbp-24]
add rdx, rcx
mov ecx, DWORD PTR [rdx]
mov edx, DWORD PTR [rbp-4]
movsx rdx, edx
lea rsi, [0+rdx*4]
mov rdx, QWORD PTR [rbp-32]
add rdx, rsi
mov edx, DWORD PTR [rdx]
add ecx, edx
mov edx, DWORD PTR [rbp-8]
add edx, ecx
mov DWORD PTR [rax], edx
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
cmp eax, 1
jle .L6
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
cdq
shr edx, 31
add eax, edx
and eax, 1
sub eax, edx
mov DWORD PTR [rcx], eax
mov DWORD PTR [rbp-8], 1
jmp .L7
.L6:
mov DWORD PTR [rbp-8], 0
.L7:
add DWORD PTR [rbp-4], 1
jmp .L8
.L9:
nop
pop rbp
ret
.LC0:
.string " ashr "
ashr(int*, int*, int&, int):
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov QWORD PTR [rbp-40], rdx
mov DWORD PTR [rbp-44], ecx
mov rax, QWORD PTR [rbp-24]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-8], eax
mov rax, QWORD PTR [rbp-32]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR [rbp-40]
mov DWORD PTR [rax], edx
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov DWORD PTR [rbp-4], 0
.L12:
mov eax, DWORD PTR [rbp-44]
sub eax, 1
cmp eax, DWORD PTR [rbp-4]
jle .L11
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rdx, rax
mov eax, DWORD PTR [rbp-4]
cdqe
add rax, 1
lea rcx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rcx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rdx], eax
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rdx, rax
mov eax, DWORD PTR [rbp-4]
cdqe
add rax, 1
lea rcx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rcx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rdx], eax
add DWORD PTR [rbp-4], 1
jmp .L12
.L11:
mov eax, DWORD PTR [rbp-44]
cdqe
sal rax, 2
lea rdx, [rax-4]
mov rax, QWORD PTR [rbp-32]
add rdx, rax
mov eax, DWORD PTR [rbp-8]
mov DWORD PTR [rdx], eax
nop
leave
ret
.LC1:
.string " "
display(int*, int*, int):
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov DWORD PTR [rbp-36], edx
mov eax, DWORD PTR [rbp-36]
sub eax, 1
mov DWORD PTR [rbp-4], eax
.L15:
cmp DWORD PTR [rbp-4], 0
js .L14
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
sub DWORD PTR [rbp-4], 1
jmp .L15
.L14:
mov esi, OFFSET FLAT:.LC1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov eax, DWORD PTR [rbp-36]
sub eax, 1
mov DWORD PTR [rbp-4], eax
.L17:
cmp DWORD PTR [rbp-4], 0
js .L18
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov eax, DWORD PTR [rax]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
sub DWORD PTR [rbp-4], 1
jmp .L17
.L18:
nop
leave
ret
.LC2:
.string " --Enter the multiplicand and multipier in signed 2's complement form if negative--"
.LC3:
.string " Number of multiplicand bit="
.LC4:
.string " multiplicand="
.LC5:
.string " No. of multiplier bit="
.LC6:
.string "Multiplier="
.LC7:
.string "qn q[n+1] BR AC QR sc "
.LC8:
.string " initial "
.LC9:
.string " "
.LC10:
.string " "
.LC11:
.string " "
.LC12:
.string " subtracting BR "
.LC13:
.string " adding BR "
.LC14:
.string " "
.LC15:
.string "Result="
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 248
mov DWORD PTR [rbp-244], edi
mov QWORD PTR [rbp-256], rsi
mov QWORD PTR [rbp-224], 0
mov QWORD PTR [rbp-216], 0
mov QWORD PTR [rbp-208], 0
mov QWORD PTR [rbp-200], 0
mov QWORD PTR [rbp-192], 0
mov esi, OFFSET FLAT:.LC2
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov esi, OFFSET FLAT:.LC3
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
lea rax, [rbp-228]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov esi, OFFSET FLAT:.LC4
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov eax, DWORD PTR [rbp-228]
sub eax, 1
mov DWORD PTR [rbp-24], eax
.L21:
cmp DWORD PTR [rbp-24], 0
js .L20
lea rax, [rbp-128]
mov edx, DWORD PTR [rbp-24]
movsx rdx, edx
sal rdx, 2
add rax, rdx
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
sub DWORD PTR [rbp-24], 1
jmp .L21
.L20:
mov eax, DWORD PTR [rbp-228]
sub eax, 1
mov DWORD PTR [rbp-24], eax
.L23:
cmp DWORD PTR [rbp-24], 0
js .L22
mov eax, DWORD PTR [rbp-24]
cdqe
mov edx, DWORD PTR [rbp-128+rax*4]
mov eax, DWORD PTR [rbp-24]
cdqe
mov DWORD PTR [rbp-80+rax*4], edx
sub DWORD PTR [rbp-24], 1
jmp .L23
.L22:
mov edx, DWORD PTR [rbp-228]
lea rax, [rbp-80]
mov esi, edx
mov rdi, rax
call complement(int*, int)
mov esi, OFFSET FLAT:.LC5
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
lea rax, [rbp-232]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov eax, DWORD PTR [rbp-232]
mov DWORD PTR [rbp-20], eax
mov esi, OFFSET FLAT:.LC6
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov eax, DWORD PTR [rbp-232]
sub eax, 1
mov DWORD PTR [rbp-24], eax
.L25:
cmp DWORD PTR [rbp-24], 0
js .L24
lea rax, [rbp-176]
mov edx, DWORD PTR [rbp-24]
movsx rdx, edx
sal rdx, 2
add rax, rdx
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
sub DWORD PTR [rbp-24], 1
jmp .L25
.L24:
mov DWORD PTR [rbp-236], 0
mov DWORD PTR [rbp-28], 0
mov esi, OFFSET FLAT:.LC7
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov esi, OFFSET FLAT:.LC8
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov edx, DWORD PTR [rbp-232]
lea rcx, [rbp-176]
lea rax, [rbp-224]
mov rsi, rcx
mov rdi, rax
call display(int*, int*, int)
mov esi, OFFSET FLAT:.LC9
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov rdx, rax
mov eax, DWORD PTR [rbp-20]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC10
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
.L35:
cmp DWORD PTR [rbp-20], 0
je .L26
mov ebx, DWORD PTR [rbp-236]
mov eax, DWORD PTR [rbp-176]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC11
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov esi, ebx
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov edx, DWORD PTR [rbp-176]
mov eax, DWORD PTR [rbp-236]
add eax, edx
cmp eax, 1
jne .L27
cmp DWORD PTR [rbp-28], 0
jne .L28
mov edx, DWORD PTR [rbp-232]
lea rcx, [rbp-80]
lea rax, [rbp-224]
mov rsi, rcx
mov rdi, rax
call add(int*, int*, int)
mov esi, OFFSET FLAT:.LC12
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov eax, DWORD PTR [rbp-232]
sub eax, 1
mov DWORD PTR [rbp-24], eax
.L30:
cmp DWORD PTR [rbp-24], 0
js .L29
mov eax, DWORD PTR [rbp-24]
cdqe
mov eax, DWORD PTR [rbp-224+rax*4]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
sub DWORD PTR [rbp-24], 1
jmp .L30
.L29:
mov DWORD PTR [rbp-28], 1
jmp .L31
.L28:
cmp DWORD PTR [rbp-28], 1
jne .L31
mov edx, DWORD PTR [rbp-232]
lea rcx, [rbp-128]
lea rax, [rbp-224]
mov rsi, rcx
mov rdi, rax
call add(int*, int*, int)
mov esi, OFFSET FLAT:.LC13
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov eax, DWORD PTR [rbp-232]
sub eax, 1
mov DWORD PTR [rbp-24], eax
.L33:
cmp DWORD PTR [rbp-24], 0
js .L32
mov eax, DWORD PTR [rbp-24]
cdqe
mov eax, DWORD PTR [rbp-224+rax*4]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
sub DWORD PTR [rbp-24], 1
jmp .L33
.L32:
mov DWORD PTR [rbp-28], 0
.L31:
mov esi, OFFSET FLAT:.LC14
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov ecx, DWORD PTR [rbp-232]
lea rdx, [rbp-236]
lea rsi, [rbp-176]
lea rax, [rbp-224]
mov rdi, rax
call ashr(int*, int*, int&, int)
jmp .L34
.L27:
mov edx, DWORD PTR [rbp-176]
mov eax, DWORD PTR [rbp-236]
cmp edx, eax
jne .L34
mov ecx, DWORD PTR [rbp-232]
lea rdx, [rbp-236]
lea rsi, [rbp-176]
lea rax, [rbp-224]
mov rdi, rax
call ashr(int*, int*, int&, int)
.L34:
mov edx, DWORD PTR [rbp-232]
lea rcx, [rbp-176]
lea rax, [rbp-224]
mov rsi, rcx
mov rdi, rax
call display(int*, int*, int)
mov esi, OFFSET FLAT:.LC11
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
sub DWORD PTR [rbp-20], 1
mov esi, OFFSET FLAT:.LC11
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov rdx, rax
mov eax, DWORD PTR [rbp-20]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC10
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
jmp .L35
.L26:
mov esi, OFFSET FLAT:.LC15
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov edx, DWORD PTR [rbp-232]
lea rcx, [rbp-176]
lea rax, [rbp-224]
mov rsi, rcx
mov rdi, rax
call display(int*, int*, int)
mov eax, 0
add rsp, 248
pop rbx
pop rbp
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L39
cmp DWORD PTR [rbp-8], 65535
jne .L39
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L39:
nop
leave
ret
_GLOBAL__sub_I__Z10complementPii:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret