c++ to assembly language

themadhatterxxx

Junior Member
May 12, 2008
14
0
0
Can someone please give me the equivalent in assembly language?

for (int index = 0; index < 5; ++index)
{
if (index > 1 && index < 4)
{
cout << "hello" << endl;
}
else if (index <= 1)
{
cout << "bye" << endl;
}
cout << "done" << endl;
}

Thank you
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Haha, the equivalent to those three calls to cout operator << in assembler isn't something to sneeze at. The loops... no big deal. But writing to stdout involves either making some assumptions about the location of the text buffer or firing off some interrupts.

In other words, I'm with George. And if you take degibson's (presumably tongue in cheek) advice you'll have some reading to do, even with that little bit of code, because all you'll see at the calls to cout is some register setup and call syntax. Trace into cout operator << and you'll really be in the thick of it.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Even if you do find someone to do your homework for you, that just means you'll fail the midterm and final.

Also, understanding what is happening in assembly code is part of your education as a software developer. You'll probably never write real applications in it unless possibly if you work on device drivers or code for embedded systems but you'll better understand pointers, stacks, buffers, mallocs/news, and function calls.
 

slpaulson

Diamond Member
Jun 5, 2000
4,414
14
81
If you don't even know enough to specify what architecture you want the code for, you've got some studying to do.
 

hans007

Lifer
Feb 1, 2000
20,212
18
81
depending on what he has to do to say "cout" , it could just be

something like push <address of string>
call <some address of cout>

and the rest is just some counter with a jge and and incrementer on some register for the counter. probably some cmp statements for the if and else parts and more jumps.

then again , to the OP, if you cant figure this out, you might as well change your major now. as you will be horribly overmatched in any real job that involves ASM or C++
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Markbnj
Haha, the equivalent to those three calls to cout operator << in assembler isn't something to sneeze at. The loops... no big deal. But writing to stdout involves either making some assumptions about the location of the text buffer or firing off some interrupts.

In other words, I'm with George. And if you take degibson's (presumably tongue in cheek) advice you'll have some reading to do, even with that little bit of code, because all you'll see at the calls to cout is some register setup and call syntax. Trace into cout operator << and you'll really be in the thick of it.

Ahh... don't forget the magic of libraries! I'm sure the OP's HW wants him to throw some int 21h's around... but gcc won't do that ugliness. Check it out:

My foo.C:
1 #include <iostream>
2
3 using namespace std;
4
5 void foo() {
6 cout << "Bar";
7 }

g++ -c -S foo.C -o foo.S (I trimmed off the junk that g++ spits out, and the .asciiz definition of "Bar")

73 .type _Z3foov, @function
74 _Z3foov:
75 .LFB1401:
76 pushl %ebp
77 .LCFI9:
78 movl %esp, %ebp
79 .LCFI10:
80 subl $8, %esp
81 .LCFI11:
82 movl $.LC0, 4(%esp)
83 movl $_ZSt4cout, (%esp)
84 call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
85 leave
86 ret

Ugly, perhaps, but not horrible. :) And no int 21h, sadly.

Edited to add Markbnj's quote.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Markbnj
C++ makes for pretty assembler, doesn't it? Ugh.

Actually it looks a lot better if you declare everything as extern "C", but that of course has its own issues. :)
 

mcmilljb

Platinum Member
May 17, 2005
2,144
2
81
Originally posted by: Markbnj
C++ makes for pretty assembler, doesn't it? Ugh.

Sure does. When I took system programming back in college, we had to look at some of the crap. Too bad we didn't offer a x86 assembly class so I could understand it all :frown: