help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Function Declaration in g++


From: derrick_chi
Subject: Function Declaration in g++
Date: Fri, 31 Aug 2007 08:49:12 -0700
User-agent: G2/1.0

  Hello guys

     I am new to this forum and to gcc/g++ and I need to know exactly
how to declare a function in g++.  I wrote a C++ program to calculate
the GCD of two numbers, I've tested it and it works.  I wanted to
compile the program and target a mips processor so I issued the
following command
"sde-c++ -c -mtune=r3k -T ph.ld -O2 GCD.cpp -o GCD.bin"  The program
compiled fine, but would link because the linker for some reason did
not reconize the iostream header file, no idea why?

So I used the -c option and like I said it compiled fine.  However
when I look over the disassembled list the assembly code is not quite
correct, it looks as if the compiler placed the function "GCD" before
the main() and before any initialization period.  I read over the
first few instructions and sure enough I was right because the
function is placed before any initialization the program starts off
within the function and can't get out its in an infinite loop and
can't get out.  I figured it must have something to do with how I am
declaring the function and I wanted to ask you guys.  Some one help me
please.  The C++ code is pasted in below as well as the disassemble
list.


#include "iostream"
#include "iomanip"
#include "math.h"


using namespace std;

unsigned int GCD_FUNCT( unsigned int,  unsigned int );


int main() {

   unsigned int ARRAY[32], GCD[16];
   unsigned int Loop_Control_V, Reg1, Reg2, new_value;


  for ( unsigned int i = 0; i < 31; ++ i ) {


        new_value = i + ( 3 * (4) );

          if ( new_value % 2 != 0 ) {

               new_value = new_value * 4;
               }

          else{
                 new_value = new_value * 10;
                 }

         ARRAY[i] = new_value;
         }

     Loop_Control_V = 0;

          while ( Loop_Control_V < 30 ) {

                if ( ARRAY[Loop_Control_V] > ARRAY[Loop_Control_V +
1] ) {


                     Reg1 = ARRAY[Loop_Control_V];
                     Reg2 = ARRAY[Loop_Control_V + 1];
                     }

                else {
                     Reg1 = ARRAY[Loop_Control_V + 1];
                     Reg2 = ARRAY[Loop_Control_V];
                     }

          GCD[Loop_Control_V] = GCD_FUNCT(Reg1,Reg2);
          Loop_Control_V = Loop_Control_V + 1;

          }

  return 0;

}

 unsigned int GCD_FUNCT( unsigned int REGa,  unsigned int REGb) {

 unsigned int temp1, temp2, GCD;

     temp1 = REGa;
     temp2 = REGb;

     while ( temp1 != temp2 ) {

           if ( temp1 > temp2 ) {

           temp1 = temp1 - temp2;
           }
             else {
                      temp2 = temp2 - temp1;
                      }
             }
     GCD = temp1;
  return GCD;
 }


-- Disassemble list.

GCD.bin:     file format elf32-tradbigmips

Disassembly of section .text:

00000000 <_Z9GCD_FUNCTjj>:
_Z9GCD_FUNCTjj():
   0:   10850007        beq     a0,a1,20 <_Z9GCD_FUNCTjj+0x20>
   4:   00000000        nop
   8:   00a4102b        sltu    v0,a1,a0
   c:   10400006        beqz    v0,28 <_Z9GCD_FUNCTjj+0x28>
  10:   00000000        nop
  14:   00852023        subu    a0,a0,a1
  18:   1485fffb        bne     a0,a1,8 <_Z9GCD_FUNCTjj+0x8>
  1c:   00000000        nop
  20:   03e00008        jr      ra
  24:   00801021        move    v0,a0
  28:   1000fff5        b       0 <_Z9GCD_FUNCTjj>
  2c:   00a42823        subu    a1,a1,a0

