Файл: Introduction to Microcontrollers. Architecture, Programming, and Interfacing of the Motorola 68HC12 (G.J. Lipovski, 1999).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 1421

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

9.3 Conditional Statements

269

unsigned char guc; int gsi; main(){ char Isc; unsigned int lui;

if(lsc == 0) guc = 0;

if(gsi < 5) { ... /* many instructions */ } if(Isc + guc) gsi = 0;

if(guc < 5) Isc = 0; else Isc = 9;

}

a.A C Program

4: main(){ char Isc; unsigned int lui;if(Isc == 0) guc = 0;

0000095B A6AD

LDAA

3,-SP

0000095D

2603

BNE

*+5

;abs = 0962

0000095F

790800

CLR

$0800

6:

if(gsi < 5) { /* many instructions */ lui =0; }

00000962

FC0801

LDD

$0801

00000965

8C0005

CPD

#5

00000968

2C04

BGE

*+6

;abs = 096E

0000096A

C7

CLRB

0000096B

87

CLRA

0000096C

6C81

STD

1,SP

7:if(Isc + guc)gsi = 0;

0000096E

A680

LDAA

0,SP

00000970

B704

SEX

A,D

00000972

B745

TFR

D,X

00000974

F60800

LDAB

$0800

00000977

87

CLRA

00000978

1AE6

LEAK

D,X

0000097A

044504

TBEQ

X,*+7 ,-abs = 0981

0000097D

C7

CLRB

0000097E

7C0801

STD

$0801

8:if(guc < 5) Isc = 0; else Isc = 9;

00000981

B60800

LDAA

$0800

00000984

8105

CMPA

#5

00000986

2404

BCC

*+6

;abs = 098C

00000988

6980

CLR

0,SP

0000098A

2004

BRA

*+6

;abs = 0990

0000098C

C609

LDAB

#9

0000098E

6B80

STAB

0,SP

9: }

00000990

1B83

LEAS

3,SP

00000992

3D

RTS

b. Assembly Language Generated by Part (a)

Figure 9.7. A Program with If-Then Expressions


270

Chapter 9 Implementation of C Procedures

unsigned char alpha, beta , gamma, delta, epsilon, zeta;

main ( ) {

if((alpha

< 5)&&(beta = = 0 ) )

gamma

=

0;

if((alpha

< 5)||(beta = = 0 ) )

gamma

=

0;

if(alpha

!= 0) beta = 10; else if(gamma — 0) delta++;

else if((epsilon != 0)&&(zeta==l))

beta=beta « 3; else beta=0;

}

a.A C Program

6:if((alpha < 5)&&(beta == 0)) gamma = 0;

0000095B

B60800

LDAA

$0800

0000095E

8105

CMPA

#5

00000960

2C08

BGE

*-t-10

;abs = 096A

00000962

B60801

LDAA

$0801

00000965

2603

BNE

*+5

;abs = 096A

00000967

790802

CLR

$0802

