Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "lib/opcad.h"
- #include "lib/util.h"
- #include <stdbool.h>
- /*
- * Función que verifíca si es un comando válido y si lo és regresa el número de argumentos que requiere para funcionar
- * Argumentos: char *comando, cadena.
- * Extendible: Si, se pueden aumentar operaciones.
- */
- int comparaComando(char *comando){
- if( strcmp("MOV", comando) == 0 )
- return 2;
- else if( strcmp("ADD", comando) == 0 )
- return 2;
- else if( strcmp("SUB", comando) == 0 )
- return 2;
- else if( strcmp("MUL", comando) == 0 )
- return 2;
- else if( strcmp("DIV", comando) == 0 )
- return 2;
- else if( strcmp("INC", comando) == 0 )
- return 1;
- else if( strcmp("DEC", comando) == 0 )
- return 1;
- else if( strcmp("JNZ", comando) == 0 )
- return 1;
- else if( strcmp("END", comando) == 0 )
- return 0;
- else if( strcmp("MAX", comando) == 0 )
- return 4;
- else if( strcmp("GET", comando) == 0 )
- return 4;
- else if( strcmp("USE", comando) == 0 )
- return 4;
- else if( strcmp("FRE", comando) == 0 )
- return 4;
- /*
- * Insertar operaciones nuevas aquí
- */
- else
- return 3;
- }
- /*
- * Función que verifica si es un registro válido o no
- * Argumentos: char *registro, cadena.
- * Extendible: Si, Se pueden aumentar registros
- */
- int comparaRegistro( char *registro ){
- if( strcmp("AX", registro) == 0 )
- return 0;
- else if( strcmp("BX", registro) == 0 )
- return 0;
- else if( strcmp("CX", registro) == 0 )
- return 0;
- else if( strcmp("DX", registro) == 0 )
- return 0;
- /*
- * Insertar nuevos registros aquí
- */
- else
- return 1;
- }
- /*
- * Función que verifica si una cadena es un número hexadecimal válido
- * Argumentos: char *reg, cadena.
- * Extendible: No.
- */
- int comparaHex(char *reg){
- int i = 0;
- while( reg[i] != '\0' ){
- switch( reg[i] ){
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case 'A':
- case 'B':
- case 'C':
- case 'D':
- case 'E':
- case 'F':
- case 'a':
- case 'b':
- case 'c':
- case 'd':
- case 'e':
- case 'f':break;
- default: return 1;break;
- }
- i++;
- }
- return 0;
- }
- int mov(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 ){
- if( strcmp("AX", op2) == 0 )
- return 0;
- else if( strcmp("BX", op2) == 0 )
- *AX = *BX;
- else if( strcmp("CX", op2) == 0 )
- *AX = *CX;
- else if( strcmp("DX", op2) == 0 )
- *AX = *DX;
- else if( comparaHex(op2) == 0 )
- *AX = strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("BX", op1) == 0 ){
- if( strcmp("BX", op2) == 0 )
- return 0;
- else if( strcmp("AX", op2) == 0 )
- *BX = *AX;
- else if( strcmp("CX", op2) == 0 )
- *BX = *CX;
- else if( strcmp("DX", op2) == 0 )
- *BX = *DX;
- else if( comparaHex(op2) == 0 )
- *BX = strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("CX", op1) == 0 ){
- if( strcmp("CX", op2) == 0 )
- return 0;
- else if( strcmp("AX", op2) == 0 )
- *CX = *AX;
- else if( strcmp("BX", op2) == 0 )
- *CX = *BX;
- else if( strcmp("DX", op2) == 0 )
- *CX = *DX;
- else if( comparaHex(op2) == 0 )
- *CX = strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("DX", op1) == 0 ){
- if( strcmp("DX", op2) == 0 )
- return 0;
- else if( strcmp("AX", op2) == 0 )
- *DX = *AX;
- else if( strcmp("CX", op2) == 0 )
- *DX = *CX;
- else if( strcmp("BX", op2) == 0 )
- *DX = *BX;
- else if( comparaHex(op2) == 0 )
- *DX = strtohex(op2);
- else
- return 1;
- return 0;
- }
- else
- return 1;
- return 0;
- }
- int sub(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 ){
- if( strcmp("AX", op2) == 0 )
- *AX -= *AX;
- else if( strcmp("BX", op2) == 0 )
- *AX -= *BX;
- else if( strcmp("CX", op2) == 0 )
- *AX -= *CX;
- else if( strcmp("DX", op2) == 0 )
- *AX -= *DX;
- else if( comparaHex(op2) == 0 )
- *AX -= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("BX", op1) == 0 ){
- if( strcmp("BX", op2) == 0 )
- *BX -= *BX;
- else if( strcmp("AX", op2) == 0 )
- *BX -= *AX;
- else if( strcmp("CX", op2) == 0 )
- *BX -= *CX;
- else if( strcmp("DX", op2) == 0 )
- *BX -= *DX;
- else if( comparaHex(op2) == 0 )
- *BX -= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("CX", op1) == 0 ){
- if( strcmp("CX", op2) == 0 )
- *CX -= *CX;
- else if( strcmp("AX", op2) == 0 )
- *CX -= *AX;
- else if( strcmp("BX", op2) == 0 )
- *CX -= *BX;
- else if( strcmp("DX", op2) == 0 )
- *CX -= *DX;
- else if( comparaHex(op2) == 0 )
- *CX -= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("DX", op1) == 0 ){
- if( strcmp("DX", op2) == 0 )
- *DX -= *DX;
- else if( strcmp("AX", op2) == 0 )
- *DX -= *AX;
- else if( strcmp("CX", op2) == 0 )
- *DX -= *CX;
- else if( strcmp("BX", op2) == 0 )
- *DX -= *BX;
- else if( comparaHex(op2) == 0 )
- *DX -= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else
- return 1;
- return 0;
- }
- int mul(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 ){
- if( strcmp("AX", op2) == 0 )
- *AX *= *AX;
- else if( strcmp("BX", op2) == 0 )
- *AX *= *BX;
- else if( strcmp("CX", op2) == 0 )
- *AX *= *CX;
- else if( strcmp("DX", op2) == 0 )
- *AX *= *DX;
- else if( comparaHex(op2) == 0 )
- *AX *= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("BX", op1) == 0 ){
- if( strcmp("BX", op2) == 0 )
- *BX *= *BX;
- else if( strcmp("AX", op2) == 0 )
- *BX *= *AX;
- else if( strcmp("CX", op2) == 0 )
- *BX *= *CX;
- else if( strcmp("DX", op2) == 0 )
- *BX *= *DX;
- else if( comparaHex(op2) == 0 )
- *BX *= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("CX", op1) == 0 ){
- if( strcmp("CX", op2) == 0 )
- *CX *= *CX;
- else if( strcmp("AX", op2) == 0 )
- *CX *= *AX;
- else if( strcmp("BX", op2) == 0 )
- *CX *= *BX;
- else if( strcmp("DX", op2) == 0 )
- *CX *= *DX;
- else if( comparaHex(op2) == 0 )
- *CX *= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("DX", op1) == 0 ){
- if( strcmp("DX", op2) == 0 )
- *DX *= *DX;
- else if( strcmp("AX", op2) == 0 )
- *DX *= *AX;
- else if( strcmp("CX", op2) == 0 )
- *DX *= *CX;
- else if( strcmp("BX", op2) == 0 )
- *DX *= *BX;
- else if( comparaHex(op2) == 0 )
- *DX *= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else
- return 1;
- return 0;
- }
- int divi(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 ){
- if( strcmp("AX", op2) == 0 )
- *AX /= *AX;
- else if( strcmp("BX", op2) == 0 && *BX != 0 )
- *AX /= *BX;
- else if( strcmp("CX", op2) == 0 && *CX != 0 )
- *AX /= *CX;
- else if( strcmp("DX", op2) == 0 && *DX != 0 )
- *AX /= *DX;
- else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
- *AX /= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("BX", op1) == 0 ){
- if( strcmp("BX", op2) == 0 )
- *BX /= *BX;
- else if( strcmp("AX", op2) == 0 && *AX != 0 )
- *BX /= *AX;
- else if( strcmp("CX", op2) == 0 && *CX != 0 )
- *BX /= *CX;
- else if( strcmp("DX", op2) == 0 && *DX != 0 )
- *BX /= *DX;
- else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
- *BX /= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("CX", op1) == 0 ){
- if( strcmp("CX", op2) == 0 )
- *CX /= *CX;
- else if( strcmp("AX", op2) == 0 && *AX != 0 )
- *CX /= *AX;
- else if( strcmp("BX", op2) == 0 && *BX != 0 )
- *CX /= *BX;
- else if( strcmp("DX", op2) == 0 && *DX != 0 )
- *CX /= *DX;
- else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
- *CX /= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("DX", op1) == 0 ){
- if( strcmp("DX", op2) == 0 )
- *DX /= *DX;
- else if( strcmp("AX", op2) == 0 && *AX != 0 )
- *DX /= *AX;
- else if( strcmp("CX", op2) == 0 && *CX != 0 )
- *DX /= *CX;
- else if( strcmp("BX", op2) == 0 && *BX != 0 )
- *DX /= *BX;
- else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
- *DX /= strtohex(op2);
- else
- return 1;
- return 0;
- }
- else
- return 1;
- return 0;
- }
- int inc(char *op1,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 )
- *AX = *AX + 1;
- else if( strcmp("BX", op1) == 0 )
- *BX = *BX + 1;
- else if( strcmp("CX", op1) == 0 )
- *CX = *CX + 1;
- else if( strcmp("DX", op1) == 0 )
- *DX = *DX + 1;
- else
- return 1;
- return 0;
- }
- int dec(char *op1,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 )
- *AX = *AX - 1;
- else if( strcmp("BX", op1) == 0 )
- *BX = *BX - 1;
- else if( strcmp("CX", op1) == 0 )
- *CX = *CX - 1;
- else if( strcmp("DX", op1) == 0 )
- *DX = *DX - 1;
- else
- return 1;
- return 0;
- }
- int end(char *IR){
- if( strlen(IR) != 3 )
- return 1;
- else
- return 0;
- }
- int add(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
- if( strcmp("AX", op1) == 0 ){
- if( strcmp("AX", op2) == 0 )
- *AX += *AX;
- else if( strcmp("BX", op2) == 0 )
- *AX += *BX;
- else if( strcmp("CX", op2) == 0 )
- *AX += *CX;
- else if( strcmp("DX", op2) == 0 )
- *AX += *DX;
- else if( comparaHex(op2) == 0 )
- *AX += strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("BX", op1) == 0 ){
- if( strcmp("BX", op2) == 0 )
- *BX += *BX;
- else if( strcmp("AX", op2) == 0 )
- *BX += *AX;
- else if( strcmp("CX", op2) == 0 )
- *BX += *CX;
- else if( strcmp("DX", op2) == 0 )
- *BX += *DX;
- else if( comparaHex(op2) == 0 )
- *BX += strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("CX", op1) == 0 ){
- if( strcmp("CX", op2) == 0 )
- *CX += *CX;
- else if( strcmp("AX", op2) == 0 )
- *CX += *AX;
- else if( strcmp("BX", op2) == 0 )
- *CX += *BX;
- else if( strcmp("DX", op2) == 0 )
- *CX += *DX;
- else if( comparaHex(op2) == 0 )
- *CX += strtohex(op2);
- else
- return 1;
- return 0;
- }
- else if( strcmp("DX", op1) == 0 ){
- if( strcmp("DX", op2) == 0 )
- *DX += *DX;
- else if( strcmp("AX", op2) == 0 )
- *DX += *AX;
- else if( strcmp("CX", op2) == 0 )
- *DX += *CX;
- else if( strcmp("BX", op2) == 0 )
- *DX += *BX;
- else if( comparaHex(op2) == 0 )
- *DX += strtohex(op2);
- else
- return 1;
- return 0;
- }
- else
- return 1;
- return 0;
- }
- int jump(char *op1, unsigned long long *CX, int *PC){
- int aux = atoi(op1);
- if( *CX != 0 ){
- if ( aux < 0 )
- return 1;
- else{
- *PC = aux;
- return 0;
- }
- }
- else{
- (*PC)++;
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement