Lab 3 Pseudoinstructions Version 10 Date September 26
; ----------------------------------------
; Lab 3 - Pseudo-instructions
; Version 1.0
; Date: September 26, 2016
; Written By : Ricky Chhun
; Lab Hours : Monday 10:00am - 12:45pm
; ----------------------------------------
.INCLUDE
.DEF var_A = r16
.DSEG
room: .BYTE 1
dir: .BYTE 1
turn: .BYTE 1
.CSEG
.ORG 0x0000
RST_VECT:
rjmp reset // jump over IVT, plus INCLUDE code
.ORG 0x0100 // bypass IVT
.INCLUDE \"spi_shield.inc\"
.INCLUDE \"testbench.inc\" // DrawRoom and DrawDirection
reset:
ldi r16,high(RAMEND) // IO[0x3e] = 0x08
ldi r17,low(RAMEND) // IO[0x3d] = 0xff
out SPH,r16 // SP = 0x08FF
out SPL,r17
call InitShield // initialize GPIO Ports and SPI communications
clr spiLEDS // clear discrete LEDs
clr spi7SEG // clear 7-segment display
;Initialize SRM Variables
clr r17 // initalizes r17 to 0 and then stores data from r17 into variable room
sts turn, r17
sts room, r17
ldi r17, 0x03 // loads the hex number 3 into r17 and then stores the 3 into variable dir
sts dir, r17
loop:
call ReadSwitches // read switches into r7
// dir = rightmost two switches (switch & 0x03);
mov r17, switch // move switch (r7) to temporary register r17
cbr r17, 0xFC // mask-out most significant 6 bits
sts dir, r17 // save formatted value to SRAM variable dir.
/* Read Switches and update room and direction */
// room = leftmost four switches (switch >> 4);
mov r17, switch // move switch (r7) to temp register r17
cbr r17, 0x0F // mask-out least significant nibble
swap r17 // swap nibbles
sts room, r17 // save formatted value to SRAM variable room.
// turn = remaining two switches {(switch >>2)} & 0x03};
mov r17, switch // move turn to temp register r17
cbr r17, 0xF3 // mask-out all switches beside 2 and 3
lsr r17 // shift switches to the right
lsr r17 // shift switches to right once more so they are at the end
sts turn, r17 // save the value to variable turn
; Direction Finder
lds r24, dir // load direction bear is facing into r24
lds r22, turn // load direction bear is to turn into r22
rcall WhichWay // change direction based on variable turn
sts dir, r24 // save formatted value to SRAM variable dir.
rcall TestHitWall
rcall TestRightPaw
rcall TestLeftPaw
/* Draw Direction */
lds r24, dir // calling argument dir is placed in r24.
rcall DrawDirection // translate direction to 7 segment bit
mov spi7SEG, r24 // Displays DrawDirection on the 7 segment display.
call WriteDisplay
/* Room Builder */
lds r24, room // calling argument room is placed in r24.
rcall DrawRoom // translate room to 7-seg bits
mov spi7SEG, r24 // return value, the room, is saved to 7 segment display register
call WriteDisplay // display the room
rjmp loop
; -------------------------------------
; -------- Pseudo Instructions --------
; --------------------------
; ------- Turn Left --------
; Called from WhichWay subroutine (see Table 5.1)
; The input and output is register r24
; register SREG is modified by this subroutine
TurnLeft:
cpi r24, north // Is the bear facing north?
brne check_east
ldi r24, west // yes, turning left, the bear
rjmp endTurnLeft // is now facing west.
check_east: // no
cpi r24, east // Is the beat facing east?
brne check_south
ldi r24, north // yes, turning left, the bear
rjmp endTurnLeft // is now facing north
check_south: // no
cpi r24, south // Is the bear facing south?
brne facing_west
ldi r24, east // yes, turning left, the bear
rjmp endTurnLeft // is now facing east.
facing_west: // no, the bear must be facing west
ldi r24, south // turning left, the bear
endTurnLeft: // is now facing south.
ret
; --------------------------
; ------- Turn Right -------
; Called from WhichWay subroutine (see Table 5.1)
; The input and output is register r24
; register SREG is modified by this subroutine
TurnRight:
cpi r24, north // Is the bear facing north?
brne check_westR
ldi r24, east // yes, turning right, the bear
rjmp endTurnRight // is now facing east.
check_westR: // no
cpi r24, west // Is the bear facing west?
brne check_southR
ldi r24, north // yes, turning right, the bear
rjmp endTurnRight // is now facing north.
check_southR: // no
cpi r24, south // Is the bear facing south?
brne facing_eastR
ldi r24, west // yes, turning right, the bear
rjmp endTurnRight // is now facing west.
facing_eastR: // no, the bear must be facing east
ldi r24, south // turning right, the bear
endTurnRight: // is now facing south.
ret
; --------------------------
; ------- Turn Around -------
; Called from WhichWay subroutine (see Table 5.1)
; The input and output is register r24
; register SREG is modified by this subroutine
TurnAround:
cpi r24, north // Is the bear facing north?
brne check_eastA
ldi r24, south // yes, turning around, the bear
rjmp endTurnAround // is now facing south.
check_eastA: // no
cpi r24, east // Is the bear facing east?
brne check_southA
ldi r24, west // yes, turning around, the bear
rjmp endTurnAround // is now facing west
check_southA: // no
cpi r24, south // Is the bear facing south?
brne facing_westA
ldi r24, north // yes, turning around, the bear
rjmp endTurnAround // is now facing north.
facing_westA: // no, the bear must be facing west
ldi r24, east // turning around, the bear
endTurnAround: // is now facing east.
ret
WhichWay:
bst r22, 1 // store bit 1 into SREG T bit
brts cond_1x // branch if T is set
// case 0x
bst r22, 0 // store bit 0 into SREG T bit
brts cond_01 // branch if T is set
cond_00:
rjmp whichEnd
cond_01:
rcall TurnRight
rjmp whichEnd
cond_1X:
bst r22, 0
brts cond_11
cond_10:
rcall TurnLeft
rjmp whichEnd
cond_11:
rcall TurnAround
rjmp whichEnd
whichEnd:
ret
; --------------------------------------
; ----------- Pseudo Instructions ---------
; ------------------------
;-- Did you hit a wall? --_
; Called from WhichWay subroutine
; Inputs: r22 (room), r42 (dir)
; Output: C++ return register (r24)
; If the answer is no, r24 is set to zero,
; Else the answer is yes
; No registers are modified, while SREG is modified by this subroutine
; -------------------------
HitWall:
push r16
rcall DrawDirection // direction segment bits are now in r16
mov r16, r24 // room segment bits are now in r24
mov r24, r22
rcall DrawRoom
// return drawDirection(dir) & drawRoom(room);
pop r16
ret
; -----------------------
; --------------------------------------
; ----------- Test Subroutines ---------
; ---------------------------
; -------- Test HitWall -----
; Called from main program
; Input: none Outputs: spiLEDs bits 1 and 0 only
TestHitWall:
lds r22, room
lds r24, dir
mov r16, spiLEDs
rcall HitWall
tst r24
breq noWall
sbr r16, 0b00100000 // set hit wall LED
cbr r16, 0b00010000 // sequence to true
rjmp overTheWall
noWall:
sbr r16, 0b00010000
cbr r16, 0b00100000
overTheWall:
mov spiLEDs, r16
ret
; --------------------------------------
; ----------- Pseudo Instructions ---------
; ------------------------
;-- right paw touch a wall? --_
; Called from WhichWay subroutine
; Inputs: r22 (room), r42 (dir)
; If the answer is no, the return value is set to zero,
; Else the answer is yes
; No registers are modified, while SREG is modified by this subroutine
; -------------------------
RightPaw:
rcall TurnRight
rcall HitWall
ret
; ------------------------
;-- left paw touch a wall? --_
; Called from WhichWay subroutine
; Inputs: r22 (room), r42 (dir)
; If the answer is no, the return value is set to zero,
; Else the answer is yes
; No registers are modified, while SREG is modified by this subroutine
; -------------------------
LeftPaw:
rcall TurnLeft
rcall HitWall
ret
; ----------------------
TestRightPaw:
lds r22, room
lds r24, dir
mov r16, spiLEDs
rcall RightPaw
tst r24
breq noWall
sbr r16, 0b00000010 // set hit wall LED
cbr r16, 0b00000001 // sequence to true
rjmp overTheWall
noWallR:
sbr r16, 0b00000001
cbr r16, 0b00000010
overTheWallR:
mov spiLEDs, r16
ret
; ----------------------
TestLeftPaw:
lds r22, room
lds r24, dir
mov r16, spiLEDs
rcall HitWall
tst r24
breq LeftPaw
sbr r16, 0b00001000 // set hit wall LED
cbr r16, 0b00000100 // sequence to true
rjmp overTheWall
noWallL:
sbr r16, 0b00000100
cbr r16, 0b00101000
overTheWallL:
mov spiLEDs, r16
ret
Solution
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\">
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:id=\"&&+id/linearLayout1\"
android:layout_marginLeft=\"10pt\"
android:layout_marginRight=\"10pt\"
android:layout_marginTop=\"3pt\">
<EditText
android:layout_weight=\"1\"
android:layout_height=\"wrap_content\"
android:layout_marginRight=\"5pt\"
android:id=\"&&+id/etNum1\"
android:layout_width=\"match_parent\"
android:inputType=\"numberDecimal\">
</EditText>
<EditText
android:layout_height=\"wrap_content\"
android:layout_weight=\"1\"
android:layout_marginLeft=\"5pt\"
android:id=\"&&+id/etNum2\"
android:layout_width=\"match_parent\"
android:inputType=\"numberDecimal\">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:id=\"&&+id/linearLayout2\"
android:layout_marginTop=\"3pt\"
android:layout_marginLeft=\"5pt\"
android:layout_marginRight=\"5pt\">
<Button
android:layout_height=\"wrap_content\"
android:layout_width=\"match_parent\"
android:layout_weight=\"1\"
android:text=\"+\"
android:textSize=\"8pt\"
android:id=\"&&+id/btnAdd\">
</Button>
<Button
android:layout_height=\"wrap_content\"
android:layout_width=\"match_parent\"
android:layout_weight=\"1\"
android:text=\"-\"
android:textSize=\"8pt\"
android:id=\"&&+id/btnSub\">
</Button>
<Button
android:layout_height=\"wrap_content\"
android:layout_width=\"match_parent\"
android:layout_weight=\"1\"
android:text=\"*\"
android:textSize=\"8pt\"
android:id=\"&&+id/btnMult\">
</Button>
<Button
android:layout_height=\"wrap_content\"
android:layout_width=\"match_parent\"
android:layout_weight=\"1\"
android:text=\"/\"
android:textSize=\"8pt\"
android:id=\"&&+id/btnDiv\">
</Button>
</LinearLayout>
<TextView
android:layout_height=\"wrap_content\"
android:layout_width=\"match_parent\"
android:layout_marginLeft=\"5pt\"
android:layout_marginRight=\"5pt\"
android:textSize=\"12pt\"
android:layout_marginTop=\"3pt\"
android:id=\"&&+id/tvResult\"
android:gravity=\"center_horizontal\">
</TextView>
</LinearLayout>






