I’m trying to create a program in assembly language for a Microchip PIC microcontroller to simulate a dice game. When PORTA is pressed the dice stops rolling and a number is generated at random each time. Below is the code that I have so far. ; TUTA4.ASM ; NOTE: Uncomment the appropriate section for your target chip below: ; Uncomment the following 3 lines if using a 16F88... LIST P=16F88 __CONFIG H'2007', H'3F72' ; RC mode __CONFIG H'2008', H'3FFC' ; Clock Fail-Safe disabled ; Uncomment the following 2 lines if using a 16F84A... ; LIST P=16F84A ; __CONFIG H'3FF3' ; RC mode ; Uncomment the following 2 lines if using a 16F877A... ; LIST P=16F877A ; __CONFIG H'3F03' ; RC mode #DEFINE PAGE0 BCF STATUS,5 #DEFINE PAGE1 BSF STATUS,5 STATUS EQU H'03' ; name program location 3 as STATUS TRISA EQU H'85' ; PORTA EQU H'05' ; PCL EQU H'02' ; PORTB EQU H'06' ; name program location 6 as PORTB TRISB EQU H'86' ; name program location H'86' as TRISB ANSEL EQU H'9B' ; name program location H'9B' as ANSEL W EQU 0 ; Working register flag F EQU 1 ; File register flag C EQU 0 ; Carry flag Z EQU 2 NUMB EQU H'20' TEMP EQU H'21' SEED EQU H'22' ORG 0 ; Reset vector GOTO 5 ; goto start of program ORG 4 ; Interrupt vector GOTO 5 ; goto start of program ORG 5 ; Start of program memory CLRF PORTB ; set all Port B pins to logic 0 CLRF PORTA PAGE1 ; instruct program that Page 1 commands come next CLRF TRISB ; set all Port B pins as outputs from within Page 1 ; PORT A IS DEFAULTED AS INPUTS WHICH IS FINE FOR THIS PROGRAM PAGE0 ; instruct program that Page 0 commands come next MAIN INCF SEED ; SEE BELOW BTFSS PORTA,0 ; START RUNNING WHEN PA0 is pressed ; INC a 'seeding' variable which simply GOTO MAIN ; 'runs constantly' ; This then 'seeds' the PRBS below ; instead of it being always '1' ; SEEDING JUST MEANS GIVING ; AN INITIAL VALUE ; OFTEN THIS IS BASED ON A ; CLOCK VARIABLE TO GIVE A BIT MORE ; RANDOMNESS INSTEAD OF CALL PRBG ; RUN THE PRBS CLRF DICE ; CLEAR FILE ;CALL DELAY ; OFTEN IT IS USEFUL ; TO 'DELAY' FOR A FEW MILLISECONDS ; WHEN SWITCHES ARE BEING 'USED' ; NOT USED/CALLED IN SIMULATOR POLA0 BTFSC PORTA,0 ; SIMPLE WAY TO MAKE SURE PORT A GOTO POLA0 ; BIT 0 SWITCH 'RELEASED' POLA1 BTFSC PORTA,1 ; SIMPLE WAY TO MAKE SURE PORT A GOTO POLA1 ; BIT 1 SWITCH 'RELEASED' ; YOU COULD USE THE ROADWORKS SWITCES ; TO TEST THIS PROGRAM GOTO MAIN ;=============================================================== ;SUBROUTINE TO GENERATE RANDOM NUMBERS PRBG MOVF SEED,F ; JUST CHECK THAT SEED IS NOT ZERO! BTFSC STATUS,Z ; INCF SEED ; If it is increment to '1' MOVF SEED,W ; load value of ' SEED' into Working register PLOOP MOVWF NUMB RLF NUMB,F XORWF NUMB,F RLF NUMB,F RLF NUMB,F MOVWF TEMP RLF TEMP,W ;MOVWF PORTB ; THIS LINE IS NOT EXECUTED NOW BTFSC PORTA,1 ; THE RANDOM NUMBER GENERATION GOTO FIN ; STOPS WHEN Port A1 is pressed GOTO PLOOP FIN RETURN END ;=============================================================== DICE ADDWF PCL,F NOP RETLW B'00000001' ; FACE '1' FOR DICE RETLW B'00000010' RETLW B'00000100' RETLW B'00001000' RETLW B'00010000' RETLW B'00100000' ; FACE '6' FOR DICE END MOVLW 1 ; load value of 1 into Working register MOVWF PORTB ; load this value as data into Port B LOOP2 CALL DELAY BCF STATUS,C ; clear Carry flag RLF PORTB,F ; rotate value of PORTB left by 1 logical place BTFSS PORTB,5 GOTO LOOP2 ; this command is actioned only if PORTB is not yet 0 ; the program jumping back to address LOOP2 LOOP3 CALL DELAY BCF STATUS,C ; clear Carry flag RRF PORTB,F ; rotate value of PORTB left by 1 logical place BTFSS PORTB,0 GOTO LOOP3 ; this command is actioned only if PORTB is not yet 0 ;EXERCISE IS TO DO THE FOLLOWING ;ROTATE THE FILE PORT FROM LEFT TO RIGHT ;UNTIL THE CARRY FLAG IS SET AGAIN ;AND THEN REPEAT EVERYTHING GOTO LOOP2 ; this command is only actioned when PORTB now = 0 ;==================================================================== ;THIS IS A 'SHORT' PRETEND DELAY FOR THE SIMULATOR DELAY MOVLW 2 IN_LP ADDLW H'FF' ; SUBTRACT 1 BTFSS STATUS,Z ; IS RESULT ZERO GOTO IN_LP RETURN ;==================================================================== GENSIX INCF DICE ; increment dice ADDLW H'F9' ; by adding '-7' (F9 is 2's complement) BTFSS STATUS,Z MOVLW MOVWF DICE END
I’m trying to create a program in assembly language for a Microchip PIC microcontroller to simulate a dice game. When PORTA is pressed the dice stops rolling and a number is generated at random each time. Below is the code that I have so far. ; TUTA4.ASM ; NOTE: Uncomment the appropriate section for your target chip below: ; Uncomment the following 3 lines if using a 16F88... LIST P=16F88 __CONFIG H'2007', H'3F72' ; RC mode __CONFIG H'2008', H'3FFC' ; Clock Fail-Safe disabled ; Uncomment the following 2 lines if using a 16F84A... ; LIST P=16F84A ; __CONFIG H'3FF3' ; RC mode ; Uncomment the following 2 lines if using a 16F877A... ; LIST P=16F877A ; __CONFIG H'3F03' ; RC mode #DEFINE PAGE0 BCF STATUS,5 #DEFINE PAGE1 BSF STATUS,5 STATUS EQU H'03' ; name program location 3 as STATUS TRISA EQU H'85' ; PORTA EQU H'05' ; PCL EQU H'02' ; PORTB EQU H'06' ; name program location 6 as PORTB TRISB EQU H'86' ; name program location H'86' as TRISB ANSEL EQU H'9B' ; name program location H'9B' as ANSEL W EQU 0 ; Working register flag F EQU 1 ; File register flag C EQU 0 ; Carry flag Z EQU 2 NUMB EQU H'20' TEMP EQU H'21' SEED EQU H'22' ORG 0 ; Reset vector GOTO 5 ; goto start of program ORG 4 ; Interrupt vector GOTO 5 ; goto start of program ORG 5 ; Start of program memory CLRF PORTB ; set all Port B pins to logic 0 CLRF PORTA PAGE1 ; instruct program that Page 1 commands come next CLRF TRISB ; set all Port B pins as outputs from within Page 1 ; PORT A IS DEFAULTED AS INPUTS WHICH IS FINE FOR THIS PROGRAM PAGE0 ; instruct program that Page 0 commands come next MAIN INCF SEED ; SEE BELOW BTFSS PORTA,0 ; START RUNNING WHEN PA0 is pressed ; INC a 'seeding' variable which simply GOTO MAIN ; 'runs constantly' ; This then 'seeds' the PRBS below ; instead of it being always '1' ; SEEDING JUST MEANS GIVING ; AN INITIAL VALUE ; OFTEN THIS IS BASED ON A ; CLOCK VARIABLE TO GIVE A BIT MORE ; RANDOMNESS INSTEAD OF CALL PRBG ; RUN THE PRBS CLRF DICE ; CLEAR FILE ;CALL DELAY ; OFTEN IT IS USEFUL ; TO 'DELAY' FOR A FEW MILLISECONDS ; WHEN SWITCHES ARE BEING 'USED' ; NOT USED/CALLED IN SIMULATOR POLA0 BTFSC PORTA,0 ; SIMPLE WAY TO MAKE SURE PORT A GOTO POLA0 ; BIT 0 SWITCH 'RELEASED' POLA1 BTFSC PORTA,1 ; SIMPLE WAY TO MAKE SURE PORT A GOTO POLA1 ; BIT 1 SWITCH 'RELEASED' ; YOU COULD USE THE ROADWORKS SWITCES ; TO TEST THIS PROGRAM GOTO MAIN ;=============================================================== ;SUBROUTINE TO GENERATE RANDOM NUMBERS PRBG MOVF SEED,F ; JUST CHECK THAT SEED IS NOT ZERO! BTFSC STATUS,Z ; INCF SEED ; If it is increment to '1' MOVF SEED,W ; load value of ' SEED' into Working register PLOOP MOVWF NUMB RLF NUMB,F XORWF NUMB,F RLF NUMB,F RLF NUMB,F MOVWF TEMP RLF TEMP,W ;MOVWF PORTB ; THIS LINE IS NOT EXECUTED NOW BTFSC PORTA,1 ; THE RANDOM NUMBER GENERATION GOTO FIN ; STOPS WHEN Port A1 is pressed GOTO PLOOP FIN RETURN END ;=============================================================== DICE ADDWF PCL,F NOP RETLW B'00000001' ; FACE '1' FOR DICE RETLW B'00000010' RETLW B'00000100' RETLW B'00001000' RETLW B'00010000' RETLW B'00100000' ; FACE '6' FOR DICE END MOVLW 1 ; load value of 1 into Working register MOVWF PORTB ; load this value as data into Port B LOOP2 CALL DELAY BCF STATUS,C ; clear Carry flag RLF PORTB,F ; rotate value of PORTB left by 1 logical place BTFSS PORTB,5 GOTO LOOP2 ; this command is actioned only if PORTB is not yet 0 ; the program jumping back to address LOOP2 LOOP3 CALL DELAY BCF STATUS,C ; clear Carry flag RRF PORTB,F ; rotate value of PORTB left by 1 logical place BTFSS PORTB,0 GOTO LOOP3 ; this command is actioned only if PORTB is not yet 0 ;EXERCISE IS TO DO THE FOLLOWING ;ROTATE THE FILE PORT FROM LEFT TO RIGHT ;UNTIL THE CARRY FLAG IS SET AGAIN ;AND THEN REPEAT EVERYTHING GOTO LOOP2 ; this command is only actioned when PORTB now = 0 ;==================================================================== ;THIS IS A 'SHORT' PRETEND DELAY FOR THE SIMULATOR DELAY MOVLW 2 IN_LP ADDLW H'FF' ; SUBTRACT 1 BTFSS STATUS,Z ; IS RESULT ZERO GOTO IN_LP RETURN ;==================================================================== GENSIX INCF DICE ; increment dice ADDLW H'F9' ; by adding '-7' (F9 is 2's complement) BTFSS STATUS,Z MOVLW MOVWF DICE END
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Topic Video
Question
I’m trying to create a program in assembly language for a Microchip PIC microcontroller to simulate a dice game. When PORTA is pressed the dice stops rolling and a number is generated at random each time. Below is the code that I have so far.
; TUTA4.ASM
; NOTE: Uncomment the appropriate section for your target chip below:
; Uncomment the following 3 lines if using a 16F88...
LIST P=16F88
__CONFIG H'2007', H'3F72' ; RC mode
__CONFIG H'2008', H'3FFC' ; Clock Fail-Safe disabled
; Uncomment the following 2 lines if using a 16F84A...
; LIST P=16F84A
; __CONFIG H'3FF3' ; RC mode
; Uncomment the following 2 lines if using a 16F877A...
; LIST P=16F877A
; __CONFIG H'3F03' ; RC mode
#DEFINE PAGE0 BCF STATUS,5
#DEFINE PAGE1 BSF STATUS,5
STATUS EQU H'03' ; name program location 3 as STATUS
TRISA EQU H'85' ;
PORTA EQU H'05' ;
PCL EQU H'02' ;
PORTB EQU H'06' ; name program location 6 as PORTB
TRISB EQU H'86' ; name program location H'86' as TRISB
ANSEL EQU H'9B' ; name program location H'9B' as ANSEL
W EQU 0 ; Working register flag
F EQU 1 ; File register flag
C EQU 0 ; Carry flag
Z EQU 2
NUMB EQU H'20'
TEMP EQU H'21'
SEED EQU H'22'
ORG 0 ; Reset vector
GOTO 5 ; goto start of program
ORG 4 ; Interrupt vector
GOTO 5 ; goto start of program
ORG 5 ; Start of program memory
CLRF PORTB ; set all Port B pins to logic 0
CLRF PORTA
PAGE1 ; instruct program that Page 1 commands come next
CLRF TRISB ; set all Port B pins as outputs from within Page 1
; PORT A IS DEFAULTED AS INPUTS WHICH IS FINE FOR THIS PROGRAM
PAGE0 ; instruct program that Page 0 commands come next
MAIN INCF SEED ; SEE BELOW
BTFSS PORTA,0 ; START RUNNING WHEN PA0 is pressed
; INC a 'seeding' variable which simply
GOTO MAIN ; 'runs constantly'
; This then 'seeds' the PRBS below
; instead of it being always '1'
; SEEDING JUST MEANS GIVING
; AN INITIAL VALUE
; OFTEN THIS IS BASED ON A
; CLOCK VARIABLE TO GIVE A BIT MORE
; RANDOMNESS INSTEAD OF
CALL PRBG ; RUN THE PRBS
CLRF DICE ; CLEAR FILE
;CALL DELAY ; OFTEN IT IS USEFUL
; TO 'DELAY' FOR A FEW MILLISECONDS
; WHEN SWITCHES ARE BEING 'USED'
; NOT USED/CALLED IN SIMULATOR
POLA0 BTFSC PORTA,0 ; SIMPLE WAY TO MAKE SURE PORT A
GOTO POLA0 ; BIT 0 SWITCH 'RELEASED'
POLA1 BTFSC PORTA,1 ; SIMPLE WAY TO MAKE SURE PORT A
GOTO POLA1 ; BIT 1 SWITCH 'RELEASED'
; YOU COULD USE THE ROADWORKS SWITCES
; TO TEST THIS PROGRAM
GOTO MAIN
;===============================================================
;SUBROUTINE TO GENERATE RANDOM NUMBERS
PRBG MOVF SEED,F ; JUST CHECK THAT SEED IS NOT ZERO!
BTFSC STATUS,Z ;
INCF SEED ; If it is increment to '1'
MOVF SEED,W ; load value of ' SEED' into Working register
PLOOP MOVWF NUMB
RLF NUMB,F
XORWF NUMB,F
RLF NUMB,F
RLF NUMB,F
MOVWF TEMP
RLF TEMP,W
;MOVWF PORTB ; THIS LINE IS NOT EXECUTED NOW
BTFSC PORTA,1 ; THE RANDOM NUMBER GENERATION
GOTO FIN ; STOPS WHEN Port A1 is pressed
GOTO PLOOP
FIN RETURN
END
;===============================================================
DICE ADDWF PCL,F
NOP
RETLW B'00000001' ; FACE '1' FOR DICE
RETLW B'00000010'
RETLW B'00000100'
RETLW B'00001000'
RETLW B'00010000'
RETLW B'00100000' ; FACE '6' FOR DICE
END
MOVLW 1 ; load value of 1 into Working register
MOVWF PORTB ; load this value as data into Port B
LOOP2 CALL DELAY
BCF STATUS,C ; clear Carry flag
RLF PORTB,F ; rotate value of PORTB left by 1 logical place
BTFSS PORTB,5
GOTO LOOP2 ; this command is actioned only if PORTB is not yet 0
; the program jumping back to address LOOP2
LOOP3 CALL DELAY
BCF STATUS,C ; clear Carry flag
RRF PORTB,F ; rotate value of PORTB left by 1 logical place
BTFSS PORTB,0
GOTO LOOP3 ; this command is actioned only if PORTB is not yet 0
;EXERCISE IS TO DO THE FOLLOWING
;ROTATE THE FILE PORT FROM LEFT TO RIGHT
;UNTIL THE CARRY FLAG IS SET AGAIN
;AND THEN REPEAT EVERYTHING
GOTO LOOP2 ; this command is only actioned when PORTB now = 0
;====================================================================
;THIS IS A 'SHORT' PRETEND DELAY FOR THE SIMULATOR
DELAY MOVLW 2
IN_LP ADDLW H'FF' ; SUBTRACT 1
BTFSS STATUS,Z ; IS RESULT ZERO
GOTO IN_LP
RETURN
;====================================================================
GENSIX INCF DICE ; increment dice
ADDLW H'F9' ; by adding '-7' (F9 is 2's complement)
BTFSS STATUS,Z
MOVLW
MOVWF DICE
END ; final statement
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education