LECTURE #21: G-CPU & Assembly Code EEL 3701: Digital Logic and Computer Systems Based on lecture notes by Dr. Eric M. Schwartz G-CPU Important Notes (see Schwartz s lecture for a general overview) - The G-CPU is little endian, meaning the LSB is stored and loaded before the MSB in a 16-bit word (as opposed to big endian). - The conditional code register (CCR) contains a negative (N) flag and a zero (Z) flag, which are only dependent on register A. - The memory used in the G-CPU is synchronous. For this reason, a memory clock (MCLK) is used to drive the SRAM and EPROM. Two T flipflops are used in series to create a system clock (CLOCK ) that is 1/4 th the speed of the memory clock. In this way, we can pretend that the SRAM and EPROM are running asynchronously to the system. - The SRAM used in the G-CPU has both input data lines (for writing) and output data lines (for reading). - The exhaustive next state truth table (NSTT) that is stored in the memory for implementing the controller has 2 14 possible states/addresses. - Branch statements (BRA, BEQ, BN, BP) only modify the lowest byte of an address. For this reason, you cannot jump past certain break points (where the ninth bit changes). Assembly Language Format Instruction Set: A list of all the operations that can be performed by a μprocessor. General format for an instruction (on the MC68HC11): Label: Operation-Mnemonic Operand(s) ;Comments - A label is a symbolic name for the address of an instruction. - A label is not required and is generally only used when necessary. - The colon (:) is optional and is not considered part of the label. - Labels are used with branch and jump statements. - Labels can be used to point to a specific address containing data. - An operation-mnemonic is an abbreviation that corresponds to a command. - Note: A mnemonic is a memory aid. - An operand denotes the object to be operated upon. - An operand may represent an address, register, or immediate data. - Comments are used to explain code, making it easier to follow. - The assembler I used in college denoted comments with a semi-colon. (Look at the instruction set for the G-CPU.) Page 1 of 6
Addressing Modes Effective Address: The address of the data operated on by an instruction. - You should be able to find the effective address for: - Immediate Addressing (the address of the operand pointed to by the PC) - Extended Addressing - Direct Addressing - Indexed Addressing - Inherent Addressing - Addressing is inherent to the instruction (usually used with registers). - Examples: TAB INX SUM_BA - Immediate Addressing (denoted by a # symbol) - The operand is the data to be used by the instruction. - Examples: LDAA #$42 LDX #$1B00 (You cannot STAA #$42. Why?) Note: Certain symbols indicate the radix for a number: $ => Hexadecimal @ => Octal % => Binary => ASCII (none) => Decimal - Extended Addressing - The operand gives the address where the data is stored. - Examples: LDAA $1300 STAA $1400 LDX $1301 - Direct Addressing (the G-CPU does not have this type of addressing) - Similar to extended addressing, except: - The upper address byte is assumed to be $00 - Direct addressing only accesses the address range $0000-$00FF - $0000 to $00FF is called the direct page - Direct addressing is faster than extended addressing - Examples: LDAA $A0 STAA $B0 LDX $C0 Note: The assembler knows the difference between extended because only one byte was given for the address. Page 2 of 6
- Indexed Addressing - Gives the address relative to the address in an index register (IX or IY). - The number supplied is a displacement/offset. - Displacements are (positive) unsigned binary, ranging from $00 to $FF. - Examples: LDAA 0,X STAA $2A, Y Note: IX and IY remain unchanged. - Relative Addressing (the G-CPU does not have this type of addressing) - Similar to indexed, but used to change the Program Counter (PC). - Displacements are 2 s complement, ranging from $00 to $FF. - Generally, used with branch statements. - Examples: BEQ $FE ;Branches back 2 addresses BNE $04 ;Branches forward 4 addresses Note: The MC68HC11 has memory-mapped I/O, meaning that its input and output ports are accessed using the same addressing modes as memory (discussed above). Assembler Directives Assembly control: ORG => Sets the origin (starting point) of the program or dataset Example: ORG $1300 Symbol Definition: EQU => Assigns a permanent value to a symbol name. Example: Answer EQU $42 (Note: Answer can be used anywhere the value $42 could be used.) Data Definition/Storage Allocation: DC.B => Define constant byte (allocates and initializes memory) Examples: (label) DC.B $42, $AB, $F0, 64 DC.W => Define constant word (allocates and initializes memory) Example: (label) DC.W $42AB, $F040 DS.B => Define storage bytes (allocates uninitialized memory space) Example: (label) DS.B 4 DS.W => Define storage word (allocates uninitialized memory space) Example: (label) DS.W 2 Page 3 of 6
Example Code Given a location in memory containing a dataset of X consecutive, 8-bit, unsigned binary numbers, write a program that adds the numbers together and stores the result in another portion of memory. The dataset at the given memory location is organized as follows: - X is the first byte and is an unsigned binary number indicating how many numbers must be added together to obtain the result. - Next is the address where the sum should be stored. - Finally, the dataset of consecutive, 8-bit, unsigned binary numbers. (Note: You may ignore problems with overflow/roll-over.) ORG $19FF ;Table in SRAM (Given) DS.B 1 Dataset DC.B 3 ;X DC.W $FF19 ;Address to store the sum (little endian). DC.B $4A, $11, $24 ;Dataset ORG $0000 ;G-CPU always starts at $0000 LDX #Dataset ;IX points to dataset (Dataset address is a given) LDAA 0,X ;RegA gets counter INX LDY 0,X ;IY points to storage location LDAB #$00 STAB 0,Y ;Initialize current sum to zero INX Loop INX STAA Counter ;Save counter LDAA 0,X ;Get next value LDAB 0,Y ;Get current sum SUM_AB ;Add current numbers into RegB STAB 0,Y ;Save current sum LDAB #NegOne LDAA Counter ;Get counter SUM_BA ;Decrement counter BNE Loop ;Repeat until count is zero (all numbers are added) Done BEQ Done ;End program ORG $1000 ;Storage values must be in SRAM (not EPROM) Counter DS.B 1 ;Storage for the counter NegOne EQU $FF ;To decrement, we must add a 2 s complement -1. Note: We wrote the code using indexed addressing so that it is a general program that can be used for any memory location (as opposed to hard coded to a specific address). Page 4 of 6
So what happens next? We created our program in a text editor. This is called our source file. Next we use and assembler to assemble our program, generating 2 files: - The List file is a human readable file containing: - Addressing information - Machine code (only binary/hexadecimal) - Assembly code (the program) - The Object file is the assembled machine code the μprocessor will use. We simulate and debug the program at this point. The final Object file is then downloaded to memory so the μprocessor can run it. Hand Assembly There is no G-CPU assembler, so we need to hand assemble the code. - Use the G-CPU Instruction Set (found in the G-CPU documentation). Addresses Machine Code Labels Assembly Code ORG $19FF $19FF $XX DS.B 1 $1A00 $03 Dataset DC.B 3 $1A01-$1A02 $FF $19 DC.W $FF19 $1A03-$1A05 $4A $11 $24 DC.B $4A, $11, $24 ORG $0000 $0000-$0002 $08 $00 $1A LDX #Dataset $0003-$0004 $0C $00 LDAA 0,X $0005 $30 INX $0006-$0007 $?? $00 LDY 0,X $0008-$0009 $03 $00 LDAB #$00 $000A-$000B $13 $00 STAB 0,Y $000C $30 INX $000D $30 Loop INX $000E-$0010 $06 $00 $10 STAA Counter $0011-$0012 $0C $00 LDAA 0,X $0013-$0014 $0F $00 LDAB 0,Y $0015 $15 SUM_AB $0016-$0017 $13 $00 STAB 0,Y $0018-$0019 $03 $FF LDAB #NegOne $001A-$001B $04 $00 $10 LDAA Counter $001C $14 SUM_BA $001D-$001E $21 $0D BNE Loop $001F-$0020 $20 $1F Done BEQ Done ORG $1000 $1000 $XX Counter DS.B 1 NegOne EQU $FF Note: The G-CPU cannot perform the LDY 0,X operation See next page for solutions. Page 5 of 6
Possible ways to work around the LDY 0,X problem: Option 1: Add a label at $1A01 called Address: LDY Address Option 2: Rearrange the table so that the storage address comes before X. LDY Dataset (You must increment IX twice before retrieving the counter.) Option 3: Define a space to save the address and load IY from there. LDAB 0,X STAB LSB LDAB 1,X STAB MSB LDY LSB... ORG $1001 LSB DS.B 1 MSB DS.B 1 Page 6 of 6