Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 2 Write a program in assembly language that asks the user to input tw

ID: 3599888 • Letter: A

Question

Assignment 2 Write a program in assembly language that asks the user to input two integers in the range 1 to 1,000 and calculates their product. You can only use shift, add or sub instructions to calculate the multiplication (not the mult instruction). The program should verify that both integers are within the specified range. If either on the integers are outside this range, the program should display a message "input range error" and ask the user to input again. If user persist in entering the wrong integers more than two times, the program should exit and display message "erroit exceeded." Else it should exit after calculating the product of the two integers that are within the specified range.

Explanation / Answer

.LC0:

.string "Enter The First Number range 1 to 1000: "

.LC1:

.string "Enter The Second Number range 1 to 1000 : "

.LC2:

.string "input range error"

.LC3:

.string "error limit exceed"

main:

push rbp

mov rbp, rsp

sub rsp, 16

mov DWORD PTR [rbp-4], 0

mov DWORD PTR [rbp-8], 0

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*)

lea rax, [rbp-12]

mov rsi, rax

mov edi, OFFSET FLAT:std::cin

call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)

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*)

lea rax, [rbp-16]

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-12]

test eax, eax

jle .L2

mov eax, DWORD PTR [rbp-12]

cmp eax, 999

jg .L2

mov eax, DWORD PTR [rbp-16]

test eax, eax

jle .L2

mov eax, DWORD PTR [rbp-16]

cmp eax, 1000

jle .L3

.L2:

add DWORD PTR [rbp-8], 1

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*)

.L3:

cmp DWORD PTR [rbp-8], 1

jle .L7

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*)

.L7:

mov eax, DWORD PTR [rbp-12]

test eax, eax

jle .L5

mov eax, DWORD PTR [rbp-12]

and eax, 1

test eax, eax

je .L6

mov eax, DWORD PTR [rbp-16]

add DWORD PTR [rbp-4], eax

.L6:

mov eax, DWORD PTR [rbp-16]

add eax, eax

mov DWORD PTR [rbp-16], eax

mov eax, DWORD PTR [rbp-12]

mov edx, eax

shr edx, 31

add eax, edx

sar eax

mov DWORD PTR [rbp-12], eax

jmp .L7

.L5:

mov eax, DWORD PTR [rbp-4]

mov esi, eax

mov edi, OFFSET FLAT:std::cout

call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

mov eax, 0

leave

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

cmp DWORD PTR [rbp-8], 65535

jne .L11

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

.L11:

nop

leave

ret

_GLOBAL__sub_I_main:

push rbp

mov rbp, rsp

mov esi, 65535

mov edi, 1

call __static_initialization_and_destruction_0(int, int)

pop rbp

ret