7:if((alpha < 5)|[(beta == 0)) gamma = 0;

0000096A

B60800

LDAA

$0800

0000096D

8105

CMPA

#5

0000096F

2D05

BLT

*+7

;abs= 0976

00000971

B60801

LDAA

$0801

00000974

2603

BNE

*+5

;abs = 0979

00000976

790802

CLR

$0802

8:if(alpha 1= 0) beta = 10;

00000979

B60800

LDAA

$0800

0000097C

2707

BEQ

*+9

;abs = 0985

0000097E

C60A

LDAB

#10

00000980

7B0801

STAB

$0801

00000983

201C

BRA

*+30

;abs = 09A1

9:else if(gamma == 0) delta++;

00000985

B60802

LDAA

$0802

00000988

2605

BNE

*+7

;abs =

098F

0000098A

720803

INC

$0803

0000098D

2012

BRA

*+20

;abs

=09A1

10:else if((epsilon!=0)&&(zeta==l)) beta=beta«3; else beta=0;

0000098F

B60804

LDAA

$0804

00000992

270A

BEQ

*+12

;abs = 099E

00000994

B60805

LDAA

$0805

00000997

042004

DBNE

A,*+7

;abs = 099E

0000099A

0764

BSR

*+102

;abs = OAOO

0000099C

2003

BRA

*+5

;abs = 09A1

0000099E

790801

CLR

$0801

b. Assembly Language Generated by Part (a)

Figure 9.8. Assembly Language for a Decision Tree


9.3 Conditional Statements

271

An operation result may be in accumulator B or D. It can be tested by the TBEQ or

TBNE instructions.In Figure 9.7, the statement if(lsc + guc) gsi = 0; similarly

encodes as

LDAA

0, SP

; get value of Isc

SEX

A,D

; upcast to 16 bits

TFR

Df X

; use X as accumulator

LDAB

$08 00

; get value of guc

CLRA

; upcast to 16 bits

LEAX

D, X

; add values

TBEQ

X, *+7

; check value of sum. If zero

CLRB

; generate a 16-bit zero (A is already clear)

STD

$0801

; store to clear variable gsi

In Figure 9.7, the else part of a conditional expression is easily implemented by a

BRA instruction. The statement

if (guc < 5) Isc = 0; else Isc = 9; encodes as

LDAA

$08 00

; get variable guc

CMPA

#5

; if greater than or equal to 5 as an unsigned number

BBS

*+6

; then skip over next instruction (this isBCC)

CLR

0, SP

; otherwise, if zero, clear variable Isc

BRA

*+6

; now skip over next two instructions

LDAB

#9

; write 9 into variable Isc

STAB

0,SP

A conditional expression can be a logical OR or a logical AND of tests described above. The logical OR test will check each case, from left to right, for a true result, and will execute the statement when it finds the first true result. The logical AND checks each case, from left to right, for a false, and bypasses the statement the first time it finds

a false test. If alpha,

beta,

and gamma are signed global char variables, the

statement in Figure 9.8 if ((alpha

< 5)

&&(beta = = 0 ) ) gamma = 0; encodes as

LDAA $0800

; get variable alpha

CMPA

#5

; if less than 5 as a signed number

BGE

*+10

; then skip to CLR instruction

LDAA

$0801

; if beta is nonzero

BNE

*+5

; then skip over next instruction

CLR

$0802

;if you get here, clear variable gamma

and if ((alpha < 5) | |

(beta

= = 0 ) )

gamma = 0; is encodedas

LDAA $0800

; get variable alpha

CMPA

#5

; if less than 5 as a signed number

BLT

*+7

; then skip to CLR instruction

LDAA

$08 01

; if beta is nonzero

BNE

*+5

; then skip over next instruction

CLR

$0802

; if you get here, clear variable gamma.

As seen in the previous examples, the ANDing of conditions is affected by branching around the "then" code if either condition is false, and the ORing of conditions is affected by branching to the "then" code if either condition is true.


272

Chapter 9 Implementation of C Procedures

Many else if expressions can

be inserted between an if expression and the final

else expression. The branch instructions jump out of a statement that is executed to the statement beyond the final else statement. Moreover, the final else expression may be omitted. Obviously, one can have more than two OR or AND tests, and one can nest OR tests within AND tests, or one can nest AND tests within OR tests, and so on.

One of the common errors in C is to confuse bit-wise logical OR with the OR test

discussed above. Theexpression if ((alpha < 5) | (beta == 0 ) ) gamma = 0;

encodes as

LDAA

alpha

; get variable alpha

CMPA

#5

BLT

LO

; if greater or equal to 5

LDX

#0

; generate zero

BRA

LI

; and skip

LO:

LDX

#1

; otherwise generate one

LI:

LDAA

beta

; test variable beta

BEQ

*+4

; if nonzero, branch to middle of CPS

CLRB

; otherwise clear B

CPS

#50689 ; address mode is actually LDAB #1

CLRA

; high-order byte is always zero

PSHX

; OR X into D

GRAB

1, SP

; by pushing X

ORAA

2,SP+

; then pulling it and ORing it into D

TBEQ

D, *+6

; if the result is nonzero

CLR

gamma

; clear variable gamma

What a difference a single character makes! Although the same answer is obtained with

the statement if ((alpha < 5)

| j

(beta = = 0 ) ) gamma = 0; as with if ((alpha

< 5) | (beta ==

0 ) ) gamma =

0;, the assembly language generated by the latter is

significantly less efficient than that generated by the former statement.

Another of the common errors in C is to confuse assignment with equality test. The

expression if (beta

== 0)

gamma

= 0; encodes as

TST

beta

; test variable beta

BNE

*+5

; if the result is nonzero then

CLR

gamma

; clear variable gamma

The expression if (beta =

0)

gamma = 0; encodes as

CLR

beta

; clear variable beta (note: this is an assignment statement)

BNE

*+5

; if the result is nonzero (it isn't) then

CLR

gamma

; clear variable gamma

From the rest of Figure 9.8, note how a string of else if ( . . . ) . . . else . . . ; statements cause the tests we have already discussed to be done, and when one is successful, so its following statement is executed, a branch is made to the end of the

series of else i f ( . . .

) . . . else . . .; statements. Incidentally, the subroutine branched

tobyBSR * +10 2

shifts the byte in beta left three places.