Write a short assembly subroutine to count the number of one

Write a short assembly subroutine to count the number of ones in the lower 4 bits of a register (TEMP 1). Test each bit one at a time. If the value of the bit is \'1\' Increment a register (NUM_ONES). If the value of the bit is \'0\', move on to the next bit in the register. Exit the subroutine when the lower 4 bits have been tested. Write the assembly instructions to update all 13-bits of the program counter. The first entry in the lookup table below.; Table - simple table-; This contains a simple lookup table TABLE DT \"ABCD\" DT 0 times 00 END|

Solution

In this subroutine called count_ones, we use the instruction btfsc (syntax: btfsc reg, b). This instruction checks if the bit \'b\' of the register \'reg\' is 0. If it is zero, then the succeeding instruction is not executed (the control skips the succeeding instruction and executes the next instruction) and an \'nop\' (no operation) is executed instead. Otherwise, the instruction after btfsc is executed. Hence, whenever the bit is 1, the next instruction is executed and not skipped. We can utilize this instruction by using the Increment operator to increase the count of the variable NUM_ONES using \'incf\'.

The bit to be checked starts from 0th bit to the 3rd bit. Overall, the operation of checking the bits has to be done 4 times.

count_ones

COUNT equ 04h;

BIT equ 00h;

NUM_ONES equ 00h;

again: btfsc TEMP_1,BIT;

incf NUM_ONES;

incf BIT;

decfsz COUNT;

goto again;

return;

 Write a short assembly subroutine to count the number of ones in the lower 4 bits of a register (TEMP 1). Test each bit one at a time. If the value of the bit

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site