-
4 digit number
i want to fill an array with 4 digit numbers ie location1 = 0040
location2 = 0050
location3 = 1234
and so on....
at the moment i am doing it as my code shows
Code:
/* CPU with 16bit instruction - 8bit instruction followed by 8bit operand, four
* addressing modes: absolute, literal, indexed, relative */
using System;
namespace cpu16bit_sim
{
class cpu_16bit
{
const int MOVE = 00;
const int ADD = 01;
const int SUB = 02;
const int BRA = 03;
const int CMP = 04;
const int BEQ = 05;
const int BNE = 06;
const int EXG = 07;
const int STOP = 15;
/* instruction format: */
/* 7 6 5 4 3 2 1 0 */
/* bits 1 and 0 = 2bit addressing mode */
/* 00 address mode = absolute */
/* 01 address mode = literal */
/* 10 address mode = indexed */
/* 11 address mode = relative */
/* bit 2 = 1bit direction (source/operand) */
/* bit 3 = not used */
/* bit 4 to 7 4bit instruction code */
static void Main(string[] args)
{
int PC = 0; // program counter
int D0 = 0; // data register
int A0 = 0; // address register
int CCR = 0; // condition code register
int MAR = 0; // memory address register
int MBR = 0; // memory buffer register
int IR = 0; // instruction register
int operand = 0; // 8bit operand from the IR
int source = 0; // source operand
int destination = 0; // destination value
int opcode = 0; // 4bit opcode from IR
int amode = 0; // 2bit addressing mode
int direction; // 1bit data direction
int[] memory = new int[256];// 256 byte memory
bool run = true; // program flag
int init = 0;
while(init != -1)
{
int fill = 0;
Console.Write("Please enter program one instruction at a");
Console.WriteLine(" time, type -1 to end");
Console.WriteLine("MOVE = 00");
Console.WriteLine("ADD = 01");
Console.WriteLine("SUB = 02");
Console.WriteLine("BRA = 03");
Console.WriteLine("CMP = 04");
Console.WriteLine("BEQ = 05");
Console.WriteLine("BNE = 06");
Console.WriteLine("EXG = 07");
Console.WriteLine("STOP = 15");
memory[fill++] = Int32.Parse( Console.ReadLine() );
Console.WriteLine(" another instruction?");
init = Int32.Parse( Console.ReadLine() );
}
while(run)
{
MAR = PC;
PC += 1;
MBR = memory[MAR]; // fetch next instruction
IR = MBR;
opcode = IR;
MAR = PC;
PC += 1;
MBR = memory[MAR]; // fetch operand
IR = MBR;
operand = IR;
amode = (opcode & 0x03); // extract the address mode bits
direction = (opcode & 0x04) >> 2; /* get data direction
* 0 = reg to mem
* 1 = mem to reg */
opcode = (opcode >> 0x04); // extract 4bit instruction code
// use the addressing mode to get the source operand
switch(amode)
{
case 0: {source = memory[operand]; break;} // absolute
case 1: {source = operand; break;} // literal
case 2: {source = memory[A0+operand]; break;} // indexed
case 3: {source = memory[PC+operand]; break;}// PC relative
}
// execute the instruction
switch(opcode)
{
case MOVE:
{
if(direction==0) destination=D0;
else D0 = source;
if(D0==0) CCR=1;
else CCR=0;
break;
}
case ADD:
{
if(direction==0)
{
destination=D0+source;
if(destination==0) CCR=1; else CCR=0;}
else
{
D0=D0+source;
if(D0==0) CCR=1; else CCR=0;
}
break;
}
case SUB:
{
if(direction==0)
{
destination=D0-source;
if(destination==0) CCR=1; else CCR=0;
}
else
{
D0=D0-source;
if(D0==0) CCR=1; else CCR=0;
}
break;
}
case BRA:
{
if(amode==0) PC=operand;
if(amode==1) PC=PC+operand;
break;
}
case CMP:
{
MBR=D0-source;
if(MBR==0) CCR=1;
else CCR=0;
break;
}
case BEQ:
{
if(CCR==1){
if(amode==0) PC=operand;
if(amode==1) PC=PC+operand;
}break;
}
case BNE:
{
if(CCR!=1){
if(amode==0) PC=operand;
if(amode==1) PC=PC+operand;
}break;
}
case EXG:
{
MBR=D0; D0=A0; A0=MBR; break;
}
case STOP:
{
run=false; break;
}
}// end switch(opcode)
// save result in memory if register to memory
if(direction==0)
switch(amode)
{
case 0: {memory[operand]=destination; break; //absolute
}
case 1: { break; //literal
}
case 2: {memory[A0+operand]=destination; //indexed
break;
}
case 3: {memory[PC+operand]=destination; //PC relative
break;
}
}
}// end while(run)
}// end main
}// end class
}//end namespace
however the 4 digit number is only correct when the leading digits dont equal zero, ie 1234 if i enter 0040 the leading zeros are stripped leaving 40 in array, how can i fill the array from a digit number entered by the user that has 2 leading zeros
luigi
-
Eww that looks like C code! :P
Anyway, I don't have the time to read all your code, but I believe your problem is in calling Int.Parse on Console.ReadLine(). The integer "0040" can be represented as "40" or "00000040", it's really the same thing. If you need to keep it as 0040, then perhaps consider storing it as an array of strings. Otherwise when outputting it there is a way to 'pad' output by adding extra zeros to the front if needed (this should help: http://idunno.org/displayBlog.aspx/2004071401).
Post a smaller example that highlights your problem and I'll be happy to have a proper read.