can someone help me to convert the C++ coding to assembly languagae that is .asm file

#include <iostream>

int main()
{
long dec;
int rem;

std::cout << "enter decimal number: ";
std::cin >> dec;

while (dec > 0) {
rem = dec % 16;
if (rem > 9) {
switch (rem) {
case 10: std::cout << "A"; break;
case 11: std::cout << "B"; break;
case 12: std::cout << "C"; break;
case 13: std::cout << "D"; break;
case 14: std::cout << "E"; break;
case 15: std::cout << "F"; break;
}
}
else {
std::cout << rem;
}
dec = dec / 16;
}
}
Compilers usually have flags that you can use to see the assembly that they output, but otherwise is Compiler Explorer a great tool for this. https://godbolt.org/g/xYvey3
Last edited on
write the c++ from an assembler's perspective, and make your program as small as possible with only the desired functionality in the program, then compile out your assembly listing.
then take the assembly code and clean it up and hand-tune it.

for example, you may want to isolate cout << text into a program (like hello world) and see how to do just that one piece in assembly. Maybe write a little assembly function that prints to screen from some memory location via pointer /loop.

then do it again with cin and see how to read in in assembly.

build it up slowly and cleanly this way, don't try to take a listing for the whole program as it will be overly confusing and hard to break into clean code.

...
you appear to want hex output.
before you convert to assembly, maybe use a simpler approach? It is way overly complicated. You have a number in hand. Its in binary format already, and you can get the individual bytes. I mean, in assembler for Intel, you can literally move the value to a register (eax, for example for a 32 bit int?) and the pick of al and ah etc bytes which are pairs of hex 'digits'.... and you can do that in C too with pointers (get an unsigned char * to the address of the input variable and peel off the bytes).

something like this in C?
char tbl[] = "0123456789ABCDEF";
int hx;
cin >> hx;
char * cp = (char*)(&hx);
cout << tbl[hx&0xF] << tbl[ (hx&0xF0) >> 4 ] << endl; //example of part of the number.
watch for endian-ness and adjust your output and that becomes a rather simple thing to convert to assembly.

I hope I did that right off the cuff. At least the rough idea is there. I am out of practice with bit juggling, but the point is, simplify it first.

notice that I avoided a string and vector class here on purpose. The generated assembly for those would be much more convoluted than just using simple C constructs, and harder to clean up and use. If you want to use OOP, keep it in c++. Assembly routines should do something that needs doing a lot of times (tight loops, usually) very fast and the c++ compiler is not capable, or something that you readily can't do in c++ (like the 1 cpu instruction endian fix or reading the north bridge timer or something).


Last edited on
Topic archived. No new replies allowed.