Question

I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

I need my c++ code converted to MASM (assembly language).

The instructions below:

write an assembly program that does the following;

1. count and display the number of words in the user input string.

2. Flip the case of each character from upper to lower or lower to upper.

For example if the user types in:   "Hello thEre. How aRe yOu?"

Your output should be:

The number of words in the input string is: 5

The output string is : hELLO THeRE. hOW ArE YoU?

//my C++ code

#include<iostream>
#include<sstream>
#include<string>

using namespace std;

// function that takes a word and flip the letters from lowercase to uppercase and vice versa
string flipWord(const string word){
  
   stringstream flipped;
  
   for(int index=0;index<word.length();index++){
       char letter = word.at(index);
       if('a'<=letter && letter<='z'){
           flipped<<char(letter - ('a'-'A'));
       }else if('A'<=letter && letter<='Z'){
           flipped<<char(letter + ('a'-'A'));
       }else{
           flipped<<letter;
       }
   }
   string flippedString;
   flipped>>flippedString;
   return flippedString;
}

int main(){
  
   int wordCount=0;
   cout<<"Enter a sentence: ";
  
   string sentence;
   string word;
   stringstream flippedSentence;
   string flippedString;
  
   getline(cin,sentence); // reads the whole line
   stringstream ss(sentence); // pushes the line to a string stream
  
   // for each word in string stream
   // invoked the flipWord and pushes the word into another string stream
   // at the same time count the words
   while(ss>>word){
       flippedSentence<<flipWord(word)<<' ';
       wordCount++;
   }
   cout<<"The number of words in the input string is: "<<wordCount<<endl;
   // prints the string stream one word at a time
   while(flippedSentence>>word){
       cout<<word<<" ";
   }
  
  
}

//please convert to asm

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assembly language :- (x86-64 gcc 5.1 compiler)

Program :-

std::operator|(std::_Ios_Openmode, std::_Ios_Openmode):

push rbp

mov rbp, rsp

mov DWORD PTR [rbp-4], edi

mov DWORD PTR [rbp-8], esi

mov edx, DWORD PTR [rbp-4]

mov eax, DWORD PTR [rbp-8]

or eax, edx

pop rbp

ret

flipWord(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):

push rbp

mov rbp, rsp

push rbx

sub rsp, 424

mov QWORD PTR [rbp-424], rdi

mov QWORD PTR [rbp-432], rsi

mov esi, 8

mov edi, 16

call std::operator|(std::_Ios_Openmode, std::_Ios_Openmode)

mov edx, eax

lea rax, [rbp-416]

mov esi, edx

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)

mov DWORD PTR [rbp-20], 0

.L8:

mov eax, DWORD PTR [rbp-20]

movsx rbx, eax

mov rax, QWORD PTR [rbp-432]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::length() const

cmp rbx, rax

setb al

test al, al

je .L4

mov eax, DWORD PTR [rbp-20]

movsx rdx, eax

mov rax, QWORD PTR [rbp-432]

mov rsi, rdx

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::at(unsigned long) const

movzx eax, BYTE PTR [rax]

mov BYTE PTR [rbp-21], al

cmp BYTE PTR [rbp-21], 96

jle .L5

cmp BYTE PTR [rbp-21], 122

jg .L5

movzx eax, BYTE PTR [rbp-21]

sub eax, 32

movsx eax, al

lea rdx, [rbp-416]

add rdx, 16

mov esi, eax

mov rdi, rdx

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

jmp .L6

.L5:

cmp BYTE PTR [rbp-21], 64

jle .L7

cmp BYTE PTR [rbp-21], 90

jg .L7

movzx eax, BYTE PTR [rbp-21]

add eax, 32

movsx eax, al

lea rdx, [rbp-416]

add rdx, 16

mov esi, eax

mov rdi, rdx

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

jmp .L6

.L7:

movsx eax, BYTE PTR [rbp-21]

lea rdx, [rbp-416]

add rdx, 16

mov esi, eax

mov rdi, rdx

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

.L6:

add DWORD PTR [rbp-20], 1

jmp .L8

.L4:

mov rax, QWORD PTR [rbp-424]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string() [complete object constructor]

mov rdx, QWORD PTR [rbp-424]

lea rax, [rbp-416]

mov rsi, rdx

mov rdi, rax

call std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)

nop

lea rax, [rbp-416]

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() [complete object destructor]

jmp .L14

mov rbx, rax

mov rax, QWORD PTR [rbp-424]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

jmp .L11

mov rbx, rax

.L11:

lea rax, [rbp-416]

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() [complete object destructor]

mov rax, rbx

mov rdi, rax

call _Unwind_Resume

.L14:

mov rax, QWORD PTR [rbp-424]

add rsp, 424

pop rbx

pop rbp

ret

.LC0:

.string "Enter a sentence: "

.LC1:

.string "The number of words in the input string is: "

.LC2:

.string " "

main:

push rbp

mov rbp, rsp

push rbx

sub rsp, 984

mov DWORD PTR [rbp-20], 0

mov esi, OFFSET FLAT:.LC0

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

lea rax, [rbp-128]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string() [complete object constructor]

lea rax, [rbp-160]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string() [complete object constructor]

mov esi, 8

mov edi, 16

call std::operator|(std::_Ios_Openmode, std::_Ios_Openmode)

mov edx, eax

lea rax, [rbp-560]

mov esi, edx

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)

lea rax, [rbp-592]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string() [complete object constructor]

lea rax, [rbp-128]

mov rsi, rax

mov edi, OFFSET FLAT:_ZSt3cin

call std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)

mov esi, 8

mov edi, 16

call std::operator|(std::_Ios_Openmode, std::_Ios_Openmode)

mov edx, eax

lea rcx, [rbp-128]

lea rax, [rbp-992]

mov rsi, rcx

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)

.L17:

lea rdx, [rbp-160]

lea rax, [rbp-992]

mov rsi, rdx

mov rdi, rax

call std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)

mov rdx, QWORD PTR [rax]

sub rdx, 24

mov rdx, QWORD PTR [rdx]

add rax, rdx

mov rdi, rax

call std::basic_ios<char, std::char_traits<char> >::operator void*() const

test rax, rax

setne al

test al, al

je .L16

lea rdx, [rbp-160]

lea rax, [rbp-64]

mov rsi, rdx

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

lea rax, [rbp-96]

lea rdx, [rbp-64]

mov rsi, rdx

mov rdi, rax

call flipWord(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

lea rax, [rbp-96]

lea rdx, [rbp-560]

add rdx, 16

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, 32

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)

lea rax, [rbp-96]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

lea rax, [rbp-64]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

add DWORD PTR [rbp-20], 1

jmp .L17

.L16:

mov esi, OFFSET FLAT:.LC1

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 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:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

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

.L19:

lea rdx, [rbp-160]

lea rax, [rbp-560]

mov rsi, rdx

mov rdi, rax

call std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)

mov rdx, QWORD PTR [rax]

sub rdx, 24

mov rdx, QWORD PTR [rdx]

add rax, rdx

mov rdi, rax

call std::basic_ios<char, std::char_traits<char> >::operator void*() const

test rax, rax

setne al

test al, al

je .L18

lea rax, [rbp-160]

mov rsi, rax

mov edi, OFFSET FLAT:_ZSt4cout

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:.LC2

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

.L18:

lea rax, [rbp-992]

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() [complete object destructor]

lea rax, [rbp-592]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

lea rax, [rbp-560]

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() [complete object destructor]

lea rax, [rbp-160]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

lea rax, [rbp-128]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

mov eax, 0

jmp .L35

mov rbx, rax

lea rax, [rbp-96]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

jmp .L22

mov rbx, rax

.L22:

lea rax, [rbp-64]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

jmp .L23

mov rbx, rax

.L23:

lea rax, [rbp-992]

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() [complete object destructor]

jmp .L24

mov rbx, rax

.L24:

lea rax, [rbp-592]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

jmp .L25

mov rbx, rax

.L25:

lea rax, [rbp-560]

mov rdi, rax

call std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() [complete object destructor]

jmp .L26

mov rbx, rax

.L26:

lea rax, [rbp-160]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

jmp .L27

mov rbx, rax

.L27:

lea rax, [rbp-128]

mov rdi, rax

call std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() [complete object destructor]

mov rax, rbx

mov rdi, rax

call _Unwind_Resume

.L35:

add rsp, 984

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

cmp DWORD PTR [rbp-8], 65535

jne .L38

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

.L38:

nop

leave

ret

_GLOBAL__sub_I_flipWord(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):

push rbp

mov rbp, rsp

mov esi, 65535

mov edi, 1

call __static_initialization_and_destruction_0(int, int)

pop rbp

ret

Add a comment
Know the answer?
Add Answer to:
I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need the following done in MASM (Assmebly or ASM) This program must use Irvine Library and the outline for the code is...

    I need the following done in MASM (Assmebly or ASM) This program must use Irvine Library and the outline for the code is below (Kip irvine) Write an assembly language program to input a string from the user. Your program should do these two things: 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   ...

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output:...

    Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output: A 1000Pairs.txt looks like this: {100, A } {200, A } {300, B } {400, C } {500, D } {600, E } {700, F } {800, G } {900, H } {1000, I } {1100, J } {1200, K } {1300, L } {1400, M } {1500, N } {1600, O } {1700, P } {1800, Q } {1900, R } {2000, S...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • Hello! i need a program in C that uses FILE*! The instructions are below! Please dont...

    Hello! i need a program in C that uses FILE*! The instructions are below! Please dont copy paste another chegg post! Thank you! Will leave positive review! I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT