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.

Turning ASM to HEX - Mock Exam Question

Status
Not open for further replies.

Spadez

New Member
Hi,

I have a mock exam question which talks about creating the correct hex code from assembly code. Attached is the exact question.

PIC16F628A datasheet: https://www.electro-tech-online.com/custompdfs/2011/04/40044f.pdf

So, to do this should I find out the binary for the command from table15-2, then convert this binary to hex?

Question one says to convert the following command:
BTFSS STATUS, TOUT

This has the binary:
01 11BB BFFF FFFF

Am I right up to this point?

Im not sure how to find out bit status is in the datasheet. I can see that TOUT is equated to 0x04H at the top, which is in binary:

0000 0000 0000 0100

This goes in the file section of the hex to give me:

01 11BB B000 0100

How is all this looking?

James
 

Attachments

  • Untitled8.png
    Untitled8.png
    3.2 MB · Views: 318
Hi Ian,

It would still help me to know where it was getting the hex step by step, rather than just verify my answer is right. I have MPlab, but is there a way to assemble just this line:

BTFSS STATUS, TOUT

Rather than have to add in all the headers and stuff, which will create extra HEX files, making it difficult to understand which hex pertains to this line.
 
The very first ASM compiler I got from microchip, had a card inserted into the manual, this card had all the mnemonics with the hex instruction next to it, I think its now in the data sheet. Hold on! That's the same as you have already.

The compiler won't generate a lst file until the code syntax is ok, You need to generate a asm listing that will compile, then add the line you need each time you need to decode.

You can't do just one statement.

Ian
 
It's bit 'b' in register 'f', so looking at your first statement it looks like you've got it a bit backwards. STATUS is the register, 'f', TOUT is which bit we want to test so it wouldn't it be 0111 for the BTFSS, then 010 for the bit and finally 000 0011. altogether 01 1101 0000 0011 = 0x1d03.
 
Ok, thank you for clearing that up, but can I query you on two things.

You say the b is "TOUT", which is 0x04H, so shouldnt b be "100"?

01 11BB BFFF FFFF
01 11 10 0 000 0011

Secondly, if status isnt defined in the header, how do we find out its hex, is there a default hex listed in the datasheet?
 
Last edited:
GOTO GOODNIGHT

10 1KKK KKKK KKKK

Based on the picture, goodnight has location 0015, so that makes the binary:

10 1000 0000 1110

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

RETLW 0

RETLW = 11 01XX XXXX XXXX

So for RETLW 0 is it just: 11 0100 0000 0000

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

MOVLW B'11111111'

MOVLW = 11 00XX KKKK KKKK

So its 11 00XX 1111 1111

But, what is the XX?
 
Last edited:
X is just a "don't care" value according to the datasheet, so can be a 1 or a 0 and makes no difference (tho i reckon leaving them as zeroes is probably easier/most people's personal preference)...
 
Thank you for the reply. There are two questions I dont understand on this one, attached is the question sheet.

**broken link removed**

Firstly, the question:
"goto $-1"

From my understanding, $-1 means go up a line, so that would be btfss PIR1,RCIF. Does that mean I should use a GOTO with hex address 003B for that line?

Secondly, the line "movlw 'A'". I might be being slow here, but isnt movlw for moving a literal into the working register, not a letter. How would I turn this intoi a hex?
 
Firstly, the question:
"goto $-1"

From my understanding, $-1 means go up a line, so that would be btfss PIR1,RCIF. Does that mean I should use a GOTO with hex address 003B for that line?
Correct. The assembler uses the current location ($) and subtracts 1 (well, adds -1) to get the target address, which would be the instruction preceding the GOTO. So you would treat it as GOTO 0x3B.
Secondly, the line "movlw 'A'". I might be being slow here, but isnt movlw for moving a literal into the working register, not a letter. How would I turn this intoi a hex?
A character inside single quotes is treated by the assembler as an ASCII character, so take the ASCII value of the uppercase A and use that (in hex) as the literal to move into W.
 
Thank you for the reply. That has sorted me out....nearly!

Part XIII in the last question says MOVLW OK

The thing is, "OK" isnt in quotes, so it isnt ASCII, and there is no goto called OK, so what is meant by this one?
 
The thing is, "OK" isnt in quotes, so it isnt ASCII, and there is no goto called OK, so what is meant by this one?

It would appear that they gave you code that won't assemble so it's impossible to answer that part.

Mike.
 
I imagine the OK was meant to be defined to some value the same way 'WRONG' was but it looks like they forgot to mention what value exactly...
 
Sorry to keep flogging a dead horse but I have another question. If they ask me to turn the following into machine code (HEX)

CLRF PORTB

I know how to get the binary for CLRF, but how do I find out the binary for PORTB?

In the opcode, is B the bit? So for example, b = 7 for the following:

BSF MY_REG, 7

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

Sorry about all the questions. Thank you for any help you can give.

James
 
Thank you for all the help. My exam is 9am tomorrow morning so im leaving it a bit late, but how would I tackle this instruction:


ACCA EQU 0x28

ADDWF ACCA+1, F

Is that basically just making ACCA EQU 0x29?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top