1

I was reading a chapter related to I/O using the MC68HC11; this book showed a suggested exercise (not really a hard one) but i was not able to solve it by using assembly:

image

I've been thinking and i can do it by using some basic logic (i do program in C and C++ ) but i got stuck while trying to do it in assembly.

Logic goes like this:

Loop:

value = ReadSensorValue()

if (value condition 1) // do action else if (value condition 2) // do something end if

go to Loop

Can help me to solve it but using real instructions in assembly?



EDIT:

ERROR

ERROR2

JustToKnow
  • 785
  • 6
  • 23
  • 30 years ago I wrote several programs in 68HC11 assembler so could have helped you more. Alas it's all forgotten now. But if you want to progress with this question I think you need to expand your pseudocode a lot more, into a full C listing that performs *all* the tasks requested. Then, conversion to assembler should be more apparent. – quilkin Jun 28 '20 at 15:56
  • The trick is to turn all your if/else into pieces of "if X goto Y" because that's the only conditional logic you can do, more or less. And don't forget that you probably need a "goto Z" at the end of the conditional block to jump over what would be your "else". – CherryDT Jun 28 '20 at 16:58
  • `proc` is used by `ASM11` (and other assemblers) to indicate the beginning of a procedure/function so that you can use procedure scoped labels, i.e., those having `@@` inside them. So, remove `proc` and replace `@@` to maybe `_` in labels (whatever your assembler does not complain about), or just use ASM11 to make your life easier. – tonypdmtr Jun 30 '20 at 11:24

1 Answers1

1

Here's one way to do it (using ASM11). I understand you're trying to learn but you don't show any effort. (This is the last one I'm doing without seeing some effort from you first.)

;*******************************************************************************
; MCU specific
;*******************************************************************************

REGS                equ       $1000               ;register base
PORTB               equ       REGS+$04            ;port B (output only)
PORTC               equ       REGS+$03            ;port C
STACKTOP            equ       $01FF               ;Top of Stack
ROM                 equ       $F800               ;beginning of ROM
Vreset              equ       $FFFE               ;reset vector

;*******************************************************************************
; Application specific
;*******************************************************************************

TEMPERATURE         equ       PORTC               ;temperature is available here

CONTROL             equ       PORTB
HEATER.             equ       1                   ;bit that controls heater
COMPRESSOR.         equ       2                   ;bit that controls cooler

MIN_TEMP            equ       20                  ;min allowed temp in C
MAX_TEMP            equ       22                  ;max allowed temp in C

;*******************************************************************************
                    org       ROM
;*******************************************************************************

;*******************************************************************************
; Purpose: Get temperature as Centigrade degrees
; Input  : None
; Output : A = temperature in whole degrees (fractional part discarded)
; Note(s): Formula is TEMPERATURE*5/10 using integer arithmetic
;        : Simplifies to TEMPERATURE/2

GetTemperature      proc
                    ldaa      TEMPERATURE         ;A = temperature
                    lsra                          ;A = temperature/2
                    adca      #0                  ;(optional) round up
                    rts

;*******************************************************************************

CoolIt              proc
                    pshx
                    ldx       #CONTROL
                    bclr      ,x,HEATER.
                    bset      ,x,COMPRESSOR.
                    pulx
                    rts

;*******************************************************************************

HeatIt              proc
                    pshx
                    ldx       #CONTROL
                    bclr      ,x,COMPRESSOR.
                    bset      ,x,HEATER.
                    pulx
                    rts

;*******************************************************************************

AllOff              proc
                    pshx
                    ldx       #CONTROL
                    bclr      ,x,COMPRESSOR.|HEATER.
                    pulx
                    rts

;*******************************************************************************

Start               proc
                    lds       #STACKTOP
                    bsr       AllOff

Loop@@              bsr       GetTemperature      ;A = temperature in degrees

                    cmpa      #MIN_TEMP           ;if below minimum
                    blo       HeatIt@@            ; go heat it up

                    cmpa      #MAX_TEMP           ;if above maximum
                    bhi       CoolIt@@            ; go cool it down

                    bsr       AllOff              ;if within range turn all off
                    bra       Loop@@              ;go check temperature again

CoolIt@@            bsr       CoolIt
                    bra       Loop@@              ;go check temperature again

HeatIt@@            bsr       HeatIt
                    bra       Loop@@              ;go check temperature again

;*******************************************************************************
                    org       Vreset
                    dw        Start
;*******************************************************************************
tonypdmtr
  • 3,037
  • 2
  • 17
  • 29
  • I have a problem translating from C/C++/JAVA to assembly. Don't know why but i try a lot :/, Probably my code is sh*** and don't want to make a laugh out of it – JustToKnow Jun 28 '20 at 20:24
  • I edited to turn off both heater and cooler when the temperature is within range. This is more energy efficient and will avoid overshooting the target either up or down. – tonypdmtr Jun 28 '20 at 20:33
  • 4
    I don't think any reasonable person would laugh at a beginner's code (but you never know). However, that shouldn't bother you; shame on them. You can't learn if you don't actually try it out yourself. And you can't expect to not make mistakes while learning. Plus, it's easier for someone else to see in your coding effort where your misunderstanding is and try to explain that for you. – tonypdmtr Jun 28 '20 at 20:39
  • 2
    I'm checking it out; i know how to program in different languages, i found this MC and it's pretty simple to understand the architecture (i have provided different examples in C in my last questions) but i'm not an expert while translating into assembly. What's your trick?. It is easy to me to get lost while using assembly code :( – JustToKnow Jun 28 '20 at 20:39
  • 3
    My 'trick' is I think in assembly language directly, not C :) – tonypdmtr Jun 28 '20 at 20:41
  • Tony, i know you're right; when you write code it is simple to me to follow, that's why i think you are a great teacher; i found a manual on the internet and i've been reading it and it goes well but when they show "suggested" exercises my mind gets blind, i swear: i write lines and lines of code but always i'm missing something and it gets confusing to me. – JustToKnow Jun 28 '20 at 20:46
  • 1
    Tony, i have a few errors related to the syntax itself. Have you got any idea how to solve them? – JustToKnow Jun 30 '20 at 03:17
  • @Student_new: There's a whole site, https://codereview.stackexchange.com/, where beginners can post their code (which is often really clunky because they don't know good idiomatic ways to do things in whatever language) and get feedback on how to improve the code. You need to have a working, tested version first, that you *think* is bug free (sometimes that turns out not to be the case, but at least has to work for all the tests you've thought of). – Peter Cordes Jun 30 '20 at 03:25
  • 2
    @Student_new: And yes, code written by experts is often easy to follow, if it's doing something that's straightforward for the language, and seems obvious in hindsight. That's usually intentional, writing code for humans to read is a good practice. Designing it that way comes with experience in seeing the big picture of the problem and breaking it down into parts in a way that works well for the chosen language. Also sticking to good, consistent style and formatting. – Peter Cordes Jun 30 '20 at 03:26
  • @tonypdmtr You're the best teacher i've ever had!. Tony, i jumped to IRQ and interrupts; https://stackoverflow.com/questions/62650218/interrupts-iqr-and-xirq-assembly?noredirect=1#comment110795834_62650218 i put some code but i'm a little confused on that. – JustToKnow Jun 30 '20 at 13:33
  • @Student_new That's premature IMO. You still lack basics before you learn interrupts. Ironically, interrupts are exactly that. Jumping to a different topic unexpectedly. (Anyway, I'll have a look at your new question when I get some time.) – tonypdmtr Jun 30 '20 at 15:43
  • You made me laugh, Tony :D. Yeah, i found it very interesting and i want to know your thoughts (believe me, i'm learning a lot). Yes, don't miss it!:) – JustToKnow Jun 30 '20 at 15:48
  • @tonypdmtr, i have this output: https://imgur.com/a/93itHWN does it make any sense to you? – JustToKnow Jul 01 '20 at 04:53
  • What are you trying to do? This output looks like disassembly from address $0001 which is not program memory so you see 'random' instructions. – tonypdmtr Jul 01 '20 at 08:07