Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Branches and Binary - Need to learn ideal answer for mock exam question

Status
Not open for further replies.

Spadez

New Member
Hi,

I have my exam on Tuesday and there is one question in the exam that I am really unsure about. The trouble is I cant learn the model way to answer the question, because im not even really able to answer it yet. Attached is the exact question.

q7.png

So, im guessing the way to answer the first question is to do something like this:

================================

check_value equ 0x20

movlw .43
movwf check_value

<how do I get portb value into working?>

subwf check_value,w

btfsc STATUS, C ; If c bit is set, then there is a carry, then it wont be skipped
goto grtr_than

================================

Can someone please help me finished this question off. I dont know how to check a binary value on portB.

================================
================================

b) I literally have no idea about this question but here is my stab at it.

1) I's nibbles are swapped, and the result is stored in F. Therefore f is the file which is changed as the result. Its original value was A (.10) which is 0000 1010, and its binary value will now be 1010 0000.

2) The working register is affected. i is still 10, so it is 3+10, and the result 13 is stored in w (0000 1101).

3) 13 is still in the working register, so k is given the value (0000 1101)

Is this the kind of thing I should be doing?

================================
================================

Im stressing out about these, especially part one, any any help or advice would be much appreciated.

James
 
Last edited:
Hi Nigel, I will have a look at that site, although from first look it seems to be the least user friendly site ive ever seen.

Can you or anyone else help me with this
"Can someone please help me finished this question off. I dont know how to check a binary value on portB"

I havent been able to find the answer to this yet myself.
 
Hi Nigel, I will have a look at that site, although from first look it seems to be the least user friendly site ive ever seen.

Can you or anyone else help me with this
"Can someone please help me finished this question off. I dont know how to check a binary value on portB"

I havent been able to find the answer to this yet myself.

hi,
As its Homework we only try to give hints..

Consider that you read PORTB into Wreg
You are testing for a value greater than 'x'

What would the C and/or Z flags be set to if you subtracted 'X' from Wreg.???

There are number of different ways to check equality, gtr_than, less_than

EDIT:
Look at this image.
 

Attachments

  • AAesp02.gif
    AAesp02.gif
    21.7 KB · Views: 300
Last edited:
Hi Eric,

Its not really homework, as it is just revision for my exam, I just need to understand how to do this stuff before I get examed.

I understand that the carry flag is set if the value is greater than, and the zero flag is set if they are the same (after arithemtic).

I just dont understand how I get the binary value of PortB.

What is it like

Movf PORTB, W

Would this get me the binary value of PORTB into the working register?
 
Hi Eric,

Its not really homework, as it is just revision for my exam, I just need to understand how to do this stuff before I get examed.

I understand that the carry flag is set if the value is greater than, and the zero flag is set if they are the same (after arithemtic).

I just dont understand how I get the binary value of PortB.

What is it like

Movf PORTB, W

Would this get me the binary value of PORTB into the working register?

hi,
Yes, that would load Wreg with PORTB
Look at my EDIT image
 
Excellent, thank you for the help. So this assembly code should see if the binary value of PORTB is greater than 42? Is this the best way of doing it?

Code:
check_value equ 0x20
movlw .43 
movwf check_value
movf PORTB, W
subwf check_value,w
btfsc STATUS, C 
goto grtr_than
 
Last edited:
hi,
The best way.?
Not being a smart ars*, do mean the one that takes the least lines of code, the fastest when run or the most elegant,,, it depends upon who you ask.

Do you have MPLAB IDE assembler on your PC.?
An easy way to test alternatives is write a 'noddy' type short program and step thru it, observing the registers and flags.

EDIT:
Recheck the sense of the C flag in your code fragment
 
Last edited:
I mean to design the one which takes the least amount of lines to write.

I think actually it should be movlw .42 not .43 looking back, because the zero flag isnt being checked by my code, so a reading of .43 which is bigger than .42 would not trigger the carry flag.

Ill try to have a play about in MPlab.
 
Hi Nigel, I will have a look at that site, although from first look it seems to be the least user friendly site ive ever seen.

Can you or anyone else help me with this
"Can someone please help me finished this question off. I dont know how to check a binary value on portB"

