ee251: thursday september 12 · write a program to find the maximum element from an array of n 8bit...

34
Lecture #6 1 EE251: Thursday September 12 Branch Instructions Looping Using Conditional Branching Example Programs and Flowcharts Good Programming Practice Conditional Execution Reading: Chapter 6 Lab #2 finishes this week. Report due next week. Lab #3 starts next week. Note Lab #3 prework! Homework #1 due tomorrow at 4:00 in HW box in BC Infill. Homework #2 due in one week (Sept. 20 at 4:00 p.m.)

Upload: others

Post on 12-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 1

EE251: Thursday September 12• Branch Instructions• Looping Using Conditional Branching• Example Programs and Flowcharts• Good Programming Practice• Conditional Execution

• Reading: Chapter 6 • Lab #2 finishes this week. Report due next week.• Lab #3 starts next week. Note Lab #3 prework!• Homework #1 due tomorrow at 4:00 in HW box in BC Infill. • Homework #2 due in one week (Sept. 20 at 4:00 p.m.)

Page 2: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 2

Shift Example We are assuming registers are 8 bits wide for easier examples.

They are actually 32 bits wide.

• Example assuming 2’s complement value: – ASR r3, r4, #2 ;Assume [r4]=0xF0=2_11110000 = -16

⇒ r3 = 2_11111100 = 0xFC = -4 = -16 ÷ 22

• Try 32-bit example; same problem but– R4=0xFFFFFFF0 = -16 ⇒ r3=0xFFFFFFFC= -4

• Other examples? BIC?

Page 3: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Branch Instructions (Section 6.2 in Text)

• B label: causes a branch to label.• BL label: instruction copies the address of the next instruction into r14 (lr,

the link register), and causes a branch to label: Branch & Link• BX Rm: branch to the address held in register Rm, e.g. r14: Return • BLX Rm: copies the address of the next instruction into r14 (lr, the linkregister) and branch to the address held in register Rm

Instruction Operands Brief description Flags

B label Branch -BL label Branch with Link -

BLX Rm Branch indirect with Link

-

BX Rm Branch indirect -

Lecture #6 3

Page 4: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Branch With Link (Sect. 8.1 in Text)• The "Branch with link (BL)" instruction

implements a simple subroutine call by writing PC+4 into the LR register (R14).– i.e. the address of the next instruction following the

branch with link (It’s a 4 byte instruction).• To return from subroutine, simply restore the PCfrom the LR:

– BX LR ;Branch to location in LR

– Puts [LR] into PC and continues execution from there• BE CAREFUL WITH SUBROUTINES THAT

CALL OTHER SUBROUTINES! WHY?

Lecture #6 4

Page 5: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

OPEN Routine (Lab 3) Example;******************************************; OPEN subroutine ; it is called when the elevator gets ; to the destination floor;******************************************OPEN

PUSH {LR} ; Save existing return addressBL Delay ; Wait 1 sec.LDR R0,=MSG_OPEN ; Pointer to messageBL OutStr ; Send messagePOP {LR} ; Restore return addressBX LR ; return when done

;****************************************; Data Section; Can be combined with other message texts;****************************************

AREA |.text|, READWRITE, DATA

MSG_OPEN DCB "Door is open."DCB 0x0D, 0x04

We’ll discuss PUSH and POP instructions soon. For now, just think of This as a way of saving and restoring the return address for the program that called OPEN.

Lecture #6 5

Page 6: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

S is a statement or series of statements; C is a condition to be tested

• do S forever• For i = n1 to n2 do S or

For i = n2 downto n1 do S• While C do S• Repeat S until CProgram loops are implemented by using theconditional branch instructions.C, the condition to be tested, depends on the contents of the N, Z, C, V flags..

Key Program Looping Mechanisms

Lecture #6 6

Page 7: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Do S Forever While C Do S

Repeat S Until CFor Loops (up and down)

Lecture #6 7

Flowchart of Looping Constructs

Page 8: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Condition Code Flags in Program Status Register (PSR)

N, bit [31] Negative condition code flag. Set to bit [31] of the result of the instruction. If the result is regarded as a two's complement signed integer, then N == 1 if the result is negative and N = 0 if it is positive or zero.Z, bit [30] Zero condition code flag. Set to 1 if the result of the instruction is zero, and to 0 otherwise. A result of zero often indicates an equal result from a comparison.C, bit [29] Carry condition code flag. Set to 1 if the instruction results in a carry out on an addition or NO borrow on subtraction.V, bit [28] Overflow condition code flag. Set to 1 if the instruction results in an overflow condition (incorrect result) on signed addition or subtraction.Ignore Q, bit [27]

Lecture #6 8

Page 9: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

• The possible condition codes listed below are used in Conditional Branch instructions:

Note AL is the default and does not need to be specified

Suffix Description Flags testedEQ EQual Z=1NE Not Equal Z=0CS/HS Carry Set C=1CC/LO Carry Clear C=0MI MInus (Negative) N=1PL PLus (Positive or Zero) N=0VS oVerflow Set V=1VC oVerflow Clear V=0HI Unsigned HIgher C=1 & Z=0LS Unsigned Lower or Same C=0 or Z=1GE Signed Greater or Equal N=VLT Signed Less Than N!=VGT Signed Greater Than Z=0 & N=VLE Signed Less than or Equal Z=1 or N!=VAL ALways

Lecture #6 9

Condition Codes

Page 10: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Signed vs. UnsignedCondition codes applied to branch instructions.

You will find this table useful over the semester.

Compare Signed Unsigned

== BEQ BEQ

!= BNE BNE

> BGT BHI

>= BGE BHS

< BLT BLO

<= BLE BLS

Lecture #6 10

Page 11: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Branch InstructionsUsing Condition Codes

Instruction Description

Unconditional Branch

B label Branch always

Conditional Branch

BEQ label BNE label

BCS/BHS label

BCC/BLO label

BMI label

BPL label

BVS label

BVC label

BHI label

BLS label

BGE label

BLT label

BGT label

BLE label

Branch if EQual Branch if Not Equal

Branch if Carry Set

Branch if Carry Clear

Branch if MInus (Negative)

Branch if PLus (Positive or Zero)

Branch if oVerflow Set

Branch if oVerflow Clear

Branch if unsigned HIgher

Branch if unsigned Lower or Same

Branch if signed Greater or Equal

Branch if signed Less Than

Branch if signed Greater Than

Branch if signed Less than or Equal

Lecture #6 11

Page 12: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Compare Instruction

• CMP Rn, Operand2– Compares the value in register Rn with Operand 2

(Rn – Operand2). Updates condition flags on result but does not write result to a register.

• CMN Rn, Operand2– Adds the value in register Rn to Operand 2 (Rn +

Operand2). Updates condition flags on result but does not write result to a register.

• ExamplesCMP R2, R9CMN R0, #6400

Lecture #6 12

Page 13: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Compare and Branch Combo

• Except that it does not change the condition code flags, CBZ Rn, label is equivalent to:CMP Rn, #0BEQ label

• Except that it does not change the condition code flags, CBNZ Rn, label is equivalent to:CMP Rn, #0BNE label

Instruction Operands Brief description Flags

CBZ Rn, label Compare and Branch if Zero Z=1

CBNZ Rn, labelCompare and Branch if Non

ZeroZ=0

Lecture #6 13

Page 14: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Flowchart Example:Repeat–Until Flow Structure

AREA Data, DATA, READWRITETable DCD 1,2,5,4,7,21,29,18,266,2 ; Table data

DCD 15,6,89,8,15,98,44,11,61,65535Size EQU 20

AREA Divby2, CODE, READONLY_main LDR R0, =Table ; Address of Table

MOV R1, #Size ; Size of TableLoop LDR R2, [R0] ; Get Table Entry

ASR R2, #1 ; Shift entry right 1STR R2, [R0],#4 ; Store Table Entry, inc ptrSUBS R1, #1 ; Reduce count by 1BNE Loop ; Branch back if not done

Done B Done ; Dead LoopEND

Is data signed or unsigned or can it be either?

Finishedwith table?

No

Yes

Table(i)=Table(i)/2Increment Counters

Problem: Divide each element in table by 2, using shift-right inst.

Lecture #6 14

Page 15: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Flowchart Example:WHILE–DO Flow Structure

Finishedwith table?

No

Yes

Table(i)=Table(i)/2Increment Counters

Same Problem as previous slide

AREA Data, DATA, READWRITETable DCD 1,2,5,4,7,21,29,18,266,2 ; Table data

DCD 15,6,89,8,15,98,44,11,61,65535Size EQU 20

AREA Divby2, CODE, READONLY_main LDR R0, =Table ; Address of Table

MOV R1, #Size ; Size of TableLoop CBZ R1, Done ; Check if done. If so Done

LDR R2, [R0] ; Get Table EntryASR R2, #1 ; Shift entry right 1STR R2, [R0],#4; Store Table Entry & inc.SUB R1, #1 ; Reduce count by 1B Loop ; Branch back

Done B Done ; DoneEND

Lecture #6 15

Page 16: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Complete Flowchart Example:

Turn a sequence of ASCII digits into a number

Begin

Read Charfrom buffer

Digit? No

N=Char-0x30

Read Charfrom buffer

Yes

Digit?

No

N=N*10+(Char-0x30)

Yes

DoneLecture #6 16

Page 17: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 17

Page 18: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Repeat Until LoopingExample 2.15 Write a program to find the maximum element from an array of N 8-bit elements using the repeat S until C looping construct.Start

max_val← Array[0]

max_val < array[b] ?

yes

no

b = 0?

Stop

yes

no

max_val←Array[b]

b←b-1

b←N-1

Lecture #6 18

What is smallestvalue of N which Works in this example?

Page 19: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Code for finding Max element of ArrayUsing Repeat Until Loop

AREA Data, DATA, READWRITEArray DCD 1,2,5,4,7,21,29,18,266,2 ; Array data

DCD 15,6,89,655,8,15,98,44,11,61MaxVal DCD 0 ; Maximum element Size EQU 20 ; Number of Elements in Array

AREA MaxElement, CODE, READONLY__main LDR R0, =Array ; Address of Array

MOV R3, #(Size-1) ; Loop Counter IS THIS LEGAL?!?LDR R1, [R0] ; Set Array(0) as starting max valueADD R0, #(Size-1)*4; Point to last element of array

Loop LDR R2,[R0],#-4 ; Get table entry, decr. R0 (Why by 4?)CMP R2, R1 ; Is latest element > current max valueMOVGT R1, R2 ; If so, new max valueSUBS R3, #1 ; Reduce count by 1BNE Loop ; Branch back if not doneLDR R0,=MaxVal ; R0 points to MaxValSTR R1, [R0] ; Store final MaxValue in memory

Done B Done ; DoneEND

Lecture #6 19

Page 20: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Write a program to convert the 16-bit number in r0 to ASCII format. Convert the binary number, digit by digit to BCD,converting each BCD digit into its ASCII code and store that code in the appropriate byte of label ASNM.

Solution:-A binary number can be converted to BCD format by using repeated division by 10.-The largest 16-bit binary number is 65535 which has five decimal digits.-The first division by 10 obtains the least significant digit as the remainder(Remember this process for number conversion in ECE102?)-The second division by 10 obtains the second least significant digit, etc.-Conversion of each digit, 0-9, to ASCII involves adding $30 to the digit since ASCII(0)=$30, ASCII(1)= $31, … , ASCII(9)=$39

Solution follows slide showing ASCII Table.⇓

Lecture #6 20

Conversion of Binary ⇒ BCD ⇒ ASCII

Page 21: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 21

Page 22: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

; Reset AreaAREA RESET, CODE, READONLYTHUMBEXPORT Vectors

VectorsDCD 4DCD Reset_Handler ; Reset Handler

; Program to turn number in Tdata into ASCII rep. in string ASNMAREA myData, DATA

ASNM SPACE 6 ; Reserve 5 bytes for string & null terminated

; Store constants in ROMAREA constantdata, DATA, READONLY

Tdata DCD 12345 ; data to be tested 12345 = 0x3039

Program to turn Binary Number to ASCII Initialization Area

Lecture #6 22

Page 23: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Binary to ASCII Conversion Code Area

Lecture #6 23

; Code area for binary to ASCII Version AREA |.text|, CODE, READONLY

Reset_Handler ldr r0, =Tdata ; r0 points to binary data to convert to ASCII ldr r4, =ASNM ; r4 points to ASCII String result ldr r0, [r0] ; r0 now contains this binary data mov r1, #10 ; prepare to do divide by 10 udiv r2,r0,r1 ; divide r0 by 10. Quotient is in r2 mul r3,r2,r1 ; r3=(r0/10)*10 (r0 without least sig. digit) sub r3,r0,r3 ; r3 is now remainder after divide (least sig. digit) add r3, #0x30 ; convert the r3 digit into ASCII code strb r3,[r4,#4] ; save least significant digit 4 bytes from most mov r0, r2 ; put quotient in r0

