Universidad de Costa Rica

Escuela de Ciencias de la Computación e Informática

CI-0118 Lenguajes ensambladores

Ejemplos

CI0118 / ejemplos / Semana-07 / Vector / nasm


/**
 * CI-0118
 * Grupo 2 (Fco. Arroyo), 2022-ii
 *
 *  Ejemplo de una clase con interacción de métodos en ensamblador
 *
 *
 *   Assemble: nasm -f elf64 vector.asm  
 *   Compile: g++ -g testVector.cc Vectoc.cc vector.o -o testVector.out
 *
**/


#include <iostream>
#include "Vector.h"

/**
 *  Interface to assembly routines
**/
extern "C" long init( int );				// size
extern "C" void destroy( long * );			// addr
extern "C" void allset( long *, int, int );		// addr, size, value
extern "C" void eleset( long *, int, int );		// addr, pos, value
extern "C" long eleget( long *, int );			// addr, pos


/**
 *
 *  Creates a vector of size elements (long)
 *
**/
Vector::Vector( int size ) {

   this->vector = (long *) init( size );	// Call init assembly routine
   if ( 0 == this->vector) {
      exit( 1 );
   }
   this->size = size;
   printf( "vector = 0x%x\n", this->vector);
}


/**
 *  Vector constructor of size elements, initialized to value
 *  
**/
Vector::Vector( int size, long value ) {

   this->vector = (long *) init( size );	// Call init assembly routine
   if ( 0 == this->vector) {
      exit( 1 );
   }
   this->size = size;
   this->allSet( value );

}


/**
 *
 *  Vector destructor, assembly routine calls free method
 *
**/
Vector::~Vector() {
   printf( "Calling destructor\n" );
//   destroy( this->vector );
}


/**
 *   Set the entire vector to an initial value
**/
void Vector::allSet( long value ) {

   allset( this->vector, this->size, value );

}


/**
 *   Set a specific position to a value
**/
void Vector::set( int position, long value ) {

   if ( position >=0 && position < this->size ) {
      eleset( this->vector, position, value );
   }

}


/**
 *   Get the vector to init value
**/
long Vector::get( int position ) {
   long rv = 0;

   if ( position >=0 && position < this->size ) {
      rv = eleget( this->vector, position );
   }

   return rv;

}


/**
 *   Print the vector
**/
void Vector::print() {
   for (int pos = 0; pos < this->size; pos++ ) {
      std::cout << this->get( pos ) << std::endl;
   }
   std::cout << std::endl;
}