Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

coder123

Newbie level 4
Joined
Jun 11, 2015
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
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

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,349
Reputation
4,692
Reaction score
2,275
Trophy points
1,413
Location
Southwest, USA
Activity points
62,386
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
Joined
Jun 11, 2015
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
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
Joined
Apr 17, 2014
Messages
20,531
Helped
4,463
Reputation
8,935
Reaction score
4,496
Trophy points
1,393
Activity points
135,750
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
Joined
Jun 11, 2015
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
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
Joined
Jul 4, 2009
Messages
15,154
Helped
4,944
Reputation
9,909
Reaction score
4,755
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
128,703
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top