CS273

    Quiz 9 Solution

Put your social security number in the right hand corner of each page. Please do not put your name anywhere on this quiz. Failure to follow these instructions will cost you 2 points on the quiz.

Note: Be sure to show all your work You will have until the end of class to complete the quiz.


(20 points)

  1. Given the following available main program, write the assembly language code on the next page for the procedure motorson. The code you write should match the specifications at the top of the routine: NOTE: You may not use immediate addressing within the procedure motorson to specify the enabling and direction of a motor; use the equ directives available in the main program. For this program, your car only has two motors; you may take that as a given. HINT: Remember your andX, oraX, bset and bclr instructions.

RAM     equ  $0000
STACK   equ  $00ff
DEVICES equ  $1000
EEPROM  equ  $f800
RESET   equ  $fffe

REVERSE equ  0          * motor direction reverse
FORWARD equ  1          * motor direction forward
LEFT    equ  1          * left motor
RIGHT   equ  2          * right motor
MOTORS  equ  $04
MOT1EN  equ  %00010000  * motor 1 enabled
MOT2EN  equ  %00100000  * motor 2 enabled
MOT1DIR equ  %00000001  * motor 1 forward
MOT2DIR equ  %00000010  * motor 2 forward

        org  EEPROM 
start   lds  #STACK 
        clra
        psha
        psha
        jsr  motorson
        ins
        ins 
        ldaa #FORWARD   * give direction for motor
        psha
        ldaa #LEFT      * enable left motor
        psha
        jsr  motorson
        ins
        ins 
        ldaa #REVERSE   * give direction for motor
        psha
        ldaa #RIGHT     * enable right motor
        psha
        jsr  motorson 
        ins
        ins 
dead    bra  dead

* 
* Procedure motorson
*
* Inputs: 
*  motoren - the number of the motor to enable
*            NOTE: for motor number 0 - all motors are turned off;
*            for motor number 3 - all motors are turned on.
*
*  motordir - the direction of the motor being enabled.
*             0 - indicates the motor is in reverse
*             1 - indicates the motor is in forward.
*
motorson
        tsx
        ldy  #DEVICES
        ldab MOTORS,y
        ldaa 2,x
        beq  moff             * all motors to be turned off
        cmpa #3
        bne checklr
        orab #MOT1EN|MOT2EN   * all motors on
        ldaa 3,x
        beq  revall
        orab #MOT1DIR|MOT2DIR * all motors forward
        bra  savem
revall  stab MOTORS,y
        bclr MOTORS,y #MOT1DIR|MOT2DIR  * all motors reversed
        bra  leave
checklr cmpa #LEFT
        bne checkrt
        orab #MOT1EN          * left motor on
        ldaa 3,x
        beq  revleft
        orab #MOT1DIR         * left motor forward
        bra  savem
revleft stab MOTORS,y
        bclr MOTORS,y #MOT1DIR  * left motor reversed
        bra  leave
checkrt cmpa #RIGHT
        bne  moff             * ERROR so turn motors off
        orab #MOT2EN          * right motor on
        ldaa 3,x
        beq  revrt
        orab #MOT2DIR         * right motor forward
        bra  savem
revrt   stab MOTORS,y
        bclr MOTORS,y #MOT2DIR  * right motor reversed
        bra  leave
moff    bclr MOTORS,y #MOT1EN|MOT2EN * all motors off
        bra  leave
savem   stab MOTORS,y
leave   rts

        org  RESET
        fdb  start

  1. Hand execute the program. Visually what will a car be doing if this program is executed on its board?

    Turning right.