can anyone provide a program for this? Write Wombatl machine language program th
ID: 3728955 • Letter: C
Question
can anyone provide a program for this?
Write Wombatl machine language program that converts a number into a radix representation. For example, a number converted into radix 2 or radix 3. The program should be able to convert a decimal number into any radix from 2 to 8. Your program should take two inputs. The first input will be the number to convert (should be positive) and the second input will be the radix. Note: in CPUSIM each output will be in one line. For example, if the conversion is for 8 bits binary then there will be 8 lines of output. In the binary version of your programs that can be executed on the CPUSim simulator, you are also required to write as comments the line number/address, equivalent decimal version of the binary and appropriate comments to explain each line in your program. The decimal version of the program represents each binary line as two decimal values (first value is the opcode - 4 bits; second value is the operand/memory address 12 bits). Therefore, each line of your program should be formatted as follows: Binary-number, Semi-colon, Line-number/address, Decimal-opcode, Decimal-operand/address, Comments As an example, suppose you are writing a program to read and display a number. This program could be written as follows: 00110000000000000 3 0 read n into accumulator 0010000000001010 2: 2 10 store n at location 10 0001000000001010 4: 1 10 load n from location 10 0100000000000000 6: 4 0 output n 0000000000000000 8: 0 0 stop 000000000000000010: 0 0 storage location for nExplanation / Answer
#include <stdio.h>
#include <string.h>
//Main functionality method
char* decimalToBaseConverter(char result[], int base, int inputNumber)
{
int start = 0;
//Divide by base and use the remainder and continue
while (inputNumber > 0)
{
result[start++] = reVal(inputNumber % base);
inputNumber /= base;
}
result[start] = '';
// Reverse the result
strev(result);
return result;
}
void strev(char *str)
{
int len = strlen(str);
int i;
for (i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}
//Method to return equivalent char representation of corresponding int
char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}
int main()
{
int inputNumber = 444, base = 32;
char result[100];
printf("Equivalent of %d to the base %d is "
" %s ", inputNumber, base, decimalToBaseConverter(result, base, inputNumber));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.