00000030 <main>:
main():
  30:   27bdff20        addiu   sp,sp,-224
  34:   27a80010        addiu   t0,sp,16
  38:   afbf00d8        sw      ra,216(sp)
  3c:   afb100d4        sw      s1,212(sp)
  40:   afb000d0        sw      s0,208(sp)
  44:   00002821        move    a1,zero
  48:   01003021        move    a2,t0
  4c:   24a2000c        addiu   v0,a1,12
  50:   00021880        sll     v1,v0,0x2
  54:   24a50001        addiu   a1,a1,1
  58:   30440001        andi    a0,v0,0x1
  5c:   00621821        addu    v1,v1,v0
  60:   2ca7001f        sltiu   a3,a1,31
  64:   14800002        bnez    a0,70 <main+0x40>
  68:   00021080        sll     v0,v0,0x2
  6c:   00031040        sll     v0,v1,0x1
  70:   acc20000        sw      v0,0(a2)
  74:   14e0fff5        bnez    a3,4c <main+0x1c>
  78:   24c60004        addiu   a2,a2,4
  7c:   01008021        move    s0,t0
  80:   2411001d        li      s1,29
  84:   8e060000        lw      a2,0(s0)
  88:   8e030004        lw      v1,4(s0)
  8c:   2631ffff        addiu   s1,s1,-1
  90:   0066382b        sltu    a3,v1,a2
  94:   00c02021        move    a0,a2
  98:   14e00003        bnez    a3,a8 <main+0x78>
  9c:   00602821        move    a1,v1
  a0:   00602021        move    a0,v1
  a4:   00c02821        move    a1,a2
  a8:   0c000000        jal     0 <_Z9GCD_FUNCTjj>
  ac:   00000000        nop
  b0:   ae020080        sw      v0,128(s0)
  b4:   0621fff3        bgez    s1,84 <main+0x54>
  b8:   26100004        addiu   s0,s0,4
  bc:   8fbf00d8        lw      ra,216(sp)
  c0:   8fb100d4        lw      s1,212(sp)
  c4:   8fb000d0        lw      s0,208(sp)
  c8:   00001021        move    v0,zero
  cc:   03e00008        jr      ra
  d0:   27bd00e0        addiu   sp,sp,224

000000d4 <_Z41__static_initialization_and_destruction_0ii>:
_Z41__static_initialization_and_destruction_0ii():
  d4:   27bdffe0        addiu   sp,sp,-32
  d8:   38860001        xori    a2,a0,0x1
  dc:   38a5ffff        xori    a1,a1,0xffff
  e0:   afb00010        sw      s0,16(sp)
  e4:   2cc30001        sltiu   v1,a2,1
  e8:   2cb00001        sltiu   s0,a1,1
  ec:   afb10014        sw      s1,20(sp)
  f0:   02031024        and     v0,s0,v1
  f4:   00808821        move    s1,a0
  f8:   3c040000        lui     a0,0x0
  fc:   afbf0018        sw      ra,24(sp)
 100:   14400010        bnez    v0,144
<_Z41__static_initialization_and_destruction_0ii+0x70>
 104:   24840000        addiu   a0,a0,0
 108:   2e280001        sltiu   t0,s1,1
 10c:   3c090000        lui     t1,0x0
 110:   02083824        and     a3,s0,t0
 114:   14e00006        bnez    a3,130
<_Z41__static_initialization_and_destruction_0ii+0x5c>
 118:   25240000        addiu   a0,t1,0
 11c:   8fbf0018        lw      ra,24(sp)
 120:   8fb10014        lw      s1,20(sp)
 124:   8fb00010        lw      s0,16(sp)
 128:   03e00008        jr      ra
 12c:   27bd0020        addiu   sp,sp,32
 130:   8fbf0018        lw      ra,24(sp)
 134:   8fb10014        lw      s1,20(sp)
 138:   8fb00010        lw      s0,16(sp)
 13c:   08000000        j       0 <_Z9GCD_FUNCTjj>
 140:   27bd0020        addiu   sp,sp,32
 144:   0c000000        jal     0 <_Z9GCD_FUNCTjj>
 148:   00000000        nop
 14c:   1000ffef        b       10c 
<_Z41__static_initialization_and_destruction_0ii
+0x38>
 150:   2e280001        sltiu   t0,s1,1

00000154 <_GLOBAL__I_main>:
_GLOBAL__I_main():
 154:   24040001        li      a0,1
 158:   08000035        j       d4 
<_Z41__static_initialization_and_destruction_0ii>
 15c:   3405ffff        li      a1,0xffff

00000160 <_GLOBAL__D_main>:
_GLOBAL__D_main():
 160:   00002021        move    a0,zero
 164:   08000035        j       d4 
<_Z41__static_initialization_and_destruction_0ii>
 168:   3405ffff        li      a1,0xffff



reply via email to

[Prev in Thread] Current Thread [Next in Thread]