From f1740fd228f78e82dcea870a1c502c2772a96f3d Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Fri, 22 Mar 2024 17:20:01 -0400 Subject: [PATCH] finished lab --- instructions.c | 25 +++++++++++++++++------ lab04 | Bin 22312 -> 22888 bytes lab04.c | 53 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/instructions.c b/instructions.c index e86eea3..b8d94c1 100644 --- a/instructions.c +++ b/instructions.c @@ -1,4 +1,4 @@ -// PID: 730677144 +// PID: 730677144 // I pledge the COMP 211 honor code. #include @@ -16,7 +16,7 @@ // Return: instruction_type: R_TYPE or I_TYPE (see structures) // instruction_type get_type_of_instruction(uint32_t instruct) { - if (bit_select(instruct, 26, 32) == 0) { + if (bit_select(instruct, OPCODE_START_BIT, OPCODE_END_BIT) == 0) { return R_TYPE; } else { return I_TYPE; @@ -35,8 +35,15 @@ instruction_type get_type_of_instruction(uint32_t instruct) { // you will have to set: rs, rt, rd, shamt, func // r_instruction* create_r_instruction(uint32_t instruct) { - // TODO - return NULL; + r_instruction* r = malloc(sizeof(r_instruction)); + + r->rs = bit_select(instruct, RS_START_BIT, RS_END_BIT); + r->rt = bit_select(instruct, RT_START_BIT, RT_END_BIT); + r->rd = bit_select(instruct, RD_START_BIT, RD_END_BIT); + r->shamt = bit_select(instruct, SHAMT_START_BIT, SHAMT_END_BIT); + r->func = bit_select(instruct, FUNC_START_BIT, FUNC_END_BIT); + + return r; } // end create_r_instruction() function // ------------------------------------ @@ -50,6 +57,12 @@ r_instruction* create_r_instruction(uint32_t instruct) { // you will have to set: opcode, rs, rt, immediate // i_instruction* create_i_instruction(uint32_t instruct) { - // TODO - return NULL; + i_instruction* i = malloc(sizeof(i_instruction)); + + i->opcode = bit_select(instruct, OPCODE_START_BIT, OPCODE_END_BIT); + i->rs = bit_select(instruct, RS_START_BIT, RS_END_BIT); + i->rt = bit_select(instruct, RT_START_BIT, RT_END_BIT); + i->immediate = bit_select(instruct, IMMEDIATE_START_BIT, IMMEDIATE_END_BIT); + + return i; } // end create_i_instruction() function diff --git a/lab04 b/lab04 index 8f81b49ae5ccd1e3f8926f825f2664e0ce8010fa..4f12e0327620ccc88a23feb62fb08f5bd3a76c71 100755 GIT binary patch delta 6721 zcmb_hdvui5wLj;Z`7-Zk0(l{m;hFH5nFK;Y9wZO~6Qd9iSiGtU25!bhA;N%o3mszQ z%D_bl$0c4xi`I5Y#RtZUf@07XuP(j9s%^3LR-|}uQdkOFZB*L1zkR-MCdo*<`o}$4 z-`QvH-`@M|v(Gu-IrHVoXL#@Pd~c4o$IWD^)d(wW>YDIVv44R_9K6ZY)mvcwz^ox3 zn1z=IEQbvmdssB^pk=$FahF90|BH9FU*iU-*rog-tMT?K$IfS`uc!_0;=g|9tBQqX z504aE4TZT5sB=6SWX3zFR!HU)uNo@3TYPND<~~{9CvG=pi&|q2pDX#Ni03)7Jr8w| ze^0iLNqMQv1W0_n3uVFOC-wLH$`u4YuUiQ-?Db?`dfupJ$0kPj7-5F zmo#ik!DWpM~R} zMD5|-)u+TQp7%2HAUWX^Q@sVTaOb6h__j;0(GYtt{xy*Z$Lsrd*Izm|jC68Q#oz3m zg=piihj-Wa?{pJJ^K>Fh!^RC@O`Jd*fi4ZS3<4DbDH$gyaqxQSZ;3=qciUtj6moWg zA_4jy&?IQ2uHSlNF-DP$mmKw04T@JAlV&RhY-PNI$8yI2hInIXqgg%(l>x(HYBbd; za80}^EhbFTTr8<@{LPohq<7y{q;gOFg>d{7pyt8Qen6f5JX-demBpYMCe__)%Oo;C zd29srNV^nvlQg^D9Z-3RWPM+v+FgI4vp=!PDtqetqb5Ph{)Tky=-E^V9GoA~VlsP? zyIzFV`zpEA+rRuak+?VxdNBY`YKoTuUM591-Z;tjBTz&fuVYdPdIs!kE8wX2eze}K zzltWi*tom?YWTtMo-7La_(aBBM~#Z>XpbEFLCsXl$`4`Hx$UYingz``kXR~VtpwFy zg?HxAh~!e%Fh&M%Rlyo&957_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ZFr3=bDQq7oH6=t!<7+Y zyg;;m;!Ng+BPmX5>dug^T)|XA-vUs9wV*Pz?j#!I>S+h++a8?%|ZBMis zveL6$Ji+Kp!M^7*tF^*v!22g&<`q_}HJDgxMOrI`+P!?b8(CGwSa&e7T+QeZbIPyq z`$Yb<{gX6J`kfN8Z+09s{m6L!;iP;r%@jC0NSYYnN zBo(|8XNr{-sdOnYR)$C7S&}18eY2@#C3O4_BJCh-8xdt1GfS~M)!^ca`H*5Vl`GCU zcPqR>DK^-=iVbZ2O{|2UDf~w!`lgSYvl}^1;Q1Vn+MlHx98<|qf7i*1s$d2q5w&|e zI60Y!V#63(62xTxE%EVll<`-T_>bN=4-_H4FMZ`@1?41jLvEc?XmEHHzKyUP*!$#+ zI4%~2PoGN~-9680;zOl_0+OW2zz3FzgG2GO2`yKGoK0#Mi1?N_(Qd8(o4O>EM zyv_LX@ZiUvEaq0tb${Tb*$*`jScy3p6Gy62+_5jFm^}!F9l}+84{sD* z)jn<#uT@t|2RuRwJj(M#Pr%3h;zS^YKCeCrc-_OR?=`HWlet-*$PW74!{5fA!|IaU ztXPDCzQ}Qx)_?2`>cW9>Vif!Qv2kfgw|)Y~9{UxZqnqjWCH+A?k6yMcQ_tfcmS=+Q zuU`PAxb(G5KHYFlPlTU(YNJb&IvD48Ll%ijgDy|xY%du*Cl*5Z(Tf9Y+CFT zXBOuTep|=BvAczNNyJ3AE;W(ABt`sgiObxrpU!S^VoA~9e^+AryUrjiwO)%YNsGcs zn^{{WJ;TIyXb*TaEso^dwNuTWY)$m^@>{Dp4~XDWpE=L3Df7gZrA1=XQjBeBG0rVt zHl42$GnP#^f1Rg!Ocq_sip)8sn)HY`yR67^NdG^DQxcZCsphxze?BycU|o@vGfQl( jt1t& @@ -32,12 +32,10 @@ int main() { return EXIT_SUCCESS; if (get_type_of_instruction(instruct) == R_TYPE) { - printf("R_TYPE instruction\n"); r_instruction* r_instruct = create_r_instruction(instruct); execute_r_instruction(r_instruct); free(r_instruct); } else { // I_TYPE - printf("I_TYPE instruction\n"); i_instruction* i_instruct = create_i_instruction(instruct); execute_i_instruction(i_instruct); free(i_instruct); @@ -72,7 +70,36 @@ int main() { // Return: None // void execute_r_instruction(r_instruction* instruct) { - // TODO + switch (instruct->func) { + case SLL_FUNC: + registers[instruct->rd] = registers[instruct->rt] + << instruct->shamt; + break; + case SRA_FUNC: + registers[instruct->rd] = + registers[instruct->rt] >> instruct->shamt; + break; + case ADD_FUNC: + registers[instruct->rd] = + registers[instruct->rs] + registers[instruct->rt]; + break; + case SUB_FUNC: + registers[instruct->rd] = + registers[instruct->rs] - registers[instruct->rt]; + break; + case AND_FUNC: + registers[instruct->rd] = + registers[instruct->rs] & registers[instruct->rt]; + break; + case OR_FUNC: + registers[instruct->rd] = + registers[instruct->rs] | registers[instruct->rt]; + break; + default: + fprintf(stderr, "Invalid function code!\n"); + exit(EXIT_FAILURE); + } + } // end execute_r_instruction() function // ------------------------------------ @@ -89,5 +116,21 @@ void execute_r_instruction(r_instruction* instruct) { // Return: None // void execute_i_instruction(i_instruction* instruct) { - // TODO + switch (instruct->opcode) { + case ADDI_OPCODE: + registers[instruct->rt] = + registers[instruct->rs] + instruct->immediate; + break; + case ANDI_OPCODE: + registers[instruct->rt] = + registers[instruct->rs] & instruct->immediate; + break; + case ORI_OPCODE: + registers[instruct->rt] = + registers[instruct->rs] | instruct->immediate; + break; + default: + fprintf(stderr, "Invalid opcode!\n"); + exit(EXIT_FAILURE); + } } // end execute_i_instruction() function