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

this is assemly langauge for x86 processors. Create a procedure named CalcGrade

ID: 3830663 • Letter: T

Question

this is assemly langauge for x86 processors.

Create a procedure named CalcGrade that receives an integer value
between 0 and 100, and returns a single capital letter in the AL
register. Preserve all other register values between calls to the
procedure. The letter returned by the procedure should be according
to the following ranges:

Score Range Letter Grade
-------------------------
90 to 100 A
80 to 89 B
70 to 79 C
60 to 69 D
0 to 59 F

Write a test program that generates 10 random integers between 50 and 100,
inclusive. Each time an integer is generated, pass it to the CalcGrade
procedure. You can test your program using a debugger, or if you prefer
to use the book's library, you can display each integer and its corresponding letter grade.
letter grade.

Explanation / Answer

.LC0:
.string "A"
.LC1:
.string "B"
.LC2:
.string "C"
.LC3:
.string "D"
.LC4:
.string "F"
CalcGrade[abi:cxx11](int):
push rbp
mov rbp, rsp
push rbx
sub rsp, 40
mov QWORD PTR [rbp-40], rdi
mov DWORD PTR [rbp-44], esi
cmp DWORD PTR [rbp-44], 90
jle .L2
lea rax, [rbp-21]
mov rdi, rax
call std::allocator<char>::allocator()
lea rdx, [rbp-21]
mov rax, QWORD PTR [rbp-40]
mov esi, OFFSET FLAT:.LC0
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
lea rax, [rbp-21]
mov rdi, rax
call std::allocator<char>::~allocator()
jmp .L1
.L2:
cmp DWORD PTR [rbp-44], 80
jle .L4
lea rax, [rbp-20]
mov rdi, rax
call std::allocator<char>::allocator()
lea rdx, [rbp-20]
mov rax, QWORD PTR [rbp-40]
mov esi, OFFSET FLAT:.LC1
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
lea rax, [rbp-20]
mov rdi, rax
call std::allocator<char>::~allocator()
jmp .L1
.L4:
cmp DWORD PTR [rbp-44], 70
jle .L5
lea rax, [rbp-19]
mov rdi, rax
call std::allocator<char>::allocator()
lea rdx, [rbp-19]
mov rax, QWORD PTR [rbp-40]
mov esi, OFFSET FLAT:.LC2
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
lea rax, [rbp-19]
mov rdi, rax
call std::allocator<char>::~allocator()
jmp .L1
.L5:
cmp DWORD PTR [rbp-44], 60
jle .L6
lea rax, [rbp-18]
mov rdi, rax
call std::allocator<char>::allocator()
lea rdx, [rbp-18]
mov rax, QWORD PTR [rbp-40]
mov esi, OFFSET FLAT:.LC3
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
lea rax, [rbp-18]
mov rdi, rax
call std::allocator<char>::~allocator()
jmp .L1
.L6:
lea rax, [rbp-17]
mov rdi, rax
call std::allocator<char>::allocator()
lea rdx, [rbp-17]
mov rax, QWORD PTR [rbp-40]
mov esi, OFFSET FLAT:.LC4
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
lea rax, [rbp-17]
mov rdi, rax
call std::allocator<char>::~allocator()
jmp .L1
mov rbx, rax
lea rax, [rbp-21]
mov rdi, rax
call std::allocator<char>::~allocator()
mov rax, rbx
mov rdi, rax
call _Unwind_Resume
mov rbx, rax
lea rax, [rbp-20]
mov rdi, rax
call std::allocator<char>::~allocator()
mov rax, rbx
mov rdi, rax
call _Unwind_Resume
mov rbx, rax
lea rax, [rbp-19]
mov rdi, rax
call std::allocator<char>::~allocator()
mov rax, rbx
mov rdi, rax
call _Unwind_Resume
mov rbx, rax
lea rax, [rbp-18]
mov rdi, rax
call std::allocator<char>::~allocator()
mov rax, rbx
mov rdi, rax
call _Unwind_Resume
mov rbx, rax
lea rax, [rbp-17]
mov rdi, rax
call std::allocator<char>::~allocator()
mov rax, rbx
mov rdi, rax
call _Unwind_Resume
.L1:
mov rax, QWORD PTR [rbp-40]
add rsp, 40
pop rbx
pop rbp
ret
.LC5:
.string "Score: "
.LC6:
.string " : "
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 56
mov edi, 0
call time
mov edi, eax
call srand
mov DWORD PTR [rbp-20], 0
.L19:
cmp DWORD PTR [rbp-20], 9
jg .L18
call rand
mov ecx, eax
mov edx, -1600085855
mov eax, ecx
imul edx
lea eax, [rdx+rcx]
sar eax, 5
mov edx, eax
mov eax, ecx
sar eax, 31
sub edx, eax
mov eax, edx
imul eax, eax, 51
sub ecx, eax
mov eax, ecx
add eax, 50
mov DWORD PTR [rbp-24], eax
lea rax, [rbp-64]
mov edx, DWORD PTR [rbp-24]
mov esi, edx
mov rdi, rax
call CalcGrade[abi:cxx11](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*)
mov rdx, rax
mov eax, DWORD PTR [rbp-24]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC6
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 rdx, rax
lea rax, [rbp-64]
mov rsi, rax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
lea rax, [rbp-64]
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
add DWORD PTR [rbp-20], 1
jmp .L19
.L18:
mov eax, 0
jmp .L23
mov rbx, rax
lea rax, [rbp-64]
mov rdi, rax
call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
mov rax, rbx
mov rdi, rax
call _Unwind_Resume
.L23:
add rsp, 56
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 .L26
cmp DWORD PTR [rbp-8], 65535
jne .L26
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
.L26:
nop
leave
ret
_GLOBAL__sub_I__Z9CalcGradeB5cxx11i:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret