Certainly! Below is a well-documented PIC assembly language program that calculates the Greatest Common Divisor (GCD) of two hardcoded 8-bit unsigned values, Val1 and Val2. In this example, Val1 is initialized to 15 and Val2 to 10. The result is stored in an 8-bit data label referred to as gcd. The program follows the Euclidean algorithm as described in your flowchart, utilizing repeated subtraction to determine the remainder.
PIC Assembly Language Program for GCD Calculation
; *******************************************************
; Project: GCD Calculation Using PIC Assembly Language
; Course: [Your Course Name]
; Author: [Your Name]
; Date: [Date Completed]
; Description: This program computes the Greatest Common Divisor (GCD)
; of two hardcoded 8-bit unsigned integers using the
; Euclidean algorithm. The GCD result is stored in the
; 'gcd' memory location.
; *******************************************************
; Define processor and configuration
; (Adjust the processor and configuration bits as per your specific PIC)
LIST P=16F877A ; Example processor (PIC16F877A)
INCLUDE "P16F877A.inc" ; Include device-specific definitions
; Configuration bits
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
; Define constants
VAL1 EQU 0x20 ; Address for Val1
VAL2 EQU 0x21 ; Address for Val2
GCD EQU 0x22 ; Address to store the GCD result
TEMP EQU 0x23 ; Temporary storage
COUNTER EQU 0x24 ; Loop counter for subtraction
; Initialize data
ORG 0x00 ; Origin, starting address
GOTO MAIN ; Jump to main program
; Initialize program memory (optional)
; Add any initialization routines here
; Main Program
MAIN:
; Initialize Val1 and Val2 with hardcoded values
MOVLW 0x0F ; Load WREG with decimal 15
MOVWF VAL1 ; Store Val1 = 15
MOVLW 0x0A ; Load WREG with decimal 10
MOVWF VAL2 ; Store Val2 = 10
; Start of GCD Algorithm
GCD_LOOP:
MOVF VAL2, W ; Move Vx into WREG
CPFSLT 0x00, VAL2 ; Compare Vx with 0
; Alternatively, use a branch instruction if VAL2 == 0
; Check if VAL2 == 0
MOVF VAL2, W ; Move Vx into WREG
CPFSEQ W, #0x00 ; Compare WREG to 0
BTFSS STATUS, Z ; If zero flag is set, skip next instruction
GOTO CALCULATE_REMAINDER ; If Vx != 0, calculate remainder
; If Vx == 0, GCD is Ux
MOVF VAL1, W ; Move Ux into WREG
MOVWF GCD ; Store GCD = Ux
GOTO DONE ; Exit the program
CALCULATE_REMAINDER:
; Initialize temporary variables
MOVF VAL1, W ; Load Ux into WREG
MOVWF TEMP ; TEMP = Ux
MOVF VAL2, W ; Load Vx into WREG
MOVWF COUNTER ; COUNTER = Vx
SUBTRACT_LOOP:
; Subtract Vx from TEMP and check if result is negative
MOVF TEMP, W ; Load TEMP into WREG
SUBWF COUNTER, W ; WREG = TEMP - Vx
BTFSS STATUS, C ; Check if subtraction resulted in carry (no borrow)
GOTO END_SUBTRACT ; If TEMP < Vx, exit loop
MOVF W, W ; Move result back to TEMP
MOVWF TEMP ; TEMP = TEMP - Vx
GOTO SUBTRACT_LOOP ; Repeat subtraction
END_SUBTRACT:
; The remainder is in TEMP
MOVF VAL2, W ; Move Vx into WREG
MOVWF VAL1 ; Set Ux = Vx
MOVF TEMP, W ; Move remainder into WREG
MOVWF VAL2 ; Set Vx = remainder
GOTO GCD_LOOP ; Repeat the GCD loop
DONE:
; Infinite loop to end the program
END_INF_LOOP:
GOTO END_INF_LOOP
END ; End of program
Explanation of the Program
-
Initialization:
- The program starts by defining the processor and including the necessary device-specific definitions.
- It sets up configuration bits such as turning off code protection (
_CP_OFF), watchdog timer (_WDT_OFF), enabling power-up timer (_PWRTE_ON), and selecting an external oscillator (_XT_OSC).
- Constants are defined for
Val1, Val2, gcd, temp, and counter using specific memory addresses.
-
Main Program (MAIN label):
Val1 and Val2 are initialized with the hardcoded values 15 and 10 respectively.
-
GCD Calculation Loop (GCD_LOOP label):
- The program checks if
Vx (Val2) is zero.
- If
Vx is zero:
- The GCD is
Ux (Val1), and it is stored in the gcd memory location.
- The program then enters an infinite loop (
DONE) to terminate.
- If
Vx is not zero:
- The program proceeds to calculate the remainder of
Ux divided by Vx using repeated subtraction.
-
Calculating the Remainder (CALCULATE_REMAINDER label):
TEMP is initialized with the value of Ux.
COUNTER is set to the value of Vx.
- A loop (
SUBTRACT_LOOP) repeatedly subtracts Vx from TEMP until TEMP is less than Vx. The result stored in TEMP is the remainder.
-
Updating Values for Next Iteration:
- After calculating the remainder,
Ux is updated to the value of Vx, and Vx is updated to the remainder stored in TEMP.
- The program then loops back to check if the new
Vx is zero, continuing the GCD calculation.
Notes:
Additional Components for Submission
While the assembly code is provided above, your submission should also include the following:
-
Flowcharts:
- Given GCD Flowchart: Include the flowchart you provided in your write-up.
- Remainder Calculation Flowchart: Create a flowchart that specifically details the repeated subtraction method used to calculate the remainder (
Ux % Vx).
-
Data Tables:
- Initial and Intermediate Values:
| Step | Ux (Val1) | Vx (Val2) | Temp | Counter |
|---|
| Initialization | 15 | 10 | 0 | 0 |
| After First Subtraction | 10 | 5 | 0 | 0 |
| After Second Subtraction | 5 | 0 | 0 | 0 |
| Final GCD | 5 | 0 | 0 | 0 |
-
Two-Page Write-Up:
- Page 1:
- Header: Include course name, project title, your name, and completion date.
- Introduction: Briefly describe the purpose of the program and the algorithm used.
- Page 2:
- Approach: Explain how you implemented the Euclidean algorithm in PIC assembly, including any challenges faced and how you addressed them.
- Conclusion: Summarize the effectiveness of your program and any potential improvements.
-
Submission Files:
- Source Code: Save the assembly code as
XX_proj1.asm (replace XX with your identifier).
- Report: Compile your write-up and flowcharts into a PDF named
XX_proj1.pdf.
- Submission: Upload both files to Blackboard as per the submission guidelines.
By following the above guidelines and utilizing the provided assembly code, you should be well-prepared to simulate and demonstrate the GCD calculation program to your course instructor. Should you need any further assistance or clarification, feel free to ask!