Products > Programming

Reworking a decision tree to eliminate the dreaded GOTO

(1/3) > >>

metertech58761:
So I have some 6801 assembly code that I wanted to 'port' to C or similar modern language.

I finally realized the approach I need to take: Instead of translating the assembly language opcode by opcode, just write out in plain English what each section of code is supposed to do.
I can then use that English description as the basis for the code to be generated.

I've got a few sections redone, but I'm stuck on the next section which has a decision tree that I'm struggling with.

It should be possible to rewrite the GOTO-infested decision tree as a series of IF.. THEN.. ELSE.. but I'm not seeing it. Can anyone give me some suggestions?

The overall idea is that this code sends commands to a device being tested, monitors the relay contacts to ensure they are opening and closing, then reports the results.


--- Code: ---
; Test 72 - DCT related / strobe relays
Clear screen
Display text line 1: "Tst 72: Strobe rlys"

Load 2-way message template (msg2way)
msgTbl[6] = 0x60 // arm relays
Send message (msgCmd)
Delay: 125mS

Load 2-way message template (msg2way)
msgTbl[6] = 0x41 // open relays
Send message (msgCmd)
Delay: 125mS

rlyDTmp1 = rlyDStatus

Delay: 500mS

rlyATmp1 = rlyAStatus
rlyDTmp2 = rlyDStatus

Load 2-way message template (msg2way)
msgTbl[6] = 0x42 // close relays
Send message (msgCmd)
Delay: 125mS

rlyATmp2 = rlyAStatus

Delay: 500mS

\\ Evaluate Relay C (located in Relay A slot)

temp7 = 0

if (rlyAStatus == 0), goto test72_2

test72_1

Display text line 2: "Relay C: FAIL"

temp7 = (temp7 | 1)

goto test72_3

test72_2

if (rlyATmp2 == 0), goto test72_1

Display text line 2: "Relay C: PASS"

\\ Now evaluate Relay D

test72_3

if (rlyDTmp1 == 0), goto test72_4

if ((rlyATmp1 != 0) || (rlyDTmp2 != 0)), goto test72_4

Display text line 3: "Relay D: PASS"

goto test72_6

test72_4

Display text line 3: "Relay D: FAIL"

temp7 = (temp7 | 2)

\\ And the overall results?

test72_6

if (temp7 == 0), Display text line 4: "Test: PASS", test LED green
else display text line 4: "Test: FAIL", test LED red
Delay: 1 second
Get keypress: Repeat test or continue to next

--- End code ---

jpanhalt:
Doesn't "if, then, else" logic in Assembly often result in branch, goto, or call?  I don't do C, but I have read that when C is disassembleddecompiled, there are lots of goto's.  What's the problem with a goto/branch?  (Calls are a different story.)

Take this example in PIC Assembly:

--- Code: ---btfss     PORTx,n
do something
do something else

--- End code ---
The obvious problem is that "do something else" gets executed regardless of the btfss result (if false), unless there is a branch.  Now, if the "do something" is simple, like move a constant to the working register,  the branch can be avoided by something like this:

--- Code: ---move something to WREG
btfss     PORTx,n
move something else to WREG
rest of code

--- End code ---

John

m k:

--- Quote from: metertech58761 on February 10, 2024, 03:49:47 pm ---It should be possible to rewrite the GOTO-infested decision tree as a series of IF.. THEN.. ELSE.. but I'm not seeing it. Can anyone give me some suggestions?

--- End quote ---

if
then
else if
then

switch/case is another.
Don't forget to break out.

audiotubes:
,

audiotubes:
Right, there is no problem except that an academic who never delivered anything worthwhile in his life said they were bad.

That said, there are idiomatic ways to code in various languages. Not that C is an example of anything except how complete crap given away for free can take over the world, but in C goto is not idiomatic.

Everything has its time and place. For error handling, goto even in C is often the best way to deal with things. But, you can write bad code in any language and goto is not intrisincally bad.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod