Beispiel #1
0
void test3()
{
    unsigned int a = 0xFFFF1009;

    printf("a:\n");
    printBits(a);

    int v = getBitAt(a, 3);
    printf("getBitAt(3)=%d\n", v);


    v = getBitAt(a, 10);
    printf("getBitAt(10)=%d\n", v);


    v = getBitAt(a, 20);
    printf("getBitAt(20)=%d\n", v);

    v = getBitAt(a, 5);
    printf("getBitAt(5)=%d\n", v);
}
Beispiel #2
0
// It prints the bits in bitmap as 0s and 1s.
void printBits(unsigned int bitmap)
{
	// Add your code here
	int i;
	for(i = 31; i >= 0; i--) {
		printf("%d",getBitAt(bitmap,i));
	}
	printf("\n");
	int j = 1;
	for(i = 0; i < 32; i++) {
		printf("%d",j--);
		if(j == -1) j = 9;
	}	
	printf("\n");
}
void branch(int* ir, ARM* arm) {
	int offset = getBitsAt(ir, BR_OFFSET_START, BR_OFFSET_LENGTH);
	//Shifted left two bits
	offset <<= 2;
	//Sign extend offset
	int sign_bit = getBitAt(ir, BR_OFFSET_STOP);

	if (sign_bit) {
		offset |= 0xFF000000;
	}

	//Simulating the pipeline behaviour
	arm->pc += 1;
	//Convertion to word address
	arm->pc += (offset / BYTES_IN_WORD);
}
int main(int argc, char** argv) {
    int numbersCount;
    scanf("%d%*c", &numbersCount);
    unsigned long numbers[numbersCount];
    int i;
    for (i = 0; i < numbersCount; i++) {
        scanf("%lu%*c", &numbers[i]);
    }
    int playerRow = 0;
    int playerCol = 0;
    int currentRow = 0;
    int currentCol = 0;
    char * line = readLine();
    while (strcmp(line, "end") != 0) {
        if (strcmp(line, "up") == 0) {
            currentRow--;
            if (currentRow < 0) {
                currentRow = numbersCount - 1;
            }
            if (getBitAt(numbers, currentRow, currentCol) == 1) {
                printf("GAME OVER. Stepped a mine at %d %d\n", currentRow, currentCol);
                break;
            }
            else{
                move(numbers,currentRow,currentCol,playerRow,playerCol);
                playerRow = currentRow;
                playerCol = currentCol;
            }
        }
        else if(strcmp(line,"down") == 0)
        {
            currentRow++;
            currentRow = currentRow % numbersCount;
            if (getBitAt(numbers, currentRow, currentCol) == 1) {
                printf("GAME OVER. Stepped a mine at %d %d\n", currentRow, currentCol);
                break;
            }
            else{
                move(numbers,currentRow,currentCol,playerRow,playerCol);
                playerRow = currentRow;
                playerCol = currentCol;
            }
        }
        else if(strcmp(line,"left") == 0)
        {
            currentCol++;
            currentCol = currentCol % 32;
            if (getBitAt(numbers, currentRow, currentCol) == 1) {
                printf("GAME OVER. Stepped a mine at %d %d\n", currentRow, currentCol);
                break;
            }
            else{
                move(numbers,currentRow,currentCol,playerRow,playerCol);
                playerRow = currentRow;
                playerCol = currentCol;
            }
        }
        else if(strcmp(line,"right") == 0)
        {
            currentCol--;
            if(currentCol < 0)
            {
                currentCol = 31;
            }
            if (getBitAt(numbers, currentRow, currentCol) == 1) {
                printf("GAME OVER. Stepped a mine at %d %d\n", currentRow, currentCol);
                break;
            }
            else{
                move(numbers,currentRow,currentCol,playerRow,playerCol);
                playerRow = currentRow;
                playerCol = currentCol;
            }
        }
        free(line);
        line = readLine();
    }
    free(line);
    
    for(i=0;i<numbersCount;i++)
    {
        printf("%lu\n",numbers[i]);
    }
    return (EXIT_SUCCESS);
}
void calcAddress(int* r, int isPreIndex, char* arg3, char* arg4, char* arg5) {
	/* POST-INDEXING
	 * It is of the following two forms
	 * [Rn], #expression
	 * [Rn], {+/-}Rm{, <shift>}
	 *
	 * PRE-INDEXING
	 * It is of the following two forms
	 * Note the bracket close in the second one
	 * [Rn, #expression]
	 * [Rn, {+/-}Rm{, <shift>}]
	*/
	if(arg3[0] == '#') {
		//#expression] form (']' depends on indexing)
		if(isPreIndex) isInClear(']', arg3, MAX_ARG_SIZE);

		int offset = strToInt(arg3, MAX_ARG_SIZE);

		//offset must fit in 12bits as signed number
		if(offset > 0x7ff || offset < ((int)(0xfffff800))) {
			printf("ERROR: Cannot fit offset value \"%d\" to 12 bits in ldr\n", offset);
			exit(EXIT_FAILURE);
		}

		//If it does continue
		int isNeg = getBitAt(&offset, 31);

		//Set U Flag, add or subtract offset
		setBitAt(r, 23, isNeg == 0);

		if(isNeg) {
			offset = -(offset);
		}

		//Set the offset
		setBitsAt(r, 0, &offset, 0, 11);

	} else {
		//{+/-}Rm{, <shift>}] ']' depends on indexing form
		//SET IFLAG as offset is a shifted register
		//NOTE OPPOSITE CASE IN SINGLE DATA TRANSFER
		if(getBitAt(r, 26)) setBitAt(r, 25, 1);

		//Is the bracket closing? If yes, then there is no shift
		//Rm] or Rm, <shift>] for pre-index
		//Rm, <shift>

		//Clear any closing brackets from Rm] in pre-indexing
		if(isPreIndex) isInClear(']', arg3, MAX_ARG_SIZE);

		//Check positive or negative and get Rm value
		int Rm;

		if(arg3[0] == '-') {
			//Set U Flag to subtract offset
			setBitAt(r, 23, 0); //subtract
			//Omit '-' when parsing
			Rm = strToInt(arg3 + 1, MAX_ARG_SIZE - 1);
		} else {
			//By default the UFLAG is set to add
			Rm = strToInt(arg3, MAX_ARG_SIZE);
		}

		//Set Rm
		setBitsAt(r, 0, &Rm, 0, 4);

		if(arg4[0] != '\0') {
			/*THERE IS SHIFTING
			* It can be 1 of the following
			* <shift> <register>]
			* <shift> <#expression>]
			* ']' depend on indexing type
			*/

			//Get and set the shift code
			int shiftCode = getShiftCode(arg4);
			setBitsAt(r, 5, &shiftCode, 0, 2);

			//get rid of closing brackets if pre-indexing
			if(isPreIndex) isInClear(']', arg5, MAX_ARG_SIZE);

			//Check the second argument to shift
			if(arg5[0] == '#') {
				//<#expression> form
				int shift_amount = strToInt(arg5, MAX_ARG_SIZE);

				//Shift amount must fit into 5 bits
				if(shift_amount > 0x1f) {
					printf("ERROR: Too large immediate shift value: %d\n", shift_amount);
					exit(EXIT_FAILURE);
				}

				//Set the shift amount
				setBitsAt(r, 7, &shift_amount, 0, 5);
			} else {
				//<register> form
				//Set register shift flag
				setBitAt(r, 4, 1);

				//Get and set Rs
				int Rs = strToInt(arg5, MAX_ARG_SIZE);
				setBitsAt(r, 8, &Rs, 0, 4);
			}
		}
	} //endof if(arg3[0] == '#') else
}