CS 702 - Operating Systems - Spring 2005
Assembly Code


Loyola College > Department of Computer Science > Dr. James Glenn > CS 702 > Examples and Lecture Notes > Assembly Code

assembly.cpp

#include <iostream>

/**
 * An example to compile into assembly code so we can see how registers,
 * flags, and the stack are used.
 */

int foo(int a, int b);

int zero = 0;
int one = 1;

int main(int argc, char **argv)
{
  int x, y;

  std::cout << "Enter two integers" << std::endl;
  std::cin >> x >> y;

  std::cout << foo(x, y) << std::endl;
}

int foo(int a, int b)
{
  int temp;

  temp = a * b + a;
  
  if (temp > a)
    return zero;
  else
    return one;
}



Compiled Code

(gdb) disassemble
Dump of assembler code for function _Z3fooii:
0x08048732 <_Z3fooii+0>:        push   %ebp
0x08048733 <_Z3fooii+1>:        mov    %esp,%ebp
0x08048735 <_Z3fooii+3>:        sub    $0x8,%esp
0x08048738 <_Z3fooii+6>:        mov    0x8(%ebp),%eax
0x0804873b <_Z3fooii+9>:        imul   0xc(%ebp),%eax
0x0804873f <_Z3fooii+13>:       add    0x8(%ebp),%eax
0x08048742 <_Z3fooii+16>:       mov    %eax,0xfffffffc(%ebp)
0x08048745 <_Z3fooii+19>:       mov    0xfffffffc(%ebp),%eax
0x08048748 <_Z3fooii+22>:       cmp    0x8(%ebp),%eax
0x0804874b <_Z3fooii+25>:       jle    0x8048757 <_Z3fooii+37>
0x0804874d <_Z3fooii+27>:       mov    0x80499b8,%eax
0x08048752 <_Z3fooii+32>:       mov    %eax,0xfffffff8(%ebp)
0x08048755 <_Z3fooii+35>:       jmp    0x804875f <_Z3fooii+45>
0x08048757 <_Z3fooii+37>:       mov    0x80499bc,%eax
0x0804875c <_Z3fooii+42>:       mov    %eax,0xfffffff8(%ebp)
0x0804875f <_Z3fooii+45>:       mov    0xfffffff8(%ebp),%eax
0x08048762 <_Z3fooii+48>:       leave
0x08048763 <_Z3fooii+49>:       ret
End of assembler dump.
This code can also be downloaded from the file assembly.cpp.