# [PIC]Compare two 4 bit numbers using PIC16f676 in Assembly Language

Status
Not open for further replies.

#### coder123

##### Newbie level 4
I nedd help with the following problem statement, Thank you

Suppose there are two 4 bit numbers X and Y ( 0000,0001,....) .... if X>Y, Z=1 else Z=0

How do i code this is PIC16f676

#### bigdogguru

What programming language are you using to code?

For C, you could use the ternary operator:

Code:
mask = 0x0F;

Z = (X & mask) > (Y & mask) ? 1 : 0;

A bitwise mask is used to ensure the first four bits of each value are cleared.

Example Code for PC:

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main(void)
{
unsigned char X = 5;
unsigned char Y = 3;
unsigned char Z;
unsigned char mask = 0x0F;

Z = (X & mask) > (Y & mask) ?  1 :  0;

if(Z)
{
printf("X is greater than Y\n");
}
else
{
printf("Y is greater or equal to X\n");
}

return 0;
}

BigDog

#### coder123

##### Newbie level 4
I need the code in assembly language.
I also need the code where you initialise the input and output ports .

Thanks a lot

#### KlausST

##### Super Moderator
Staff member
Hi,

I also need the code where you initialise the input and output ports .
you have to tell use where are the inputs and outputs.

Klaus

#### coder123

##### Newbie level 4
Just tell me the logic how will you compare two binary numbers in assembly language.
(without using and <, > or equal sign )
Binary comparison

If you can help with the assmebly language code that will be great

#### betwixt

##### Super Moderator
Staff member
Subtract one from the other:

movlw number1
movwf SomeLocation
movlw number2
subwf Somelocation,f

Then check the STATUS register. The Z flag will be set if the values are equal and if it isn't set, the C flag tells you which number is larger.

Brian.

Status
Not open for further replies.