In: Computer Science
Convert this C++ program exactly as you see it into x86 assembly language:
// Use the Irvine library for the print function
#include <iostream>
// The string that needs to be printed
char word[] = "Golf\0";
// Pointer to a specific character in the string
char * character = word;
//NOTE: This main() function is not portable outside of Visual Studio
void main()
{
// Set up a LOOP - See the while loop's conditional expression below
int ecx = 4;
do
{
// Print the character
// In x86 assembly language you must use the following two lines of code:
// mov al, WHATEVER_CHARACTER_YOU_WANT_TO_PRINT
// call WriteChar
std::cout << *character;
// Increment the pointer
++character;
} while (--ecx != 0);
// In x86 assembly language you must use the following line of code:
// call CrLf
std::cout << std::endl;
// In x86 assembly language you must use the following line of code:
// call WaitMsg system("PAUSE");
}
word:
.string "Golf"
.string ""
character:
.quad word
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR[rbp-4], 4
.L3:
mov rax, QWORD PTR character[rip]
movzx eax, BYTE PTR [rax]
movsx eax, al
mov esi, eax
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)
mov rax, QWORD PTR character[rip]
add rax, 1
mov QWORD PTR character[rip], rax
sub DWORD PTR [rbp-4], 1
cmp DWORD PTR [rbp-4], 0
setne al
test al, al
je .L2
jmp .L3
.L2:
mov esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_TO_ES6_
mov edi, OFFSET FLAT:_ZSt4cout
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> >&))
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 .L7
cmp DWORD PTR [rbp-8], 65535
jne .L7
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
call __cxa_atexit
.L7:
nop
leave
ret
_GLOBAL__sub_I_word:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret