Coe538 Study Guide 1 of 8 Coe538 Final Study Guide 2016 (Questions & Answers) This version contains questions AND answers. This study guide is meant to help you review coe538 and prepare for the final. How to use this study guide. The Guide has 2 versions: one with answers; another without. Start with the version without answers. Each question indicates how many minutes should be needed to answer it. Read a question. If you think you need The questions WITH ANSWERS 1. (10 minutes) Write an assembly language fragment that will make the contents of address 3 contain the value 5. a) Do this with 2 assembly languages instructions and determine the number of clock cycles required and the total size (in bytes) of the program fragment. b) Do this with a single assembly instruction. How many clock cycles? How many bytes? a) ldaa #5 ;machine language: 86 05 (2 bytes) (1 clock cycle) staa 3 ;machine language: 7a 00 03 (3 bytes) (3 clock cycles) ; TOTAL: Size: 5 bytes; Clock cycles: 4 b) movb #5,3 ;Machine language: 18 0b 05 00 03 (5 bytes, 4 clock cycles) REMARKS: If you assemble this code with Codewarrior, it will use Direct addressing instead of Extended addressing for memory locations in page zero (0-0xff). This will make the instructions 1 byte shorted. However, we do not use the Direct addressing mode in this course. The only addressing modes you need to know are: PC-relative Indexed (including pre/post increment/decrement) Extended Immediate Indexed Indirect
Coe538 Study Guide 2 of 8 2. (10 minutes) Write an assembly language fragment that will make the contents of address 3 contain the value stored in address 5. (Use as many instructions as you wish. Calculate the running time and size of the assembly language fragment.) ldab 5 ;machine language: f6 00 05 (3 bytes) (3 clock cycle) stab 3 ;machine language: 7b 00 03 (3 bytes) (3 clock cycles) ; TOTAL: Size: 6 bytes; Clock cycles: 6 ;You could, of course, use Acc A or a single movb instruction. 3. (5 minutes) Write a C language fragment that makes the contents of address 0x0003 be 5. *(char *) 3 = 5; 4. (5 minutes) Write a C language fragment that makes the contents of address 0x0003 the same as the contents of address 0x0005. *(char *) 3 = *(char *)5; 5. (8 minutes) Write assembly language such that the 2 most significant bits of AccA are set to 1, the next 2 bits are set to 0 and the 4 least significant bits are inverted. (For example, if AccA contains 10100101, it would become 11001010.) ora #$f0 eora #$3f ;It could also be done as follows (less efficiently) ora #$c0 anda #$cf eora #$0f 6. (5 minutes) Repeat the previous question in C assuming the byte you wish to modify is a variable called p. p = (p 0xf0) ^ 0x3f; //It could also be written as: p = (p & 0x0f) ^ 0xcf;
Coe538 Study Guide 3 of 8 7. Write a Timer module to generate an interrupt every millisecond. The total number of milliseconds should be maintained in a global variable called time that maintains a 4-digit count of milliseconds elapsed in packed bcd format. (Use output compare channel 2.) Your code should have an inittimer subroutine that initializes the timer and a TimerISR interrupt service routine. Assume that the interrupt vector is at 0xffea. Assume that a 10 MHz clock drives the timer module. Assume the module base is 0x0040. ROMStart EQU $4000 ; absolute address to place my code/constant data TIOS equ $40 ;Timer Input Capture/Output Compare Select TCTRL1 equ $46 ;Timer Control Register 1 TCNT equ $44 ;16-bit main Timer counter TIE equ $4C ;Timer Interrupt Enable Register TFLG1 equ $4E ;Timer Flag Register 1 TC2 equ $54 ;Output Compare Register Channel 2 ORG $400 time ds.b 2 ;4-digit bcd counter ; code section ORG ROMStart Entry: _Startup: lds #$800 ; initialize the stack pointer ;initialize "time" (2 bytes) to zero clr time clr time+1 jsr inittimer cli ; enable interrupts bra * ;inifinte loop inittimer: ;Configure Channel 2 as Output Compare
Coe538 Study Guide 4 of 8 ldaa #%100 staa TIOS ;Turn on the Timer module ldaa #$80 staa TCTRL1 ;Enable Channel 2 interrupts ldaa #4 staa TIE ;Set OutputCompare2 to TCNT+10000 ldd TCNT addd #10000 std TC2 rts TimerISR: ;Clear Flag ldaa #4 staa TFLG1 ;Set OutputCompare2 10000 cyles later ldd TC2 addd #10000 std TC2 ;increment bcd counter (time) ldaa time+1 adda #1 daa staa time+1 ldaa time adca #0 daa staa time rti ;************************************************************** ;* Interrupt Vectors * ;************************************************************** org $ffea dc.w TimerISR ORG $FFFE DC.W Entry ; Reset Vector
Coe538 Study Guide 5 of 8 8. (25 minutes) A function counts the number of times a given character appears in a nullterminated string. For example, the string "qwartaay" contains the character 'a' 3 times. The function could be described in C as follows: unsigned char char_count_in_string(char * str, char ch) { unsigned char count = 0; while(*str) { if(*(str++) == ch) { count++; } } return count; } The documentation for an assembly language subroutine to perform this function follows. Complete the code. ;Subroutine: CountCharInString ;Description: Counts the instances of the character specified ; in Acc A that occurs in the null-terminated string ; whose starting address is specified by register X. ;Parmameters: ; AccA: the character to count ; X: Starting address of null terminated string to search. ;Returns: ; The number of matching characters as an unsigned number ; in Acc. B. ;Side Effects: ; All registers (except for CCR and B) have the same ; values when the subroutine returns. CountCharInString: clrb ;count mainained in AccB, initialed to 0 pshx ;save X so it can be restored continue cmpa 0,x ; bne notin incb notin tst 1,x+ bne continue ;nul char (end of string) reached?
Coe538 Study Guide 6 of 8 pulx rts 9. Translate the C function to assembler using the standard parameter passing conventions used by the C compiler. (Assume that the result is returned in AccB.) ;Subroutine: CountCharInString ;Description: Counts the instances of the character specified ; as parameter on stack (offset +7) ; whose starting address is specified by ; parameter on stack (offset +5). ;Parmameters: ; ch: the character to count (7,sp) ; astring: Starting address of null terminated ; string to search. (5,sp) ;Returns: ; The number of matching characters as an unsigned number ; in Acc. B. ;Side Effects: ; All registers (except for CCR & B) have the same values ; when the subroutine returns. CountCharInString: ch equ 7 ;offset in Stack to ch parameter astring equ 5 ;offset in Stack to astring parameter pshx psha clrb ldx astring,sp ldaa ch,sp continue cmpa 0,x bne notin incb notin tst 1,x+ bne continue pula pulx rts 10. How can you add Accumulator A to Accumulator D? Write the instruction(s). (Assume unsigned integers in both A and D.) ;One possible way
Coe538 Study Guide 7 of 8 staa tmp +1 ;tmp is a global 16-bit (2 byte) variable clr tmp addd tmp ;tmp now contains a 2-byte integer with msbyte zero and lsbyte = AccA 11. Translate the C function to assembler using the standard parameter passing conventions used by the C compiler. (Assume that the result is returned in AccB.) ;Subroutine: CountCharInString ;Description: Counts the instances of the character specified ; as parameter on stack (offset +7) ; whose starting address is specified by ; parameter on stack (offset +5). ;Parmameters: ; ch: the character to count (7,sp) ; astring: Starting address of null terminated ; string to search. (5,sp) ;Returns: ; The number of matching characters as an unsigned number ; in Acc. B. ;Side Effects: ; All registers (except for CCR and B) have the same values ; when the subroutine returns. CountCharInString: pshx psha clrb ldx 5,sp ldaa 7,sp continue cmpa 0,x bne notin incb notin tst 1,x+ bne continue pula pulx rts 9. Consider the 3 common methods of serial communication: SCI, SPI and I²C. Suppose two
Coe538 Study Guide 8 of 8 devices are connected for bi-directional communicaation using each of these protocols. Apart from ground and power, how many wires connect the two devices using each of the protocols? Rank the protocols from fastest to slowest. Which protocols (if any) allow bidirectional communication between more than 2 devices using a single bus? SCI: 2 wires (Tx and Rx); SPI: 3 wires (MISO, MOSI, SS); IIC: 2 wires (SCK, SDA) Fastest: SPI; Medium: IIC; Slowest: SCI Only SPI and IIC (which are bus protocols). SCI can only ben used between 2 devices: a DCE and DTE) 1. Determine the machine language and the number of clock cycles needed to execute each of the instructions below: ldaa #6 ldaa $1234 ldaa 0,x 4023 8606 ldaa #6 ;1 cycle 4025 B612 34 ldaa $1234 ;3 cycles 4028 A600 ldaa 0,x ;3 cycles