; Six preceding instructions are repeated 3 times, changing only the index ; offset in the strb instruction (to #3, #2, and #1 in that order

add r2,#0x30 ; convert most significant digit into ASCII--why r2? strb r2, [r4] ; save most significant ASCII digit

done b done ; finished-infinite loopEND ; end of assembly program

Page 24: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Programmer’s GoalsIn Approximate Priority Order

• Write a CORRECT program.• Write an easily understood program.• Write an easily modified program.• Meet your committed schedule.• Write a fast program.• Write a compact (short) program.

How do you determine your actual goals and priorities?

Lecture #6 24

Page 25: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Good Programming Practice• “Top-Down” Design

– Begin with the end in mind: Project Requirements • Very important to get these correct, otherwise waste MUCH

time!• Usually an iterative process with your “customer”

– “Divide and Conquer”• Overall programming task is broken down into smaller

subtasks• Subtasks broken down as appropriate to even smaller

subtasks• Problem solved in small “chunks” that are understandable

and manageable• “Bottom-Up” Implementation of key algorithms

– Especially where data structures, detailed requirements aren’t clear

Lecture #6 25

Page 26: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 26

Good Programming Practice, cont’d“Top-Down” + “Bottom-Up” = Work toward Middle

– Top-Down Creates overall Structure (Outline) of Code Modules• Global data structures• Overall program flow

– Bottom-Up provides foundation• Detailed data structures• Low-level algorithms• Required parameters

Prototype & Breadboard at all levels– Helps test modules above and below– Use dummy data to minimize time in testing– Then replace prototype modules with real modules as completed

Page 27: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 27

Good Programming Practice, cont’d 2

Test at earliest possible time--”Egoless Programming”– Earliest testing is cheapest/fastest testing– Validate Requirements with “Customer” and keep in loop as you

implement--spend time thinking about getting requirements right– Invite others to review high-level design; Return the favor.

Test as thoroughly as possible--”Robust Code”– Test modules with “test fixture” modules that test all the “edges

of your algorithms”--spend time thinking about unit testing– Expose entire program to as many varied input combinations as

you can afford--spend time thinking about system testing

Page 28: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Conditional Execution

Add instruction Condition Flag testedADDEQ r3, r2, r1 Add if EQual Add if Z = 1ADDNE r3, r2, r1 Add if Not Equal Add if Z = 0ADDHS r3, r2, r1 Add if Unsigned Higher or Same Add if C = 1ADDLO r3, r2, r1 Add if Unsigned LOwer Add if C = 0ADDMI r3, r2, r1 Add if Minus (Negative) Add if N = 1ADDPL r3, r2, r1 Add if PLus (Positive or Zero) Add if N = 0ADDVS r3, r2, r1 Add if oVerflow Set Add if V = 1ADDVC r3, r2, r1 Add if oVerflow Clear Add if V = 0ADDHI r3, r2, r1 Add if Unsigned HIgher Add if C = 1 & Z = 0ADDLS r3, r2, r1 Add if Unsigned Lower or Same Add if C = 0 or Z = 1ADDGE r3, r2, r1 Add if Signed Greater or Equal Add if N = VADDLT r3, r2, r1 Add if Signed Less Than Add if N != VADDGT r3, r2, r1 Add if Signed Greater Than Add if Z = 0 & N = VADDLE r3, r2, r1 Add if Signed Less than or Equal Add if Z = 1 or N = !V

Big Idea: Execute instruction if/only if conditions are met

Lecture #6 28

Page 29: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Example of CMP

Area absolute, CODE, READONLYEXPORT __main ENTRY

__main CMP r1, #0RSBLT r0, r1, #0 ;more on this soon

done B done ; deadloopEND

Note: RSB = Reverse SuBtract in RSBLT instruction

Lecture #6 29

Page 30: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

; Suppose [r1] = a, [r2] = xCMP r1, #0 ; Compare a with 0 RSBLT r1, r1, #0 ; a = 0 - a if a < 0ADD r2, r2, #1 ; x = x + 1

Implementation 2:

If a<0 thena = -a

x = x + 1

Neat, huh?

If-then Statement

Lecture #6 30

Page 31: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Another Example of Conditional Execution

if (a <= 0) y = -1;

elsey = 1;

CMP r0, #0MOVLE r1, #-1MOVGT r1, #1

LE: Signed Less than or EqualGT: Signed Greater Than

a ⟶ r0y ⟶ r1

Lecture #6 31

Page 32: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Compound Boolean ExpressionAssembly Program

; r0 = x, r1 = aCMP r0, #20 ; compare x and 20MOVLE r1, #1 ; a=1 if less or equalCMP r0, #25 ; CMP if greater thanMOVGE r1, #1 ; a=1 if greater or equal

endif

What is happening in the code segment above?

Lecture #6 32

Page 33: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Greatest Common Divisor (GCD)

While (a != b ) {if (a > b) a = a – b; else b = b – a;

}

; r0 = a and r1 = bgcd CMP r0, r1 ; a > b?

BEQ done ; if a = b, doneBLT less ; a < bSUB r0, r0, r1 ; a = a – bB gcd

less SUB r1, r1, r0 ; b = b – aB gcd

done B done

Euclid’s Algorithm; r0 = a and r1 = bgcd CMP r0, r1

SUBGT r0, r0, r1SUBLT r1, r1, r0BNE gcd

done B done

Lecture #6 33

Page 34: EE251: Thursday September 12 · Write a program to find the maximum element from an array of N 8bit - elements using ... b ← b-1 b ← N-1. Lecture #6 18. What is smallest value

Lecture #6 34

Summary• Branch Instructions• Looping Using Conditional Branching• Example Programs and Flowcharts• Conditional Execution as time allows• Good Programming Practice as time allows

• Bring questions Tuesday if you’re still confused after reading text, reviewing slides, and studying this!

• Next Lecture: – STACK Read Text Chapter 8 thru 8.4– More Branch related instructions– More Looping Examples as time allows– Debugging Tips