1 / 25

ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs

ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs. Lecture #7 Agenda Today Decoding Machine Code Data Direction Registers Logical Instructions (AND, OR, EOR) Arithmetic Instructions (ADD, SUB, MUL, IDIV) More Instruction Execution and Timing.

Download Presentation

ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 3430 – Introduction to Microcomputer SystemsUniversity of Colorado at Colorado Springs Lecture #7 Agenda Today • Decoding Machine Code • Data Direction Registers • Logical Instructions (AND, OR, EOR) • Arithmetic Instructions (ADD, SUB, MUL, IDIV) • More Instruction Execution and Timing ECE 3430 – Intro to Microcomputer Systems Fall 2009

  2. Decoding Machine Code Decoding Machine Code - The process of creating the assembly language (including mnemonics) for the opcodes and operands scattered throughout program memory (the machine code). - If we know the contents of the program memory, we can reverse-engineer the code to assembly level. - There is a single mapping from machine code to assembly code and vice-versa. - FYI: It is not always possible to reverse-engineer machine code to a source code form more abstract than assembly (i.e. to the C or C++ source code level). Information is combined and dismissed during the “higher level language” to assembly translation. There can be an infinite number of ways to write higher level code that ultimately produces the same machine code. Debuggers for these higher level languages rely on other constructs (such as symbol files) to map a particular machine instruction back to a single line of source code. ECE 3430 – Intro to Microcomputer Systems Fall 2009

  3. Decoding Machine Code *.asm file Program Memory Program Execution: • The opcode byte(s) contain all the information about size and addressing mode of the instruction. • The first opcode inherently tells you where the next opcode is stored in memory—and so on. • Just as assemblers can assemble your assembly code into machine code, inverse-assemblers can take machine code and produce the original assembly code. • Why would you want to do this? 1) To verify that the cross-assembler did what you expected. 2) If the address & data busses are “probe-able”, you can use a logic analyzer to track the program and find our where the programs are and what they are doing (nicer logic analyzers have “built-in” inverse assemblers for many common CPUs). • . • The pink book tells you what the opcode is for a given mnemonic and addressing mode. • The pink book also tells you what the mnemonic and addressing mode is for a given opcode. cross-assembler LDAA #$FF $86 $FF … *.asm file LDAA #$FF inverse-assembler (machine code decoding) ECE 3430 – Intro to Microcomputer Systems Fall 2009

  4. Decoding Machine Code Decode the following: $96, $30, $8B, $071) First we look up $96 in the pink book. We know that it is an opcode because nothing precedes it. From here on out however, we must keep track of where instructions begin and end. $96 = LDAA DIR2) We see that there is one byte of operand because $96 uses direct addressing. This means the next byte is the operand for LDAA ($30).The first instruction is LDAA $30. If the opcode was not found in the table, then it may be a multi-byte opcode (as is used by LDY for example). You then have to consider the next byte as well when decoding the instruction. 3) Since the first instruction is complete, the next byte is the opcode for the next instruction. So we look it up in the pink book: $8B = ADDA IMM Since ACCA is 8-bits wide, the value added to it is 8-bits. Hence, $07 is the only operand for the instruction. The second instruction is ADDA #$07.4) The complete code for this block of raw memory is: LDAA $30 ADDA #$07 ECE 3430 – Intro to Microcomputer Systems Fall 2009

  5. Data Direction Registers • The HC11 has bi-directional ports, so how do we change them from an input to an output? • There are configuration registers that handle this. Each of the following registers has a designated memory address mapped into the internal register block of the HC11 (these can be found in the pink book too): DDRB ($0006) = data direction for port B DDRC ($0007) = data direction for port C DDRD ($0009) = data direction for port D PACTL ($0026)= data direction for PA3 and PA7 (may be typo in older books) • Writing a ‘0’ to these registers turns that pin into an input(reset). • Writing a ‘1’ to these registers turns that pin into an output. ECE 3430 – Intro to Microcomputer Systems Fall 2009

  6. Bit-Wise Manipulation of Memory The HC11 is a byte-addressable architecture. This means that each address targets a byte stored in memory. So what if you want to change one bit in memory (without effecting the other bits in that byte)? Apply boolean logic: AND: Force a bit to zero or no change. OR: Force a bit to one or no change. EOR: Toggle a bit to opposite state or no change. ECE 3430 – Intro to Microcomputer Systems Fall 2009

  7. Bit-Wise Manipulation of Memory The AND, OR, and EOR instructions conceptually form a logic gate between each corresponding bit of the operand and the value already stored in the accumulator. These logical operations can only be performed on accumulator A and B in the HC11. ANDA operand  bitwise AND Ex) (ACCA) 7 6 5 4 3 2 1 0 ANDB operand M() 7 6 5 4 3 2 1 0 x x x x x x x x ORAA operand  bitwise OR ORAB operand EORA operand bitwise exclusive OR EORB operand Logic Gate 8 of these ECE 3430 – Intro to Microcomputer Systems Fall 2009

  8. Bit-Wise Manipulation of Memory These logical instructions are useful for setting, clearing, or toggling bits within a byte:OR w/ 1 to set LDAA $00 ORAA #%00010000 ; will force PA4 to ‘1’ STAA $00AND w/ 0 to clear LDAA $00 ANDA #%11101111 ; will force PA4 to ‘0’ STAA $00 EOR w/ 1 to toggle LDAA $00 EORA #%00010000 ; will toggle the state ; of PA4 STAA $00 ECE 3430 – Intro to Microcomputer Systems Fall 2009

  9. Arithmetic Instructions: The ADD Instruction ADD – A general name for a set of HC11 addition instructions. The destination “address” must be a register or accumulator. Example add instructions: ADDA: A + M  A (4 addressing modes for M) ADDB: B + M  B (4 addressing modes for M) ADDD: D + M:M+1  D (4 addressing modes for M) ADCA: A + M + C  A (4 addressing modes for M) ADCB: B + M + C  B (4 addressing modes for M) ABA: A + B  A (inherent addressing) ABX: IX + 00:B  X (inherent addressing) ABY: IY + 00:B  Y (inherent addressing) ECE 3430 – Intro to Microcomputer Systems Fall 2009

  10. Arithmetic Instructions: The ADD Instruction ADD Instruction Execution –Ex) ADDA $10 = $9B, $10 (2 bytes, 3 clock cycles)Step 1: PC is put on the address bus ($E000) and a READ is initiated. The data ($9B) is put on the data bus by the memory and grabbed by the CPU. The control unit decodes the instruction and recognizes that it is an ADDA using direct addressing. The PC is incremented (PC = PC+ 1 = $E001).Step 2: PC is put on the address bus ($E001) and a READ is initiated. The data ($10) is put on the data bus and grabbed by the CPU. The PC is incremented (PC = PC + 1 = $E002).Step 3: The DIRECT address ($0010) is put on the address bus and the data at that address M($0010) is put on the data bus. This value is added to ACCA and the result is left in ACCA. Memory $0010 data - - $E000 $9B $E001 $10 $E002 - ECE 3430 – Intro to Microcomputer Systems Fall 2009

  11. Arithmetic Instructions: The ADD Instruction Write code to add M($0010) and M($0011) and store in ACCA –LDAA $10 ; ACCA = M($0010) using DIRECT addressingLDAB $11 ; ACCB = M($0011) using DIRECT addressing ABA ; ACCA = ACCA + ACCB using INHERENT addressing STAA $0040 ; store result in $0040 (RAM on the MicroStamp) The above would add the contents of $0010 and $0011 together. The sum resides in ACCA until we store the result out to memory. In this case the result is stored in memory location $0040. ECE 3430 – Intro to Microcomputer Systems Fall 2009

  12. Arithmetic Instructions: The SUB Instruction SUB –A general name for a set of HC11 instructions performing subtraction. Again, the destination “address” must be a register or accumulator. Example subtract instructions: SUBA: A – M  A (4 addressing modes for M) SUBB: B – M  B (4 addressing modes for M) SUBD: D – M:M+1  D (4 addressing modes for M) SBCA: A – M – C  A (4 addressing modes for M) SBCB: B – M – C  B (4 addressing modes for M) SBA: A – B  A (inherent addressing) ECE 3430 – Intro to Microcomputer Systems Fall 2009

  13. Arithmetic Instructions: The SUB Instruction Ex) SBA ; A = A-B (INH) SUBA #$10 ; A = A - $10 (IMM) SUBA $10 ; A = A – M($10) (DIR) SBCA $10 ; A = A – M($10) – C (DIR) What happens if the result is negative?The “N” bit in the CCR is set. What happens if two’s compliment overflow occurs?The “V” bit in the CCR is set. ECE 3430 – Intro to Microcomputer Systems Fall 2009

  14. Arithmetic Instructions: The MUL Instruction The MUL instruction uses inherent addressing. It does an 8-bit, unsigned multiplication of the contents in ACCA and ACCB and stores the 16-bit, unsigned result in ACCD: A x B  D Ex: Multiply the contents of $0042 by 5 and store back to $0043: LDAA $0042 LDAB #5 MUL STD $0043 ECE 3430 – Intro to Microcomputer Systems Fall 2009

  15. Arithmetic Instructions: The IDIV Instruction The IDIV instruction uses inherent addressing. It does a 16-bit, unsigned integer divide using the contents of index register X and ACCD and produces a 16-bit quotient and 16-bit remainder: D / IX : 16-bit quotient  IX, 16-bit remainder  D Ex: Divide the contents of $0042 by 5 and store quotient to $0044 and remainder to $0046: LDD $0042 LDX #5 IDIV STX $0044 STD $0046 There is also an FDIV instruction which stands for fractional divide (not floating divide). The HC11 has no floating point support. ECE 3430 – Intro to Microcomputer Systems Fall 2009

  16. Instruction Execution/Timing Timing- An electronic computer is a synchronous system.- This means that all circuitry is synchronous to the ‘1’ system clock (memory, CPU, I/O).- The system clock is derived from a crystal oscillator (XTAL Clock)- The system clock (E-clock) is the XTAL clock divided by 4 E-clock = XTAL Frequency 4- Our System (MicroStamp): - XTAL = 8.389 MHz - E-clock = 2.1 MHz - Cycle Time = 1/(E-clock freq) = 477nsOther common XTAL frequencies: 8.0 MHz, 4.0 MHz Ex) How long does the instruction “LDAA $FF” take to execute? - First we recognize that we are using direct addressing - Second, we go to the pink book to find out how many cycles the instruction takes (LDAA DIR = 3 clocks)Execution Time = (# of cycles)(execution time per clock) = (3)(477ns) = 1431 ns May be 8.0 MHz on MicroStamp! ECE 3430 – Intro to Microcomputer Systems Fall 2009

  17. Instruction Execution/Timing Instruction Execution: STAA $20 (DIR = 3 clock cycles) (machine = $97, $20)Step 1 - READ Opcode from PC location - PC = $E000 is put on the address bus from the PC - $97 is output from memory onto the data bus, CPU receives $97 - PC is incremented ($E001)Step 2 - READ Operand from PC location - PC = $E001 is put on the address bus from the PC - $20 is output from memory onto the data bus, CPU receivers $20 - PC is incremented ($E002)Step 3 - $0020 is output to address bus from CPU - ACCA() is put onto the data bus - a WRITE is performed - the data that in in ACCA is stored into memory at $0020Timing - READ Opcode = 1 clock cycle Total = 3 cycles - READ Operand = 1 clock cycle Time = (3)(477ns) = 1.43us - WRITE data = 1 clock cycle ECE 3430 – Intro to Microcomputer Systems Fall 2009

  18. Code Execution How long does the following instruction take (i.e., execution time)?XTAL = 8MHzSUBD $1020 • First find the time it takes for 1 cycle: E-clock = XTAL/4 = 2MHz Time/cycle = 1/(2MHz) = 500ns • Next find the # of cycles this instruction takes from the pink book: SUBD EXT = 6 cycles • Multiply (# of cycles)(time/cycle) = (6)(500ns) = 3us ECE 3430 – Intro to Microcomputer Systems Fall 2009

  19. Code Execution Add the contents of M($0040) and M($0041) and store result in M($0042):LDAA $40 ; ACCA = M($0040)LDAB $41 ; ACCB = M($0041)ABA ; ACCA = ACCA + ACCBSTAA $42 ; M($0042) = ACCA 1) How much memory does this code take?LDAA $40 ; 2 bytes LDAB $41 ; 2 bytes ABA ; 1 byte STAA $42 ; 2 bytes 7 bytes 2) How long does this code take?LDAA $40 ; 3 cycles XTAL = 8MHzLDAB $41 ; 3 cycles E-clock = 8MHz/4 = 2 MHzABA ; 2 cycles Time/cycle = 1/E-clock = 500nsSTAA $42 ; 3 cycles 11 cycles (11)(500ns) = 5.5us ECE 3430 – Intro to Microcomputer Systems Fall 2009

  20. Code Execution Can we do it better?LDAA $40 ; ACCA = M($40)ADDA $41 ; A = A + M($41) STAA $42 ; M($42) = ACCA 1) How much memory does this code take?LDAA $40 ; 2 bytes ADDA $41 ; 2 bytes STAA $42 ; 2 bytes 6 bytes 2) How long does this code take?LDAA $40 ; 3 cycles XTAL = 8MHzADDA $41 ; 3 cycles E-clock = 8MHz/4 = 2 MHzSTAA $42 ; 3 cycles Time/cycle = 1/E-clock = 500ns 9 cycles (9)(500ns) = 4.5us ECE 3430 – Intro to Microcomputer Systems Fall 2009

  21. Code Execution What is the Improvement?Size: (7-6) x 100 = 14.3%7Speed: (5.5 – 4.5) x 100 = 18.2% 5.5 A small change in how we chose to implement the code resulted in an improvement in both time and space. If the code were executed thousands of times, the size and speed improvements would become very apparent (if executed in a loop for example)! ECE 3430 – Intro to Microcomputer Systems Fall 2009

  22. Code Execution How many reads and writes were performed?LDAA DIR : 3 cycles : READ Opcode : READ Operand : READ M(DIR)ADDA DIR : 3 cycles : READ Opcode : READ Operand : READ M(DIR)STAA DIR : 3 cycles : READ Opcode : READ Operand : WRITE M(DIR) Total: 8 reads 1 write ECE 3430 – Intro to Microcomputer Systems Fall 2009

  23. Code Execution What have we forgotten? The carry bit. We are not taking into account if there was a carry out. You have to write your code to account for this if you care about it (multi-precision arithmetic). Is this operation signed or unsigned?Doesn’t matter. Addition and subtraction works for both. How the rest of the program uses the results of the add/subtract operation differs depending on whether you are dealing with signed or unsigned values (it is all in how you interpret the binary). ECE 3430 – Intro to Microcomputer Systems Fall 2009

  24. Code Execution Where is the program counter after the code is executed? ANSWER: $E006, that is the location of the opcode for the next instruction. Memory $E000 $96 LDAA $40 $E001 $40 - $E002 $9B ADDA $41 $E003 $41 - $E004 $97 STAA $42 $E005 $42 - $E006 - ECE 3430 – Intro to Microcomputer Systems Fall 2009

  25. End of Exam 1 Material This is the end of the exam 1 material. Prepare questions for next lecture. ECE 3430 – Intro to Microcomputer Systems Fall 2009

More Related