Hello everyone,

I have to write a code to creat a pseudo assembler..I have an input file from which i read the assembly instructions from there i get the register numbers ...i have to convert that into machine code..i.e binary form...
Now my problem is that i can find the register values convert them into integer.....i can convert the integer into binary using itoa.......
but if my register number is 5 i am getting the binary as 101....but i need the binary to be 00101....can anyone please explain how to get the zero padding done......

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by firstPerson

Hello everyone,

I have to write a code to creat a pseudo assembler..I have an input file from which i read the assembly instructions from there i get the register numbers ...i have to convert that into machine code..i.e binary form...
Now my problem is that i can find the register values convert them into integer.....i can convert the integer into binary using itoa.......
but if my register number is 5 i am getting the binary as 101....but i need the binary to be 00101....can anyone please explain how to get the zero padding done......

for adding those extra zeroes u can just write a function which which check the lenght of the inputted binary string and accordingly will put extra zeroes at the beginning to make it of the particular length that u want.

``````void zeroPadding(char *bin, char *paddedNum, int requiredLen)
{
/*paddedNum should have enough memory allocated(==requiredLen+1)*/

int inLen = strlen(bin);
int i;
for(i=0;i<requiredLen-inLen;i++){
}
while(*bin){
bin++;
}
return;
}``````

use it as

``````char *bin = "101"; //say
char *newBin = (char *)malloc(6); //e.g. if u want the binary of length 5

hope that helps................

Thanks A Million!!!:cool:

Edited by gauri_phatak_87: n/a

Thanks A Million!!!:cool:

u r welcome.

Another way using std::string;

``````#include<iostream>
#include<algorithm>
#include<string>

using std::string;
using std::cout;

int toChar(char c){
return c + '0';
}

{
string Str = "";
//Did not want to support radix greater than 10. You do it.
return Str;

while(base10Number){
}

Str += '0';

std::reverse(Str.begin(),Str.end());

return Str;
}
int main()
{
cout << convertBase10_To(2,15);

return 0;
}``````

Edited by firstPerson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.