CPE/EE 421 Microcomputersmilenka/cpe421-05F/notes/cpe421-s04_21.pdf · ¾Groups of instructions:...

24
Alex Milenkovich 1 CPE/EE 421/521 Microcomputers 1 U A H U A H U A H CPE/EE 421 Microcomputers Instructor: Dr Aleksandar Milenkovic Lecture Note S04 *Material used is in part developed by Dr. D. Raskovic and Dr. E. Jovanov CPE/EE 421/521 Microcomputers 2 U A H U A H U A H Course Administration Instructor: Aleksandar Milenkovic [email protected] www.ece.uah.edu/~milenka EB 217-L Mon. 5:30 PM – 6:30 PM, Wen. 12:30 – 13:30 PM URL: http://www.ece.uah.edu/~milenka/cpe421-05F TA: Joel Wilder Labs: Lab#1 is on. First session 9/12. Hws: Hw #1 is posted (due 9/21/05, 2:20); Text: Microprocessor Systems Design: 68000 Hardware, Software, and Interfacing Review: Chapter 1, Chapter 2; Today: Instruction Set, Subroutines, Assembly and C;

Transcript of CPE/EE 421 Microcomputersmilenka/cpe421-05F/notes/cpe421-s04_21.pdf · ¾Groups of instructions:...

  • Alex Milenkovich 1

    CPE/EE 421/521 Microcomputers 1

    U

    A

    HU

    A

    H

    U

    A

    H

    CPE/EE 421Microcomputers

    Instructor: Dr Aleksandar MilenkovicLecture Note

    S04

    *Material used is in part developed by Dr. D. Raskovic and Dr. E. Jovanov

    CPE/EE 421/521 Microcomputers 2

    U

    A

    HU

    A

    H

    U

    A

    HCourse Administration

    Instructor: Aleksandar [email protected]/~milenkaEB 217-LMon. 5:30 PM – 6:30 PM, Wen. 12:30 – 13:30 PM

    URL: http://www.ece.uah.edu/~milenka/cpe421-05F

    TA: Joel Wilder

    Labs: Lab#1 is on. First session 9/12.

    Hws: Hw #1 is posted (due 9/21/05, 2:20);

    Text: Microprocessor Systems Design: 68000 Hardware, Software, and Interfacing

    Review: Chapter 1, Chapter 2;

    Today: Instruction Set, Subroutines, Assembly and C;

  • Alex Milenkovich 2

    CPE/EE 421/521 Microcomputers 3

    U

    A

    HU

    A

    H

    U

    A

    H

    The 68000 Family Instruction Set

    Assumption: Students are familiar with the fundamentals of microprocessor architecture

    Groups of instructions:Data movementArithmetic operationsLogical operationsShift operationsBit ManipulationProgram Control

    Important NOTE:The contents of the CC byte of the SR are updated after the execution of an instruction. Refer to Table 2.2

    CPE/EE 421/521 Microcomputers 4

    U

    A

    HU

    A

    H

    U

    A

    HData Movement OperationsCopy information from source to destination

    Comprises 70% of the average program

    MOVE/MOVEA

    MOVE to CCRMOVE ,CCR – word instruction

    MOVE to/from SR MOVE ,SR – in supervisor mode only;MOVE #$2700,SR – sets the 68K in supervisor mode

    MOVE USP – to/from User Stack PointerMOVE.L USP,A3 - transfer the USP to A3

    MOVEQ – Move Quick(8b #value to 32b reg)

    MOVEM – to/from multiple registers (W/L)e.g., MOVEM.L D0-D5/A0-A5, -(A7)

    MOVEM.L (A7)+,D0-D5/A0-A5

    MOVEP – Move Peripheral

  • Alex Milenkovich 3

    CPE/EE 421/521 Microcomputers 5

    U

    A

    HU

    A

    H

    U

    A

    HData Movement Operations, cont’dMoving data from a 32-bit register

    to memory using the MOVEP instruction

    Bytes from the register are stored in every other memory byte

    NOTE: The instruction takes 24 clock cycles to execute

    CPE/EE 421/521 Microcomputers 6

    U

    A

    HU

    A

    H

    U

    A

    HData Movement Operations, cont’dLEA

    Calculates an effective address and loads it into an address register – LEA ,An

    Can be used only with 32-bit operands

    Assembly language RTL

    LEA $0010FFFF,A5 [A5] ← $0010FFFFLoad the address $0010 FFFF into register A5.

    LEA $12(A0,D4.L),A5 [A5] ← $12 + [A0] + [D4]Load contents of A0 plus contents of D4 plus $12 into A5.

    NOTE: If the instruction MOVEA.L $12(A0,D4),A5 had been used, the contents of that address would have been deposited in A5.

    Why use it? FASTER! ADD.W $1C(A3,D2),D0 vs. LEA $1C(A3,D2),A5

    ADD.W (A5),D0

  • Alex Milenkovich 4

    CPE/EE 421/521 Microcomputers 7

    U

    A

    HU

    A

    H

    U

    A

    H

    68000 RegistersD0 01234567 D1 89ABCDEF D2 0001002D D3 ABCD7FFFD4 33449127 D5 AAAAAAAA D6 ABCD0003 D7 55555555A0 00007020 A1 00007000 A2 00007010 A3 00007030A4 00010020 A5 00FF789A A6 00010000 A7 00010010Status register2700

    Main memory007000 AE 007020 5A 010000 DD 010020 DC007001 F2 007021 AD 010001 B2 010021 25007002 32 007022 99 010002 00 010022 15007003 77 007023 92 010003 15 010023 17007004 89 007024 79 010004 76 010024 29007005 90 007025 33 010005 19 010025 39007006 1A 007026 97 010006 92 010026 49007007 AE 007027 14 010007 26 010027 2D007008 EE 007028 79 010008 17 010028 B2007009 F1 007029 E7 010009 14 010029 6200700A F2 00702A 00 01000A E7 01002A 8100700B A4 00702B 0A 01000B E8 01002B 2100700C AE 00702C 88 01000C 19 01002C 4500700D 88 00702D 18 01000D 92 01002D 1800700E AA 00702E 82 01000E 19 01002E 3100700F E4 00702F 79 01000F 54 01002F D9007010 7E 007030 23 010010 45 010030 AA007011 8D 007031 17 010011 99 010031 77007012 9C 007032 46 010012 15 010032 78007013 C4 007033 9E 010013 43 010033 AE007014 B2 007034 FC 010014 25 010034 EA007015 12 007035 FF 010015 76 010035 34007016 39 007036 77 010016 89 010036 25007017 90 007037 60 010017 17 010037 17007018 00 007038 21 010018 81 010038 15007019 89 007039 42 010019 17 010039 1400701A 14 00703A 55 01001A 4E 01003A 1700701B 01 00703B EA 01001B 72 01003B F900701C 3D 00703C 61 01001C 33 01003C 8A00701D 77 00703D 81 01001D 23 01003D 0F00701E 89 00703E C9 01001E E1 01003E F200701F 9A 00703F AA 01001F CD 01003F E5

    Example:

    CPE/EE 421/521 Microcomputers 8

    U

    A

    HU

    A

    H

    U

    A

    H

    a) ORG $9000LEA TABLE1(PC),A5Assembly listing1 00009000 ORG $90002 00009000 4BFA0FFE LEA TABLE1(PC),A5

    What is the effect of applying each of the following 68000 instructions assuming the initial condition shown before? Represent modified internal registers, memory locations and conditions.

    b) LEA 6(A0,D6.W),A2

    EA = $00009000 + 2 + $0FFE = $0000A000 A5=$0000A000, CC: Not affected (NA)

    current PC value

    EA = 6 + $00007020 + $0003 = $00007029 A2=$00007029 CC: NA

    offset A0 D6.W

  • Alex Milenkovich 5

    CPE/EE 421/521 Microcomputers 9

    U

    A

    HU

    A

    H

    U

    A

    HData Movement Operations, cont’d

    PEAPush Effective Address

    Calculates an effective address and pushes it onto the stack pointed at by A7 – PEA

    Can be used only with 32-bit operands

    EXG

    Exchanges the entire 32-bit contents of two registers

    EXG Xi,Xj

    SWAP

    Exchanges the upper- and lower-order words of a DATA register

    SWAP Di

    CPE/EE 421/521 Microcomputers 10

    U

    A

    HU

    A

    H

    U

    A

    H

    Integer Arithmetic Operations

    Floating-point operations not directly supported

    Except for division, multiplication, and if destination is Ai, all act on 8-, 16-, and 32-bit values

    ADD/ADDA (no mem-to-mem additions, if destination is Ai, use ADDA)

    ADDQ (adds a small 3-bit literal quickly)

    ADDI (adds a literal value to the destination)

    ADDX (adds also the contents of X bit to the sum)used for multi-precision addition

    CLR (clear specified data register or memory location)equivalent to MOVE #0, for address registers use SUB.L An,An

  • Alex Milenkovich 6

    CPE/EE 421/521 Microcomputers 11

    U

    A

    HU

    A

    H

    U

    A

    HInteger Arithmetic Operations, cont’dDIVU/DIVS – unsigned/2’s-complement numbers

    DIVU ,Dn or DIVS ,Dn32-bit longword in Dn is divided by the 16-bit word at

    16-bit quotient is deposited in the lower-order word of DnThe remainder is stored in the upper-order word of Dn

    MULU/MULS – unsigned/2’s-complement numbersLow-order 16-bit word in Dn is multiplied by the 16-bit word at 32-bit product is deposited in Dn

    SUB, SUBA, SUBQ, SUBI, SUBX

    NEG – forms the 2’s complement of an operandNEG

    NEGX – Negate with Extend, used for multi-prec. arith.

    EXT – Sign ExtendEXT.W Dn copies bit 7 to bits 8-15EXT.L Dn copies bit 15 to bits 16-31

    CPE/EE 421/521 Microcomputers 12

    U

    A

    HU

    A

    H

    U

    A

    HBCD Arithmetic Operations

    Only 3 instructions support BCDABCD Di,Dj or ABCD –(Ai),-(Aj)Add BCD with extend – adds two packed BCD digits together with X bit from the CCR

    SBCD – similar[destination]←[destination]-[source]-[X]

    NBCD subtracts the specified operand from zero together with X bit and forms the 10’s complement of the operand if X =0, or 9’s complement if X =1

    Involve X because they are intended to be used in operations on a string of BCD digits

  • Alex Milenkovich 7

    CPE/EE 421/521 Microcomputers 13

    U

    A

    HU

    A

    H

    U

    A

    H

    Logical Operations

    Standard AND, OR, EOR, and NOT

    Immediate operand versions: ANDI, ORI, EORI

    AND a bit with 0 – mask

    OR a bit with 1 – set

    EOR a bit with 1 – toggle

    Logical operations affect the CCR in the same way as MOVE instructions

    CPE/EE 421/521 Microcomputers 14

    U

    A

    HU

    A

    H

    U

    A

    H

    Shift Operations

    Logical ShiftLSL – Logical Shift LeftLSR – Logical Shift Right

  • Alex Milenkovich 8

    CPE/EE 421/521 Microcomputers 15

    U

    A

    HU

    A

    H

    U

    A

    H

    Shift Operations, cont’d

    Arithmetic ShiftASL – Arithmetic Shift LeftASR – Arithmetic Shift Right

    CPE/EE 421/521 Microcomputers 16

    U

    A

    HU

    A

    H

    U

    A

    H

    Shift Operations, cont’d

    RotateROL – Rotate LeftROR – Rotate Right

  • Alex Milenkovich 9

    CPE/EE 421/521 Microcomputers 17

    U

    A

    HU

    A

    H

    U

    A

    H

    Shift Operations, cont’d

    Rotate Through ExtendROXL – Rotate Left Through ExtendROXR – Rotate Right Through Extend

    CPE/EE 421/521 Microcomputers 18

    U

    A

    HU

    A

    H

    U

    A

    H

    Effect of the Shift Instructions

    After CCR After CCRInitial Value First Shift XNZVC Second Shift XNZVC

    ASL 11101011 11010110 11001 10101100 11001ASL 01111110 11111100 01010 11111000 11011

    ASR 11101011 11110101 11001 11111010 11001ASR 01111110 00111111 00000 00011111 10001

    LSL 11101011 11010110 11001 10101100 11001LSL 01111110 11111100 01000 11111000 11001

    LSR 11101011 01110101 10001 00111010 10001LSR 01111110 00111111 00000 00011111 10001

    ROL 11101011 11010111 ?1001 10101111 ?1001ROL 01111110 11111100 ?1000 11111001 ?1001

    ROR 11101011 11110101 ?1001 11111010 ?1001ROR 01111110 00111111 ?0000 10011111 ?1001

  • Alex Milenkovich 10

    CPE/EE 421/521 Microcomputers 19

    U

    A

    HU

    A

    H

    U

    A

    H

    Forms of Shift Operations

    Mode 1 ASL Dx,Dy Shift Dy by Dx bits

    Mode 2 ASL #,Dy Shift Dy by #data bits

    Mode 3 ASL Shift the contents

    at the effective address by one place

    All three modes apply to all eight shift instructions

    CPE/EE 421/521 Microcomputers 20

    U

    A

    HU

    A

    H

    U

    A

    H

    Bit Manipulation Operations

    Act on a single bit of an operand:1. The complement of the selected bit is moved to the

    Z bit (Z set if specified bit is zero)2. The bit is either unchanged, set, cleared, or toggled

    NVCX bits are not affected

    May be applied to a bit within byte or longword

    BTST – Bit Test only

    BSET – Bit Test and Set (specified bit set)

    BCLR – Bit Test and Clear (specified bit cleared)

    BCHG – Bit Test and Change (specified bit toggled)

  • Alex Milenkovich 11

    CPE/EE 421/521 Microcomputers 21

    U

    A

    HU

    A

    H

    U

    A

    H

    Effective address of the operand

    Bit Manipulation Operations, cont’d

    All 4 have the same assembly language forms:

    BTST Dn, or BTST #,

    Location of the bit to be tested

    CPE/EE 421/521 Microcomputers 22

    U

    A

    HU

    A

    H

    U

    A

    H

    Program Control Operations

    Examine bits in CCR and chose between two courses of action

    CCR bits are either: Updated after certain instruction have been executed, orExplicitly updated (bit test, compare, or test instructions)

    Compare instructions: CMP, CMPA, CMPI, CMPMSubtract the contents of one register (or mem. location) from another register (or mem. location) Update NZVC bits of the CCRX bit of the CCR is unaffected

    The result of subtraction is ignored

  • Alex Milenkovich 12

    CPE/EE 421/521 Microcomputers 23

    U

    A

    HU

    A

    H

    U

    A

    H

    Program Control Operations, cont’d

    CMP: CMP ,[]-[]

    CMPI: CMP #,comparison with a literal

    CMPA: CMP ,Anused for addresses, operates only on word and longword operands

    CMPM: CMP (Ai)+,(Aj)+compares memory with memory, one of few that works only with operands located in memory

    TST: TST zero is subtracted from specified operand;N and Z are set accordingly, V and C are cleared, X is unchanged

    Except CMPA, all take byte, word, or longword operands

    CPE/EE 421/521 Microcomputers 24

    U

    A

    HU

    A

    H

    U

    A

    H

    Program Control Operations, cont’d

    Branch InstructionsBranch ConditionallyBranch UnconditionallyTest Condition, Decrement, and Branch

    BRANCH CONDITIONALLY

    Bcc cc stands for one of 14 logical conditions (Table 2.4)Automatically calculated displacement can be d8 or d16Displacement is 2’s complement signed number 8-bit displacement can be forced by adding .S extensionZNCV bits are used to decide

  • Alex Milenkovich 13

    CPE/EE 421/521 Microcomputers 25

    U

    A

    HU

    A

    H

    U

    A

    HProgram Control Operations, cont’d

    BRANCH UNCONDITIONALLYBRA or JMP (An)

    JMP d16(An) JMP d8(An,Xi)JMP Absolute_addressJMP d16(PC)JMP d8(PC,Xi)

    TEST CONDITION, DECREMENT, and BRANCHDBcc Dn, (16 bit displacement only)

    One of 14 values from Table 2.4, plus T, plus F

    If test is TRUE, branch is NOT taken !

    If cc is NOT TRUE, Dn is decremented by 1;If Dn is now equal to –1 next instruction is executed

    if not, branch to

    CPE/EE 421/521 Microcomputers 26

    U

    A

    HU

    A

    H

    U

    A

    HSubroutines

    BRANCH TO SUBROUTINEBSR = [A7]← [A7] - 4

    M([A7])]← [PC] [PC]← [PC] + d8

    RETURN FROM SUBROUTINERTS = [PC]← [M([A7])]

    [A7]← [A7] + 4

  • Alex Milenkovich 14

    CPE/EE 421/521 Microcomputers 27

    U

    A

    HU

    A

    H

    U

    A

    HSubroutines, cont’d

    BRANCH TO SUBROUTINE000FFA 41F900004000 LEA TABLE, A0001000 61000206 NextChr BSR GetChar001004 10C0 MOVE.B D0,(A0)001006 0C00000D CMP.B #$0D,D000100A 66F4 BNE NextChr

    001102 61000104 BSR GetChr001106 0C000051 CMP.B #’Q’,D000110A 67000EF4 BEQ QUIT

    001208 1239000080000 GetChr MOVE.B ACIAC,D0

    BSR d8 d8=? (or d16, to specify d8 use BSR.S)

    d8 = $00001208 – ($00001000 + 2) = $00000206

    current PC value

    CPE/EE 421/521 Microcomputers 28

    U

    A

    HU

    A

    H

    U

    A

    HNested Subroutines

  • Alex Milenkovich 15

    CPE/EE 421/521 Microcomputers 29

    U

    A

    HU

    A

    H

    U

    A

    HNested Subroutines, cont’d

    CPE/EE 421/521 Microcomputers 30

    U

    A

    HU

    A

    H

    U

    A

    HNested Subroutines, cont’d

    Returning directly to a higher-level subroutineSub2 .

    .BEQ Exit..RTS

    Exit LEA 4(A7),A7RTS

    RTR (Return and restore condition codes)Save the condition code register on the stack:MOVE CCR, -(A7)

    Use RTR instead of RTS

  • Alex Milenkovich 16

    CPE/EE 421/521 Microcomputers 31

    U

    A

    HU

    A

    H

    U

    A

    H

    Miscellaneous Instructions

    Scc: Set byte conditionallyScc (cc same as in DBcc)If the condition is TRUE, all the bits of the byte specified by are SET, if the condition is FALSE, bits are CLEARED

    NOP: No Operation

    RTS: Return from Subroutine

    STOP: STOP #nStop and load n into Status Register; n is 16-bit number; Privileged instruction

    CHK, RESET, RTE, TAS, TRAPV - later

    CPE/EE 421/521 Microcomputers 32

    U

    A

    HU

    A

    H

    U

    A

    HExample: Linked List

    Adding an element to the end of a linked list• HEAD points to the first element, NEW contains the address of

    the new item to be inserted• Longwords

    LEA HEAD,A0 A0 initially points to the start of the * linked listLOOP TST.L (A0) IF the address field = 0

    BEQ EXIT THEN exitMOVEA.L (A0),A0 ELSE read the address of the next element BRA LOOP Continue

    EXIT LEA NEW,A1 Pick up address of new elementMOVE.L A1,(A0) Add new entry to end of listCLR.L (A1) Insert the new terminator

  • Alex Milenkovich 17

    CPE/EE 421/521 Microcomputers 33

    U

    A

    HU

    A

    H

    U

    A

    HExample: Linked List, cont’d

    Initial linked list:

    LEA HEAD,A0 A0 initially points to the start of the * linked listLOOP TST.L (A0) IF the address field = 0

    BEQ EXIT THEN exitMOVEA.L (A0),A0 ELSE read the address of the next element BRA LOOP Continue

    EXIT LEA NEW,A1 Pick up address of new elementMOVE.L A1,(A0) Add new entry to end of listCLR.L (A1) Insert the new terminator

    CPE/EE 421/521 Microcomputers 34

    U

    A

    HU

    A

    H

    U

    A

    HExample: Linked List , cont’d

    Linked list after inserting an element at the end:

    LEA HEAD,A0 A0 initially points to the start of the * linked listLOOP TST.L (A0) IF the address field = 0

    BEQ EXIT THEN exitMOVEA.L (A0),A0 ELSE read the address of the next element BRA LOOP Continue

    EXIT LEA NEW,A1 Pick up address of new elementMOVE.L A1,(A0) Add new entry to end of listCLR.L (A1) Insert the new terminator

  • Alex Milenkovich 18

    CPE/EE 421/521 Microcomputers 35

    U

    A

    HU

    A

    H

    U

    A

    HExample: Linked List , Memory Map

    CPE/EE 421/521 Microcomputers 36

    U

    A

    HU

    A

    H

    U

    A

    H

    Assembly Language and C

    We are interested in:How a high-level language uses low-level language features?C: System programming, device drivers, …Use of addressing modes by compilersParameter passing in assembly languageLocal storage

  • Alex Milenkovich 19

    CPE/EE 421/521 Microcomputers 37

    U

    A

    HU

    A

    H

    U

    A

    H

    Assembly Language and C, ACIA example

    Character_Input(Func, Dev_loc, Input_Char, Error_St)Error_St=0IF Func = 0

    THEN Initialize Input_DevELSE Read status of Input_Dev

    IF status OK THENBEGIN

    Set Cycle_Count to max valueREPEAT

    Read status of Input_DevDecrement Cycle_Count

    UNTIL Input_Dev is ready OR Cycle_Count = 0Input_Char = input from Input_DeviceIF Cycle_Count = 0

    THEN Error_St = $FF END_IFEND

    ELSE Error_St = status from Input_DevEND_IF

    END_IFEnd Character_Input

    CPE/EE 421/521 Microcomputers 38

    U

    A

    HU

    A

    H

    U

    A

    H

    ACIA example, 68000 assembly language version

    * ACIA_Initialize and Character_Input routine* Data register D0 contains Function (zero=initialize, non-zero = get a character)

    * Data register D0 is re-used for the Cycle_Count (a timeout mechanism)

    * Data register D1 returns Error_Status* Data register D2 returns the character from the ACIA* Data register D3 is temporary storage for the ACIA’s status* Data register D4 is temporary storage for the masked ACIA’s status (error bits)

    * Address register A0 contains the address of the ACIA’scontrol/status register

    *Char_In MOVEM.W D3-D4,-(A7) Push working registers on the stack

    CLR.B D1 Start with Error_Status clearCMP.B #0,D0 IF Function not zero THEN get inputBNE InPut ELSE initialize ACIAMOVE.B #3,(A0) Reset the ACIAMOVE.B #$19,(A0) Configure the ACIABRA Exit_2 Return after initialization

  • Alex Milenkovich 20

    CPE/EE 421/521 Microcomputers 39

    U

    A

    HU

    A

    H

    U

    A

    H

    ACIA example, 68000 assembly language version

    *InPut MOVE.W #$FFFF,D0 Set up Cycle_Count for time-out

    (reuse D0)InPut1 MOVE.B (A0),D3 Read the ACIA’s status register

    MOVE.B D3,D4 Copy status to D4AND.B #%01111100,D4 Mask status bits to error conditionsBNE Exit_1 IF status indicates error, set error

    flags & returnBTST #0,D3 Test data_ready bit of statusBNE Data_Ok IF data_ready THEN get dataSUBQ.W #1,D0 ELSE decrement Cycle_CountBNE InPut1 IF not timed out THEN repeatMOVE.B #$FF,D1 ELSE Set error flagBRA Exit_2 and return

    *Data_Ok MOVE.B (2,A0),D2 Read the data from the ACIA

    BRA Exit_2 and return *Exit_1 MOVE.B D4,D1 Return Error_StatusExit_2 MOVEM.W (A7)+,D3-D4 Restore working registers

    RTS Return

    CPE/EE 421/521 Microcomputers 40

    U

    A

    HU

    A

    H

    U

    A

    HTwo registers are used in subroutine and have to be saved on the stack:

    MOVE.W D3-D4,-(A7)(otherwise, data would be lost)

    D0 is simply reused without saving, because the old data will not be needed

    PROS:Position independent codeRe-entrancy (subroutine has to save registers before they are reused

    CONS:Reduces number of registers available to programmerNumber of parameters limited by the number of registers

    Passing Parameters via Registers

  • Alex Milenkovich 21

    CPE/EE 421/521 Microcomputers 41

    U

    A

    HU

    A

    H

    U

    A

    H

    Passing parameters by valueActual parameter is transferredIf the parameter is modified by the subroutine, the “new value” does not affect the “old value”

    Passing parameters by referenceThe address of the parameter is passedThere is only one copy of parameterIf parameter is modified, it is modified globally

    Mechanisms for Parameter Passing

    CPE/EE 421/521 Microcomputers 42

    U

    A

    HU

    A

    H

    U

    A

    H

    Passing Parameters by Value

    LEA (-4,A7),A7 Save space on stack for Error_Status and Input_Char

    0SP

    0

    SPError_Status

    Input_Char

    -2

    -4State of stack after executing this instruction

    Address with respect to the initial stack pointer

  • Alex Milenkovich 22

    CPE/EE 421/521 Microcomputers 43

    U

    A

    HU

    A

    H

    U

    A

    H

    MOVE.L #ACIA,-(A7) Push ACIA address on the stackMOVE.W Func,-(A7) Push function code on the stack

    0

    SPError_Status

    Input_Char

    -2

    -4

    Function

    ACIA address

    Input_Char

    Error_Status

    -10

    -8

    -4

    -2

    0

    SP

    Passing Parameters by Value

    CPE/EE 421/521 Microcomputers 44

    U

    A

    HU

    A

    H

    U

    A

    H

    Return address

    Function

    ACIA address

    Input_Char

    Error_Status

    -14

    -10

    -8

    -4

    -2

    0

    SP

    BSR Char_In Call subroutineLEA (6,A7),A7 Clean up stack - remove parameters

    Function/ACIAMOVE.W (A7)+,Char Pull the input character off the stackMOVE.W (A7)+,Err Pull the Error_Status off the stack

    Passing Parameters by Value

  • Alex Milenkovich 23

    CPE/EE 421/521 Microcomputers 45

    U

    A

    HU

    A

    H

    U

    A

    H

    * Character_Input and ACIA_Initialize routine* Data register D3 is temporary storage for the ACIA’s status* Data register D4 is temporary storage for the Cycle_Count* Address register A0 contains the address of the ACIA’scontrol/status register

    *Char_In MOVEM.L A0/D3-D4,-(A7) Push working registers on the stack

    MOVE.L (18,A7),A0 Read address of ACIA from the stackCLR.B (24,A7) Start with Error_Status clear

    Passing Parameters by Value

    Saved registers

    Return address

    Function

    ACIA address

    Input_Char

    Error_Status

    -26

    -14

    -10

    -8

    -4

    -2

    0

    SP12

    16

    18

    22

    24

    Address with respect to the final value of stack pointer

    Address with respect to the initial stack pointer

    CPE/EE 421/521 Microcomputers 46

    U

    A

    HU

    A

    H

    U

    A

    H

    Passing Parameters by ValueCMPI.B #0,(16,A7) IF Function not zero THEN get inputBNE InPut ELSE initialize ACIAMOVE.B #3,(A0)MOVE.B #$19,(A0)BRA Exit_2 Return after initialization

    *InPut MOVE.W #$FFFF,D0 Set up Cycle_Count for time-out

    (reuse D0)InPut1 MOVE.B (A0),D3 Read the ACIA’s status register

    MOVE.B D3,D4 Copy status to D4AND.B #%01111100,D4 Mask status bits to error conditionsBNE Exit_1 IF status indicates error, deal with itBTST #0,D3 Test data_ready bit of saved status

    statusBNE Data_OK IF data_ready THEN get dataSUBQ.W #1,D0 ELSE decrement Cycle_countBNE InPut1 IF not timed out THEN repeatMOVE.B #$FF,(24,A7) ELSE Set error flagBRA Exit_2 and return

    Saved registers

    Return address

    Function

    ACIA address

    Input_Char

    Error_Status

    -26

    -14

    -10

    -8

    -4

    -2

    0

    SP12

    16

    18

    22

    24

  • Alex Milenkovich 24

    CPE/EE 421/521 Microcomputers 47

    U

    A

    HU

    A

    H

    U

    A

    H

    Passing Parameters by Value

    Data_OK MOVE.W (2,A0),(22,A7) Read the data from the ACIA and put on the stack

    BRA Exit_2 and return *Exit_1 MOVE.B D4,(24,A7) Return Error_StatusExit_2 MOVEM.L (A7)+,A0/D3-D4 Restore working registers

    RTS Return

    Function

    ACIA address

    Input_Char

    Error_Status

    -10

    -8

    -4

    -2

    0

    SP

    CPE/EE 421/521 Microcomputers 48

    U

    A

    HU

    A

    H

    U

    A

    H* BACK TO MAIN PROGRAM :*

    BSR Char_In Call subroutineLEA (6,A7),A7 Clean up stack - remove parameters

    Function/ACIAMOVE.W (A7)+,Char Pull the input character off the stackMOVE.W (A7)+,Err Pull the Error_Status off the stack

    0

    SPError_Status

    Input_Char

    -2

    -4

    Passing Parameters by Value