Sunteți pe pagina 1din 8

assembly code:

DATA SEGMENT
NUM1 DW 000AH
NUM2 DW 0004H
GCD DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA ;Load the Data to AX.
MOV DS,AX ;Move the Data AX to DS.
MOV AX,NUM1 ;Move the first number to AX.
MOV BX,NUM2 ;Move the second number to BX.
UP: CMP AX,BX ;Compare the two numbers.
JE EXIT ;If equal, go to EXIT label.
JB EXCG ;If first number is below than second,
;go to EXCG label.
UP1: MOV DX,0H ;Initialize the DX.
DIV BX ;Divide the first number by second number.
CMP DX,0 ;Compare remainder is zero or not.
JE EXIT ;If zero, jump to EXIT label.
MOV AX,DX ;If non-zero, move remainder to AX.
JMP UP ;Jump to UP label.
EXCG:XCHG AX,BX ;Exchange the remainder and quotient.
JMP UP1 ;Jump to UP1.
EXIT:MOV GCD,BX ;Store the result in GCD.
MOV AH,4CH
INT 21H
CODE ENDS
END START

Ourcode:

2000 MVI AX,08 ;Move the first number to AX.


2002 MVI BX,04 ;Move the second number to BX.
2004 UP: CMP BX ;Compare the two numbers.
2005 JE EXIT ;If equal, go to EXIT label.
2007 JB EXCG ;If first number is below than second,
;go to EXCG label.
2009 UP1: SUB BX ;Subtract the accumulator by number in BX.
200A CZ EXIT ;If zero flag is set, jump to EXIT label.
200C JMP UP ;Jump to UP label.
200E EXCG: XCHG AX,BX ;Exchange the remainder and quotient.
200F JMP UP1 ;Jump to UP1.
2012 EXIT: MOV AX, BX ;Store result in accumulator
2013 STA 0050 ;Store accumulator result in 5030
2015 HLT ;End program
Opcodes:
Instruction set Opcodes Bytes
Data transfer:

MVI AX 88 2

MVI BX 84 2

MOV AX, BX 82 1

STA Address 81 2

XCHG AX, BX 80 1

Arithematic:

SUB BX 48 1

Control:

CMP BX 2C 1

JE Label 28 2

JB Label 24 2

JMP Label 22 2

CZ Label 21 2

HLT 20 1

Input file: If 16bit addressable

Opcode explanation Actual file input

88 08 0000000010001000
0000000000001000
84 04 0000000010000100
0000000000000100
2C 0000000000101100
28 1220 0000000000101000
0001001000100000
24 0E20 0000000000100100
0000111000100000
48 0000000000101000
21 1220 0000000000100001
0001001000100000
22 0420 0000000000100010
0000010000100000
80 0000000001000000
22 0920 0000000000100010
0000100100100000
82 0000000010000010
81 0050 0000000010000001
0000000001010000
20 0000000000100000

Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

struct proc{
char byte[50];
};

struct reg{
char byte[50]; int set;
};

char memory[][16]={"01010100","00010101","00110110"};
char control[3][50]={"LOAD","ADD","STORE"}; //i dont think we need this control variable
char
register[][16]={"0000000000000000","0000000000000000","0000000000000000","0000000000000
000"};
int flags[2]={0};//flags[0]= 1 when equal flags[1]=1 when num1<num2;
int bytes[3]={1,2,3};
struct proc pc,mar,mdr,ir,temp;
struct reg ax, bx;
int to_b,cu_b,opcode_dec;
struct proc decToBinary(int n,int s)
{

int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}

while(i<s)
{
binaryNum[i]=0;i++;
}

for (int j = i-1, k=0; j>=0,k<s;k++, j--)


{ temp.byte[k]=binaryNum[j]+'0';
}
return temp;
}

int bintodec(int val)


{
int sum=0,p=0,t;
while(val > 0)
{
t=val%10;
sum+=(t*pow(2,p++));
val/=10;
}
return sum;
}

struct proc inc(struct proc src)


{

int d=atoi(src.byte);
d=bintodec(d);
printf("%d\n",d);
d++;
src=decToBinary(d,8);
//printf("%s\n",src.byte);
return src;
}

struct proc map(struct proc src)


{
int opcode_dec=atoi(src.byte);
opcode_dec=bintodec(opcode_dec);
to_b=bytes[opcode_dec];
printf("%s\n",control[d]);
//strcpy(src.byte,control[0][d]);
return src;
}
void fetch()
{
strcpy(mar.byte,pc.byte); //dest,src
pc=inc(pc);
}

void decode()
{
mdr=map(mar);
printf("\n%s\n",mdr.byte);
strcpy(ir.byte,mdr.byte);
}

struct proc data_val()


{
fetch();//increments the pc to 1;
int d=atoi(src.byte);
d=bintodec(d);
printf("%s\n",control[d]);
strcpy(temp.byte,control[d]);
return temp;
}

void transfer(int opcode, struct proc temp)


{
int addr;
char[8] temp = "00000000";
switch(opcode)
{
case 88:
strcpy(register[0],data.byte);
break;
case 84:
strcpy(register[1],data.byte);
break;
case 82:
strcpy(register[0],register[1]);
break;
case 80:
strcpy(temp,register[0]);
strcpy(register[0],register[1]);
strcpy(register[1],temp);
break;
case 81:
addr = atoi(data);
addr = bintodec(addr);
int low = addr%100;
int high = addr/100;
addr = low*100 + high;
strcpy(memory[addr],register[0]);
break;
}
}

void control(int opcode,struct proc temp)


{
switch(opcode)
{
case 824:
int ax = atoi(register[0]);
ax = bintodecimal(ax);
int bx = atoi(register[1]);
bx= bintodecimal(bx);
Compare(ax,bx);
break;

case 28: if(flags[0]==1)


{
int first_byte= temp;
int second_byte =data_val();
register[0] = first_byte;
control(second_byte, temp);
}
// else
// pc++;
break;

case 24: if(flags[1] == 1)


{ int first_byte = temp;
int second_byte = data_val();
register[0]= first_byte;
control(second_byte,temp);
}
break;

case 22:
int first_byte = temp;
int second_byte = data_val();
register[0] = first_byte;
control(second_byte,temp);
break;

case 21: if(flags[0] == 1)


{ int first_byte = temp;
int second_byte= data_val();
register[0] = first_byte();
control(second_byte, temp);
}
break;
case 20:exit(0);
break;
}

void Compare(int A, int B)


{ flags[]={0};
if(A==B)
flags[0]=1;
else if(A < B)
flags[1]=1;
else
flags[2] = 1;
}

void arithematic(int opcode)


{
if (opcode ==48)
{
int bx = atoi(register[1]);
bx = bintodec(bx);
int ax = atoi(register[0]);
ax = bintodec(ax);
ax = ax - bx;
char[8] ax_s = decToBinary(ax);
itoa(register[0], ax_s, 10);
}
}

int main()
{
strcpy(pc.byte,"00000001");
while(1)
{
fetch();
cu_b=1; //current_byte
decode();
if(to_b > 1)
temp=data_val();
if(opcode_dec > 80 && !(opcode_dec == 824))//when 2C is converted to dec we get '824';
transfer(opcode_dec,temp);
else if(opcode_dec == 48)
arithmetic(opcode_dec);
else
control(opcode_dec,temp);
}
return 0;
}

S-ar putea să vă placă și