Write an assembly program using Pep/9 simulator to print your first name in one
ID: 3717157 • Letter: W
Question
Write an assembly program using Pep/9 simulator to print your first name in one line and last name in second line.
Write an assembly program using Pep/9 simulator to receive two integers and calculate the average and display AVG([FirstNumber], [SecondNumber]) = [Average] e.g. if inputs are 5 and 8 the output should be: AVG(5, 8) = 6 Note: Ignore the decimal values.
Write an assembly program using Pep/9 simulator that simulate the C++ program below: int width; int length; int perim; int main(){ cin>>width>>length; perim = (width + length) * 2; cout<<“width = “ << width; cout<<”length = “ << length; cout<<”perim = “ << perim; return 0; }
Explanation / Answer
print first and last name :
.LC0: //labels
.string "Input your firstname: "
.LC1:
.string "%s"
.LC2:
.string "Input your lastname: "
main:
push rbp // push a value to the stack
mov rbp, rsp // move value from source to destination ,rbp --destination and rsp is source .Below mov commands are same
sub rsp, 64 //SUB operation sets or clears the overflow and carry flags.
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-32] //LEA (load effective address)
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call __isoc99_scanf //call sub routine
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
lea rax, [rbp-64]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call __isoc99_scanf
lea rax, [rbp-32]
mov rdi, rax
call puts
lea rax, [rbp-64]
mov rdi, rax
call puts
mov eax, 0
leave //leave is counter part to enter
ret // return
------------------------
receive two integers and calculate the average and display AVG([FirstNumber], [SecondNumber]) = [Average]
.LC0:
.string "Enter first number :"
.LC1:
.string "%d"
.LC2:
.string "Enter second number :"
.LC4:
.string " Average of %d and %d is = %f"
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-8]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call __isoc99_scanf
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
lea rax, [rbp-12]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call __isoc99_scanf
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-12]
add eax, edx
cvtsi2ss xmm0, eax
movss xmm1, DWORD PTR .LC3[rip] //Moves a scalar single-precision floating-point value from the source operand (second operand) to the destination operand (first operand).
divss xmm0, xmm1 //Divides the low single-precision floating-point value in the first source operand by the low single-precision floating-point value in the second source operand, and stores the single-precision floating-point result in the destination operand.
movss DWORD PTR [rbp-4], xmm0
cvtss2sd xmm0, DWORD PTR [rbp-4] //Converts a single-precision floating-point value in the “convert-from” source operand to a double-precision floating-point value in the destination operand
mov edx, DWORD PTR [rbp-12]
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov edi, OFFSET FLAT:.LC4
mov eax, 1
call printf
mov eax, 0
leave
ret
.LC3:
.long 1073741824
-------------------------------------------
assembly language for given cpp program
.LC0:
.string "width ="
.LC1:
.string "length ="
.LC2:
.string "perim ="
main:
push rbp
mov rbp, rsp
sub rsp, 16
lea rax, [rbp-8]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
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 edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-12]
add eax, edx
add eax, eax
mov DWORD PTR [rbp-4], eax
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 rdx, rax
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<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*)
mov rdx, rax
mov eax, DWORD PTR [rbp-12]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
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 rdx, rax
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov rdi, rdx
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 .L5
cmp DWORD PTR [rbp-8], 65535 //compare the 2 values
jne .L5
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
.L5:
nop // no operation
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.