I havent been able to find the answer to this yet myself.

It's not that friendly, but all the information is there:
 
I mean to design the one which takes the least amount of lines to write.

I think actually it should be movlw .42 not .43 looking back, because the zero flag isnt being checked by my code, so a reading of .43 which is bigger than .42 would not trigger the carry flag.
Ill try to have a play about in MPlab.


hi
Did you see my EDIT regarding the 'C' flag in your code.

Code:
    movlw .43
    subwf PORTB,W
    btfsc STATUS, C
    goto gtr_than
 
So with this code:

Its doing PORTB - 43 and then checking if there is a negative result.

If port b is 42 then: 42-43 would be still a carry, but 42 isnt a larger result than 42, it is the same. Does this mean I need to add in a zero flag check, or should I make the code this:

movlw .42
subwf PORTB,W
btfsc STATUS, C
goto gtr_than

Now if PORTB is 42, then carry isnt triggered, but if PORTB is 43 it is triggered.
 
Last edited:
So with this code:

Its doing PORTB - 43 and then checking if there is a negative result.

If port b is 42 then: 42-43 would be still a carry, but 42 isnt a larger result than 42, it is the same. Does this mean I need to add in a zero flag check, or should I make the code this:

movlw .42
subwf PORTB,W
btfsc STATUS, C
goto gtr_than

Now if PORTB is 42, then carry isnt triggered, but if PORTB is 43 it is triggered.

hi,
Using my code,
First run 42d, 0x2A on PORTB
not gtr than

Second run 43d, 0x2B on PORTB
gtr than

First 4 lines run1, second 4 lines run2, note the flags
 

Attachments

  • AAesp03.gif
    AAesp03.gif
    12.5 KB · Views: 279
So, using your code:

When .42 is fed in, it says not greater than.
When .43 is fed in, it says greater than.

If port b is 42 then: 42-43 equals -1, so there would be still a carry should there not?
 
So, using your code:

When .42 is fed in, it says not greater than.
When .43 is fed in, it says greater than.

If port b is 42 then: 42-43 equals -1, so there would be still a carry should there not?

hi,
Your question paper asks only when greater than 42, it does not ask for AND IF =42...

Did you follow the code in the image I posted for 42 and 43 on PORTB.?

Have you run that code in MPLAB.??
 
I did, but I just dont understand why it is happening.

If the equation is 42-43, then the result is -1. This means the carry should be set. If the carry flag is set, then it wont skip gtr_than, resulting in it going to greater than. But 42 is not greater than 42.
 
I did, but I just dont understand why it is happening.

If the equation is 42-43, then the result is -1. This means the carry should be set. If the carry flag is set, then it wont skip gtr_than, resulting in it going to greater than. But 42 is not greater than 42.

hi,
Look at this in conjunction with the PIC's Instruction set.

Using
MOVLW .43
SUBWF PORTB,W
BTFSC STATUS,C
GOTO GTR_THAN
else

PORTB=.41 - Wreg = .43
41-43 Result =-2 which is negative, so C is not set

PORTB=.42 - Wreg = .43
42-43 Result = -1 which is negative, so C is not set

PORTB=.43 - Wreg = .43
43 - 43 Result = 0 which is NOT negative, so C is set

PORTB=.44 - Wreg = .43
44 -43 Result =1 which is NOT negative, so C is set
 
So just to refresh.

The carry bit is normally set. When the result of an equation is too high or too low for the pic to store the number, the carry flag gets cleared.

In regards to the zero flag. Is it right that it is high when the result is zero, and clear otherwise?
 
So just to refresh.

The carry bit is normally set. When the result of an equation is too high or too low for the pic to store the number, the carry flag gets cleared.

In regards to the zero flag. Is it right that it is high when the result is zero, and clear otherwise?

Dont think in terms of zero, think of it as a negative result or is it a positive result , after the subtraction.

Its like basic maths subtraction, eg: 16 -9 >> 6-9 = 7 CARRY 1 , or 9 -9 =0 NO CARRY , 11-9 = 2 NO CARRY.

So you test for BTFSC or BTFSS as required.

OK.?
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top