From 3e07927700136867e4be235ce8ef76e510a16f4e Mon Sep 17 00:00:00 2001 From: Nikita Mahoviya Date: Mon, 27 Sep 2021 19:13:33 +0530 Subject: [PATCH] Added various Assembly Language instructions --- include/xasm/utils.hpp | 4 + src/instruction.cc | 775 +++++++++++++++++++++++++++++++++++++++++ src/utils.cc | 178 ++++++++++ 3 files changed, 957 insertions(+) diff --git a/include/xasm/utils.hpp b/include/xasm/utils.hpp index e5e4908..ce658d4 100644 --- a/include/xasm/utils.hpp +++ b/include/xasm/utils.hpp @@ -31,6 +31,10 @@ bool validityRegisters(string reg); bool validityRegisterPair(string reg); int registerNumber(string str); string hexAdd(string arg1, string arg2, bool flag[], bool carry); +string hexSub(string arg1,string arg2,bool flag[],bool carry); +string increaseAddress(string a); +string complement(string a); +string hexSub16bit(string arg1,string arg2,bool flag[],bool carry); // map &)>> // instruction1{{"add", ADD}}; diff --git a/src/instruction.cc b/src/instruction.cc index 99e75b5..6ca69e2 100644 --- a/src/instruction.cc +++ b/src/instruction.cc @@ -104,4 +104,779 @@ void MVI (string arg1,string arg2,string registers[],bool flags[],map&memory){ + + int length=arg1.length(); + if(length == 1){ + + if(validityRegisters(arg1)){ + + if(arg1 != "M"){ + + /*Fetches index of register to access array string registers[]*/ + int registerID = registerNumber(arg1); + /*Converting decimal value to string format and storing in accumulator*/ + registers[0] = hexSub(registers[registerID],registers[0],flag,true); + + } + else{ + + /*Fetches data of HL pair*/ + string address = ""; + address = address + registers[5] + registers[6]; + if(address >= "0000" && address <= "FFFF"){ + + /*Converting decimal value to string format and storing in accumulator*/ + registers[0] = hexSub(memory[address],registers[0],flag,true); + + } + else{ + cout<<"Error: "<=0;i--){ + + int value = value16bit[i] + value16bitHL[i] + carry; + if(value >= 16) + flag[0] = true; + else + flag[0] = false; + carry = value / 16; + value = value % 16; + if(value>=0 && value<=9) + resultant = (char)('0' + value) + resultant; + else{ + switch(value){ + + case 10: + resultant = "A" + resultant; + break; + case 11: + resultant = "B" + resultant; + break; + case 12: + resultant = "C" + resultant; + break; + case 13: + resultant = "D" + resultant; + break; + case 14: + resultant = "E" + resultant; + break; + case 15: + resultant = "F" + resultant; + break; + } + } + } + + registers[5] = resultant[0] + resultant[1]; + registers[6] = resultant[2] + resultant[3]; + } + else{ + cout<<"Error: "<&memory){ + + int length = arg.length(); + if(length == 1){ + + if(validityRegisters(arg)){ + + if(arg != "M"){ + + /*Performs INR on a register*/ + int registerID = registerNumber(arg); + registers[registerID] = hexAdd(registers[registerID],"01",flag,false); + } + else{ + /*Performs DCR on HL pair*/ + string address = ""; + address = address + registers[5] + registers[6]; + if(validityAddress(address)){ + + memory[address] = hexAdd(memory[address],"01",flag,false); + } + else{ + + cout<<"Error: "< dataInBinary(value); + int parity = dataInBinary.count(); + if(parity%2 == 0 && parity!=0) + flags[2] = true; + else + flags[2] = false; + } +} + +void CMP (string arg1,string registers[],bool flag[],map &memory) +{ + int l1=arg1.length(); + if(l1==1) + { + if(arg1=="M") + { + string address=registers[5]+registers[6]; + if(registers[0]&memory){ + + int length = arg.length(); + if(length == 1){ + + if(validityRegisters(arg)){ + + if(arg != "M"){ + + /*Performs INR on a register*/ + int registerID = registerNumber(arg); + registers[registerID] = hexSub(registers[registerID],"01",flag,false); + } + else{ + /*Performs DCR on HL pair*/ + string address = ""; + address = address + registers[5] + registers[6]; + if(validityAddress(address)){ + + memory[address] = hexSub(memory[address],"01",flag,false); + } + else{ + cout<<"Error: "<=0;i--){ + + int value = value16bit[i] + returnValue[i] + carry; + carry = value / 16; + value = value % 16; + if(value>=0 && value<=9) + resultant = (char)('0'+value) + resultant; + else{ + resultant = (char)('A'+(value-10)) + resultant; + } + } + if(validityAddress(resultant)) + return resultant; + else{ + + cout<<"Error: "<=0;i--){ + + int value = value16bit[i] + returnValue[i] + carry; + carry = value / 16; + value = value % 16; + if(value>=0 && value<=9) + resultant = (char)('0' + value) + resultant; + else{ + resultant = (char)('A' + (value-10)) + resultant; + } + } + if(validityAddress(resultant)) + return resultant; + else + { + cout<<"Error: "<=0;i--){ + + int value = value16bit[i] + returnValue[i] + carry; + carry = value / 16; + value = value % 16; + if(value>=0 && value<=9) + resultant = (char)('0' + value) + resultant; + else{ + resultant = (char)('A'+(value-10)) + resultant; + } + } + if(validityAddress(resultant)) + return resultant; + else + { + cout<<"Error: "<=0;i--){ + + int value = value16bit[i] + returnValue[i] + carry; + carry = value / 16; + value = value % 16; + if(value>=0 && value<=9) + resultant = (char)('0' + value) + resultant; + else{ + resultant = (char)('A'+(value-10)) + resultant; + } + } + if(validityAddress(resultant)) + return resultant; + else + { + cout<<"Error: "< &memory) +{ + int l1=arg1.length(); + if(l1==4) + { + if(validityAddress(arg1)){ + + registers[0] = memory[arg1]; + } + else + { + cout<<"Error: "< &memory) +{ + int l1=arg1.length(); + if(l1==4) + { + if(validityAddress(arg1)&&validityAddress(increaseAddress(arg1))) + { + registers[6]=memory[arg1]; + registers[5]=memory[increaseAddress(arg1)]; + } + else + { + cout<<"Error: "< &memory) +{ + int l1=arg1.length(); + int l2=arg2.length(); + if(l1==1&&l2==4) + { + if(arg1=="M") + { + if(validityAddress(arg2)) + { + string part1="",part2=""; + for(int i=0;i<2;i++) + { + part1+=arg2[i]; + part2+=arg2[i+2]; + } + registers[5]=part1; + registers[6]=part2; + } + else + { + cout<<"Error: "< &memory){ + int l1=arg1.length(); + int l2=arg2.length(); + if(l1==4 && l2==2 && validityAddress(arg1)&&validityData(arg2)) + memory[arg1]=arg2; + else{ + cout<<"Error: "< &memory){ + int l1=arg1.length(); + if(l1 == 4 && validityAddress(arg1) && registers[5]!="" && registers[6]!="" && memory.empty() == false){ + memory[arg1]=registers[6]; + memory[increaseAddress(arg1)]=registers[5]; + } + else{ + cout<<"Error: "< &memory) +{ + int l1=arg1.length(); + if(l1==4) + { + if(validityAddress(arg1)) + memory[arg1]=registers[0]; + else + { + cout<<"Error: "< &memory) +{ + int l1=arg1.length(); + if(l1==1) + { + if(validityRegisterPair(arg1)) + { + string address=""; + int index=registerNumber(arg1); + address+=registers[index]+registers[index+1]; + if(validityAddress(address)) + memory[address]=registers[0]; + else + { + cout<<"Error: "< dataInBinary(variable); + /*Setting parity flag*/ + parity = dataInBinary.count(); + if(parity%2 == 0 && parity!=0) + flag[2] = true; + else + flag[2] = false; + /*Setting sign flag*/ + flag[7] = dataInBinary[7]; + /*Setting zero flag*/ + if(parity == 0) + flag[6] = true; + else + flag[6] = false; + + /*Convert decimal data to hexadecimal and store in accumulator*/ + for(int i = 1;i>=0;--i){ + + if(tempAnswer[i]>=0 && tempAnswer[i]<=9) + resultant = char('0'+tempAnswer[i]) + resultant; + else if(tempAnswer[i]>=10 && tempAnswer[i]<=15) + resultant = (char)('A'+(tempAnswer[i] - 10)) + resultant; + } + + return resultant; + +} + +string increaseAddress(string a) +{ + int number[5]; + string nextAddress=""; + int l=a.length(); + for(int i=l-1;i>=0;i--) + { + if(a[i]>='A'&&a[i]<='F') + number[i]=(int)(10+(a[i]-'A')); + if(a[i]>='0'&&a[i]<='9') + number[i]=(int)(a[i]-'0'); + } + number[l-1]++; + for(int i=l-1;i>=0;i--) + { + if(number[i]>=0&&number[i]<=15) + break; + if(number[i]>=16) + { + number[i]=number[i]%16; + number[i-1]++; + } + } + for(int i=0;i=0&&number[i]<=9) + nextAddress+=(char)('0'+(number[i])); + else if(number[i]>=10 && number[i]<=15) + nextAddress+=(char)('A'+(number[i]-10)); + } + return nextAddress; +} + +string complement(string a) +{ + int l=a.length(); + string complement=""; + for(int i=0;i='0'&&a[i]<='9') + { + int diff=15-(a[i]-'0'); + if(diff>=0&&diff<=9) + complement+=('0'+diff); + else if(diff>=10&&diff<=15) + complement+=('A'+(diff-10)); + } + if(a[i]>='A'&&a[i]<='F') + { + int value=a[i]-'A'; + int diff=15-value; + if(diff>=0&&diff<=9) + complement+=('0'+diff); + else if(diff>=10&&diff<=15) + complement+=('A'+(diff-10)); + } + } + return complement; +} + +string hexSub16bit(string arg1,string arg2,bool flag[],bool carry){ + + string resultant = ""; + int variable; + int parity; + int value1[4]; + int value2[4]; + int tempAnswer[4]; + hexToDecimal(arg1,value1); + hexToDecimal(arg2,value2); + for(int i=3;i>=0;i--){ + + if(value1[i] < value2[i]){ + + tempAnswer[i] = (16 + value1[i]) - value2[i]; + if(i!=0) + --value1[i-1]; + else{ + if(carry) + flag[0] = true; + } + } + else + + tempAnswer[i] = value1[i] - value2[i]; + } + variable = tempAnswer[0]*16*16*16 + tempAnswer[1]*16*16 + tempAnswer[2]*16 + tempAnswer[3]; + bitset<16> dataInBinary(variable); + /*Setting parity flag*/ + parity = dataInBinary.count(); + if(parity%2 == 0 && parity!=0) + flag[2] = true; + else + flag[2] = false; + /*Setting sign flag*/ + flag[7] = dataInBinary[7]; + /*Setting zero flag*/ + if(parity == 0){ + + flag[6] = true; + } + else{ + + flag[6] = false; + } + /*Convert decimal data to hexadecimal and store in accumulator*/ + for(int i = 3;i>=0;--i){ + + if(tempAnswer[i]>=0 && tempAnswer[i]<=9) + resultant = char('0'+tempAnswer[i]) + resultant; + else if(tempAnswer[i]>=10 && tempAnswer[i]<=15) + resultant = (char)('A'+(tempAnswer[i] - 10)) + resultant; + } + + return resultant; + +} \ No newline at end of file