-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.c
More file actions
1297 lines (1137 loc) · 25.4 KB
/
CPU.c
File metadata and controls
1297 lines (1137 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SIZE 10
// Function for booting the system
void bootup()
{
printf(" \n____ ______ _____ _______ _____ _____ _ _ \n| _ \ | ____| / ____||__ __| / ____|| __ \ | | | | \n| |_) || |__ | (___ | | ______ | | | |__) || | | | \n| _ < | __| \___ \ | ||______|| | | ___/ | | | | \n| |_) || |____ ____) | | | | |____ | | | |__| | \n|____/ |______||_____/ |_| \_____||_| \____/ \n ");
printf("\n \n \n \t \t \t \t BOOTING....");
sleep(5);
system("cls");
printf(" \n____ ______ _____ _______ _____ _____ _ _ \n| _ \ | ____| / ____||__ __| / ____|| __ \ | | | | \n| |_) || |__ | (___ | | ______ | | | |__) || | | | \n| _ < | __| \___ \ | ||______|| | | ___/ | | | | \n| |_) || |____ ____) | | | | |____ | | | |__| | \n|____/ |______||_____/ |_| \_____||_| \____/ \n ");
printf("\n \n \n \t \t \t \t Initializing....");
sleep(2);
system("cls");
sleep(1);
printf(" \n____ ______ _____ _______ _____ _____ _ _ \n| _ \ | ____| / ____||__ __| / ____|| __ \ | | | | \n| |_) || |__ | (___ | | ______ | | | |__) || | | | \n| _ < | __| \___ \ | ||______|| | | ___/ | | | | \n| |_) || |____ ____) | | | | |____ | | | |__| | \n|____/ |______||_____/ |_| \_____||_| \____/ \n \n \n");
sleep(1);
};
void binary_search(int [], int, int, int);
void bubble_sort(int [], int);
void bubble_sort(int list[], int size)
{
int temp, i, j;
for (i = 0; i < size; i++)
{
for (j = i; j < size; j++)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void binary_search(int list[], int lo, int hi, int key)
{
int mid;
if (lo > hi)
{
printf("Element not found!!!!\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
printf("\n Search on an element > middle of the array \n");
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
printf("\n Search on an element < middle of the array \n");
}
}
/*BEST CPU registers*/
struct registers{
int r[10];
int *MAR;
int MDR;
char **PC;
}reg;
/*opcode and operands 1,2,3 structures*/
struct operations{
char opcode[5];
char op_1[8];
char op_2[8];
char op_3[8];
}op;
/*Stack for storing functons*/
struct stack{
int stk[SIZE];
int top;
};
typedef struct stack STACK;
STACK s;
int count=0; // total number of instructions in instruction memory
int i;
//int remainder;
int mem[100]={0}; // Memory block represented in array
char *instr_mem[30]; // Instruction memory to store instructions
/*Displaying function*/
void val_disp()
{
printf("\n********REGISTERS Values**********\n");
printf("\n-------------------------\n|\tr[0] = %d\t|\n-------------------------\n|\tr[1] = %d\t|\n-------------------------\n|\tr[2] = %d\t|\n-------------------------\n|\tr[3] = %d\t|\n-------------------------\n|\tr[4] = %d\t|\n-------------------------\n|\tr[5] = %d\t|\n-------------------------\n|\tr[6]= %d\t\t|\n-------------------------\n|\tr[7] = %d\t|\n-------------------------\n|\tr[8]=%d\t\t|\n-------------------------\n|\tMAR = %p\t|\n-------------------------\n|\tMDR = %d\t\t|\n-------------------------\n|\tPC = %p\t|\n-------------------------\n", reg.r[0],reg.r[1],reg.r[2],reg.r[3],reg.r[4],reg.r[5],reg.r[6],reg.r[7],reg.r[8],reg.MAR,reg.MDR,reg.PC);
printf("\n<************REGISTERS Values********>\n");
}
/*Loading from memory*/
void mem_load()
{
int x,a;
char ch[2];
char str[1];
ch[0]=op.op_2[4];
ch[1]=op.op_2[5];
ch[2]='\0';
x=atoi(ch);
if(op.op_1[0]=='M' ||op.op_1[0]=='m' )
{
reg.MAR= &mem[x];
reg.MDR= mem[x];
printf("\nLoading from memory\n");
printf("\nMAR-->%p\t MDR--->%d\t mem[%d] = %d\n\n",reg.MAR, reg.MDR, x, mem[x]);
return;
}
else if(op.op_1[0]=='r')
{
str[0]=op.op_1[2];
str[1]='\0';
a=atoi(str);
reg.MAR=&mem[x];
reg.r[a]= mem[x];
printf("\nLoading from memory\n");
printf("\nMAR-->%p\t reg[%d]--->%d\t mem[%d] = %d\n\n",reg.MAR,a, reg.r[a], x, mem[x]);
return;
}
}
/*storing into memory*/
void mem_store()
{
int x,a;
char ch[2],str[1];
ch[0]=op.op_1[4];
ch[1]=op.op_1[5];
ch[2]='\0';
x=atoi(ch);
if(op.op_2[0]=='m' || op.op_2[0]=='M' )
{
reg.MAR= &mem[x];
mem[x]=reg.MDR;
printf("\nStoring to memory-------->\n");
printf("\nMAR-->%p\t MDR--->%d\t mem[%d] = %d\n\n",reg.MAR, reg.MDR, x, mem[x]);
return;
}
else if(op.op_2[0]=='r' )
{
str[0] = op.op_2[2];
str[1]='\0';
a= atoi(str);
reg.MAR= &mem[x];
mem[x] = reg.r[a];
printf("\nMAR-->%p\t reg[%d]--->%d\t mem[%d] = %d\n\n",reg.MAR,a, reg.r[a], x, mem[x]);
return;
}
}
/*pushing values into stack*/
void push_stack()
{
int x;
char ch[1];
ch[0]=op.op_1[2];
ch[1]='\0';
if (s.top == (SIZE -1)) {
printf("End of Stack\n");
} else{
x= atoi(ch);
s.top=s.top+1;
s.stk[s.top] = reg.r[x];
printf("\n\npush_stacking--->r[%d] = %d",x,s.stk[s.top]);
}
}
int pop_stack()
{
if (s.top == -1){
printf("End of Stack\n");
} else
return s.stk[s.top];
}
/*Load immediate (li) for initialising registers*/
void init()
{
char ch[1];
char ch1[4];
int x,y;
ch[0]=op.op_1[2];
ch[1]='\0';
x=atoi(ch);
y=atoi(op.op_2);
reg.r[x]=y;
printf("Result :\t r[%d]=%d\n\n",x,reg.r[x]);
return;
}
void load_effective_address()
{
int *p,a,x,y;
char ch[2],str[1];
ch[0]=op.op_2[4];
ch[1]=op.op_2[5];
ch[2]='\0';
x=atoi(ch);
str[0]=op.op_1[2];
str[1]='\0';
y=atoi(str);
if(op.op_2[0]=='M' ||op.op_2[0]=='m' )
{
p= &mem[x];
reg.r[y]=(size_t)p;
printf("\nLoading effective address\n");
printf("\nr[%d] = %d \t addr_mem[%d] = %d\n\n",y,reg.r[y], x, reg.r[y]);
return;
}
}
/*Load, Store, push_stack and pop_stack operations*/
void oper_mem()
{
if (strcmp(op.opcode, "ld") == 0)
{
mem_load();
return;
}
else if (strcmp(op.opcode, "sd") == 0)
{
mem_store();
return;
}
else if (strcmp(op.opcode, "push") == 0)
{
push_stack();
return;
}
else if (strcmp(op.opcode, "pop") == 0)
{
printf("\npop_stacked item----->%d",pop_stack());
s.top--;
return;
}
else if(strcmp(op.opcode, "LEA")==0)
{
load_effective_address();
return;
}
else
{
printf("Sorry Not all Instructions defined yet\n");
return;
}
}
void add_oper()
{
char ch_1[1];
char ch_2[1];
char ch_3[1];
unsigned int carry;
unsigned int sum;
int a_1, a_2, a_3;
int temp;
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a_1=atoi(ch_1);
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
a_2=atoi(ch_2);
temp=reg.r[a_2];
ch_3[0]=op.op_3[2];
ch_3[1]='\0';
a_3=atoi(ch_3);
sum = reg.r[a_2] ^ reg.r[a_3];
carry = reg.r[a_2] & reg.r[a_3];
while(carry!=0)
{
carry=carry<<1;
int x=sum;
int y=carry;
sum=x^y;
carry=x&y;
}
reg.r[a_1]= sum;
printf("\nr[%d]=%d + r[%d]=%d = r[%d]=%d \t carry = %d\n",a_2, temp, a_3, reg.r[a_3], a_1, reg.r[a_1],carry);
if(reg.r[a_1] > 2147483647)
printf("overflow occurs\n");
else
printf("no overflow\n");
return;
}
int add_logic(int c,int d)
{
int sum=c^d;
int carry=c&d;
while(carry!=0)
{
carry=carry<<1;
int x=sum;
int y=carry;
sum=x^y;
carry=x&y;
}
return sum;
}
void subtract()
{
char ch_1[1];
char ch_2[1];
char ch_3[1];
int result;
int sum;
int a_1, a_2, a_3, b,a;
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a_1=atoi(ch_1);
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
a_2=atoi(ch_2);
ch_3[0]=op.op_3[2];
ch_3[1]='\0';
a_3=atoi(ch_3);
b=reg.r[a_3];
a=reg.r[a_2];
result=add_logic(~b,1);
printf("%d\n",result);
sum=add_logic(a,result);
reg.r[a_1]=sum;
printf("\nr[%d]=%d - [%d]=%d = r[%d]=%d \n",a_2, reg.r[a_2], a_3, reg.r[a_3], a_1, reg.r[a_1]);
return;
}
void multiplication()
{
char ch_1[1];
char ch_2[1];
char ch_3[1];
int array[10];
int result;
static int i=1;
int sum;
int j=1;
int a_1, a_2, a_3, b,a;
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a_1=atoi(ch_1);
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
a_2=atoi(ch_2);
ch_3[0]=op.op_3[2];
ch_3[1]='\0';
a_3=atoi(ch_3);
a= reg.r[a_2];
b= reg.r[a_3];
/* multiplication logic*/
if((a&1)!=0)
{
array[0]=b;
}
else
{
array[0]=0;
}
while(a>1)
{
a=a>>1;
b=b<<1;
if((a&1)!=0)
{
array[i]=b;
i++;
}
}
for(j=1;j<i;j++)
{
result=add_logic(array[0],array[j]);
}
reg.r[a_1]= result;
printf("\nr[%d]=%d * [%d]=%d = r[%d]=%d \n",a_2, reg.r[a_2], a_3, reg.r[a_3], a_1, reg.r[a_1]);
return;
}
/*int divi(int tempdividend, int tempdivisor) {
int quotient = 1;
if (tempdivisor == tempdividend) {
remainder = 0;
return 1;
} else if (tempdividend < tempdivisor) {
remainder = tempdividend;
return 0;
}
do{
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
} while (tempdivisor <= tempdividend);
quotient = quotient + divi(tempdividend - tempdivisor, reg.r[]);
return quotient;
}*/
/*void division()
{
char ch_1[1];
char ch_2[1];
char ch_3[1];
int array[10];
int quotient;
static int i=1;
int s;
int a_1, a_2, a_3, b,a;
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a_1=atoi(ch_1);
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
a_2=atoi(ch_2);
ch_3[0]=op.op_3[2];
ch_3[1]='\0';
a_3=atoi(ch_3);
a= reg.r[a_2];
b= reg.r[a_3];
quotient = divi(a,b);
reg.r[a_1]= quotient;
printf("\nr[%d]=%d / [%d]=%d = r[%d]=%d \n",a_2, reg.r[a_2], a_3, reg.r[a_3], a_1, reg.r[a_1]);
return;
}*/
void modulus()
{
char ch_1[1];
char ch_2[1];
char ch_3[1];
int array[10];
int result;
static int i=1;
int s;
int a_1, a_2, a_3, b,a;
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a_1=atoi(ch_1);
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
a_2=atoi(ch_2);
ch_3[0]=op.op_3[2];
ch_3[1]='\0';
a_3=atoi(ch_3);
a= reg.r[a_2];
b= reg.r[a_3];
s=a&b-1;
reg.r[a_1]= s;
printf("\nr[%d]=%d mod [%d]=%d = r[%d]=%d \n",a_2, reg.r[a_2], a_3, reg.r[a_3], a_1, reg.r[a_1]);
return;
}
/*Arithmetic operations*/
void oper_ALU()
{
if (strcmp(op.opcode, "add") == 0)
{
add_oper();
return;
}
else if (strcmp(op.opcode, "sub") == 0)
{
subtract();
return;
}
else if (strcmp(op.opcode, "mul") == 0)
{
multiplication();
return;
}
else if (strcmp(op.opcode, "mod") == 0)
{
modulus();
return;
}
else if (strcmp(op.opcode, "div") == 0)
{
//division();
return;
}
}
//****Branch equal (beq)
int beq()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 == val_2)
{
printf("\nbeq r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch equal (beq) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//****Branch not to equal (bne)
int bne()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 != val_2)
{
printf("\nbne r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch not equal (bne) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//****Branch greater than or equal to zero (bgez)
int bgez()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 >= 0)
{
printf("\nbgez r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch greater than or equal to zero (bgez) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//****Branch greater than or equal (bge)
int bge()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 >= val_2)
{
printf("\nbge r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch not equal (bge) is true ------>go out of the loop****\n\n");
return 0;
}
else
return 1;
}
//Branch less than or equal(ble)
int ble()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 <= val_2)
{
printf("\nble r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch less than or equal(ble) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//Branch less than or equal to zero (blez)
int blez()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 <= 0)
{
printf("\nblez r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****//Branch less than or equal to zero(blez) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//Branch greater than or equal to zero(bgtz)
int bgtz()
{
int a,b;
int temp;
static int zero=0;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 > zero)
{
printf("\nbgtz r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch greater than or equal to zero(bgtz) ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//Branch less than zero(bltz)
int bltz()
{
int a,b;
int temp;
static int zero=0;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 < zero)
{
printf("\nbltz r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch less than zero(bltz) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//Branch greater than (bgt)
int bgt()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 < val_2)
{
printf("\nbgt r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch greater than (bgt) ------>go out of the loop****\n\n");
return 0;
}
else
return 1;
}
//Branch less than (blt)
int blt()
{
int a,b;
int temp;
static int zero=0;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=op.op_1[2];
ch_1[1]='\0';
a= atoi(ch_1);
x=a;
val_1=reg.r[a];
ch_2[0]=op.op_2[2];
ch_2[1]='\0';
b= atoi(ch_2);
y=b;
val_2=reg.r[b];
reg.r[x]=val_1;
reg.r[y]=val_2;
printf("\nr[%d]= %d \t r[%d] = %d \n",x,reg.r[x],y,reg.r[y]);
val_disp();
if(val_1 < val_2)
{
printf("\nblt r[%d]= %d, r[%d] = %d, end;\n",x,reg.r[x],y,reg.r[y]);
printf("\n****Branch less than (blt) is true ------>go out of the loop****\n\n");
return 1;
}
else
return 0;
}
//Loop implementation here
void loop(int index)
{
int i,j,equal,j_index;
j_index=index;
//Jump to an address, and store the return address in a register.
int jal()
{
loop(j_index);
return reg.r[i];
}
//Jump to an address
int j_() {
loop(j_index);
return;
}
/*
int while_loop()
{
int a,b;
int temp;
int val_1,val_2,x,y;
char ch_1[1];
char ch_2[1];
ch_1[0]=0;-
} */
for(i=index; i< count; i++)
{
reg.PC = &instr_mem[i];
sscanf(instr_mem[i], "%s\n",op.opcode);
printf("\nPC = %p\tIstruction = %s",reg.PC,instr_mem[i]);
if(!(strcmp(op.opcode,"beq")))
{
printf("\nExecuting branch equal instruction\n");
sscanf(instr_mem[i], "%s %s %s\n",op.opcode,op.op_1,op.op_2);
op.op_1[strlen(op.op_1)-1]='\0';
op.op_2[strlen(op.op_2)-1]='\0';
printf("\nopcode = %s\top_1 = %s\top_2 = %s\tend\tPC = %p\n", op.opcode, op.op_1, op.op_2,reg.PC);
equal = beq(); //if condition is trure return 1 else 0
if(equal<=1)
return;
else
continue;
}
if(!(strcmp(op.opcode,"bge")))
{
printf("\nExecuting bge instruction\n");
sscanf(instr_mem[i], "%s %s %s\n",op.opcode,op.op_1,op.op_2);
op.op_1[strlen(op.op_1)-1]='\0';
op.op_2[strlen(op.op_2)-1]='\0';
printf("\nopcode = %s\top_1 = %s\top_2 = %s\tend\tPC = %p\n", op.opcode, op.op_1, op.op_2,reg.PC);
equal = bge(); //if condition is trure return 1 else 0
if(equal==1)
return;
else
continue;
}
if(!(strcmp(op.opcode,"ble")))
{
printf("\nExecuting bge instruction\n");
sscanf(instr_mem[i], "%s %s %s\n",op.opcode,op.op_1,op.op_2);
op.op_1[strlen(op.op_1)-1]='\0';
op.op_2[strlen(op.op_2)-1]='\0';
printf("\nopcode = %s\top_1 = %s\top_2 = %s\tend\tPC = %p\n", op.opcode, op.op_1, op.op_2,reg.PC);
equal = bge(); //if condition is trure return 1 else 0
if(equal==1)
return;
else
continue;
}
if(!(strcmp(op.opcode,"blt")))
{
printf("\nExecuting blt instruction\n");
sscanf(instr_mem[i], "%s %s %s\n",op.opcode,op.op_1,op.op_2);
op.op_1[strlen(op.op_1)-1]='\0';
op.op_2[strlen(op.op_2)-1]='\0';
printf("\nopcode = %s\top_1 = %s\top_2 = %s\tend\tPC = %p\n", op.opcode, op.op_1, op.op_2,reg.PC);
equal = blt(); //if condition is trure return 1 else 0
if(equal==0)
return;
else
continue;
}
if(!(strcmp(op.opcode,"bgt")))
{
printf("\nExecuting bgt instruction\n");
sscanf(instr_mem[i], "%s %s %s\n",op.opcode,op.op_1,op.op_2);