Electronic – Creating an ASM file that will return the characters located in 8 consecutive registers

assemblymicrocontrollerpic

I have to create an ASM file for the PIC18F452 that does the following:

(a) define the label MapName as the first of 8 consecutive registers containing a null-terminated string of not more than 7 characters.
(b) access an 8-bit unsigned integer variable called MapIndex which was declared in a C file.
(c) define an ASM function getMapChar which can be called from C using the function prototype char getMapChar(void). The function should return the appropriate character when the value of MapIndex is <= 7 or the value 255 if MapIndex is > 7.
(d) make the labels MapName and getMapChar accessible to an external C file.

My code so far is shown below:

; Configuration word : WDT off, power-up timer on, code protect off, RC oscillator
list = p18f452
            MapName         equ             0x20
            MapName1        equ             0x21
            MapName2        equ             0x22
            MapName3        equ             0x23
            MapName4        equ             0x24
            MapName5        equ             0x25
            MapName6        equ             0x26
            MapName7        equ             0x27
            CurrentChar     equ             0x28

extern      MapIndex
                            org             0x00
                            goto            getMapChar

getMapChar                  movlw           0x00
                            movwf           MapName7

GLOBAL      getMapChar
GLOBAL      MapName

END

I have already done parts (a), (b) and (d) but I am having some problems with writing the code that moves through each of the consecutive registers automatically using the value of MapIndex. Could anyone help me please? It would be much appreciated.

Best Answer

Step back and think about the whole problem. No, you haven't done A, B, and D yet. There are so many things wrong here that demonstrate such major misunderstanding that I don't know how to begin explaining. All I can do is enumerate some problems:

  1. You haven't defined any variables at all. All EQU does is create a symbol of a particular value. It has nothing to do with allocating variables. Only the RES directive does that.

  2. You seem to be writing this in absolute mode. You didn't say what compiler you are using, but most likely it only works in relocatable mode.

  3. You don't need a label for every byte in the buffer. The MapNamex names are clutter that gives the impression these names will be used separately. In reality, you'll never use them because you will be indexing into the buffer at run time.

  4. This code is written like it's the single stand-alone module of a project. Clearly this can't be if there is at least one C routine in the project.

  5. The C runtime library needs to be initialized at startup, but you are preventing that by trying to own the reset vector.

  6. You are running your routine directly from the reset vector. That makes no sense since it is intended to be called from C. You need to create a subroutine.

  7. There is no place for the code to go two instructions after getMapChar. Your code just ends, so the machine will execute whatever happens to be in memory after the last instruction in this module. That makes no sense.

You need to step back and try to understand how compiled code gets run on the machine, what the subroutine linkage conventions are, and then how to implement them in assembler to make a C function that returns a byte. Try to understand the role of compiler, assembler, and linker and how they interact. You're nowhere near ready for this problem yet.