Write a PIC assembly language program that produces the GCD of two given numbers, Vall and Val2, each is an 8-bit unsigned value. The values of Val and Val2 will be hardcoded into the program and the result will appear in an 8-bit data value referred to by the label 'gcd'.
For your software development purposes you can use the 15 and 10 decimal for the values Vall and Val2, respectively. The program will be simulated and presented to the course instructor who may ask you to use different values for Vall and Val2, and will verify that your program works correctly.
The following is a flowchart for producing the GCD of two such values. The % operator produces the remainder from dividing [Ux] by [Vx]. To produce the remainder repeatedly subtract [Vx] from [Ux] but stop before the difference becomes negative.
here is the flow chart
code
+------------------+
| main |
+------------------+
|
A well-documented assembly language program that behaves as described. Note that well documented expects liberal use of comments where each line indicates what you are doing. The source code will be included in your write up as an appendix.
A flowchart that describes the algorithm your code uses to produce the remainder. You will include the given GCD flowchart and your remainder flowchart in your write up. Include a table that lists the initial values for [Ux] and [Vx] as well as after the lowest placed process box in the GCD flowchart executes.
A two-page write up that introduces the program and outlines the approach you took. At the top of the first page have the course and project title as well as your name and date that you completed the report. (continued)
The report (PDF) and source code will be submitted to Blackboard so that it may be run. Submit the source code to Blackboard as XX_proj1.asm and the report as XX_proj1.pdf where XX are your for PIC18FXX2 microcontoller
Assistant
Certainly! Below is a well-documented PIC18FXX2 assembly language program that calculates the Greatest Common Divisor (GCD) of two 8-bit unsigned numbers, Val1 and Val2. In this example, Val1 is initialized to 15 and Val2 is initialized to 10. The result of the GCD calculation is stored in an 8-bit data location labeled gcd.
This program follows the Euclidean algorithm as outlined in your provided flowchart. It repeatedly subtracts the smaller number from the larger number until the remainder is zero. The last non-zero remainder is the GCD of the two numbers.
assembly
; GCD Program for PIC18FXX2
; Calculates the GCD of two 8-bit unsigned numbers Val1 and Val2
; Val1 = 15, Val2 = 10
; Result stored in 'gcd'
list p=18F4520 ; Specify the PIC18F device (adjust as needed)
#include <p18f4520.inc> ; Include device-specific definitions
__CONFIG _CONFIG1, _OSC_HS_ & _WDT_OFF & _PWRTE_ON & _BODEN_OFF & _LVP_OFF
__CONFIG _CONFIG2, _WRT_OFF
; Define variables in RAM starting at address 0x20
cblock 0x20 ; Start of general-purpose registers
Ux ; 0x20: Register to hold Ux
Vx ; 0x21: Register to hold Vx
temp ; 0x22: Register to hold temporary value
endc
; Define constants used in the program
VAL1 EQU 15 ; Define Val1 constant with value 15
VAL2 EQU 10 ; Define Val2 constant with value 10
GCD EQU 0x25 ; Define GCD storage address in RAM (adjust as needed)
; Reset vector
org 0x00 ; Origin at address 0x00
goto main ; Jump to main routine
; Interrupt vector (not used)
org 0x08 ; Interrupt vector address
goto $ ; Infinite loop (No interrupt handling)
main:
; Initialize Ux and Vx with Val1 and Val2
movlw VAL1 ; Load literal value of Val1 into W
movwf Ux ; Move W to Ux (Ux = Val1)
movlw VAL2 ; Load literal value of Val2 into W
movwf Vx ; Move W to Vx (Vx = Val2)
GCD_Loop:
; Check if Vx is zero
movf Vx, W ; Move Vx to W
btfsc STATUS, Z ; Test the Zero flag in STATUS
goto Store_GCD ; If Vx == 0, branch to Store_GCD
; Else, continue to compute Ux % Vx
; Compute Ux % Vx by repeated subtraction
movf Ux, W ; Move Ux to W
movwf temp ; Store W into temp (temp = Ux)
Mod_Subtract_Loop:
; Subtract Vx from temp and check if temp >= Vx
movf temp, W ; Move temp to W
subwf Vx, 0 ; Subtract Vx from W (W = temp - Vx)
btfss STATUS, C ; Check if temp >= Vx (Carry flag set)
goto After_Mod ; If temp < Vx, exit loop
; Else, temp >= Vx, subtract Vx from temp
movf temp, W ; Move temp to W
subwf Vx, 0 ; Subtract Vx from W (W = temp - Vx)
movwf temp ; Store result back to temp (temp = temp - Vx)
goto Mod_Subtract_Loop ; Repeat loop if necessary
After_Mod:
; Update Ux with the value of Vx
movf Vx, W ; Move Vx to W
movwf Ux ; Update Ux with Vx (Ux = Vx)
; Update Vx with the value of temp (Vx = Ux % Vx)
movf temp, W ; Move temp to W
movwf Vx ; Update Vx with temp (Vx = temp)
goto GCD_Loop ; Repeat the GCD loop
Store_GCD:
; Store the computed GCD value from Ux into the gcd memory location
movf Ux, W ; Move Ux to W
movwf GCD ; Store W into gcd (gcd = Ux)
Done:
; End of program - enter infinite loop
goto Done ; Stay here forever
end ; End of assembly file
Explanation of the Program:
Initialization:
The program begins by initializing the variables Ux and Vx with the values Val1 and Val2 respectively.
GCD Calculation Loop (GCD_Loop):
The program checks if Vx is zero. If it is, the value of Ux is the GCD, and the program proceeds to store this value.
If Vx is not zero, the program computes the remainder of Ux divided by Vx by repeatedly subtracting Vx from Ux until the result is less than Vx. This is stored in temp.
Ux is then updated to the value of Vx, and Vx is updated to the computed remainder (temp).
The loop repeats until Vx becomes zero.
Storing the GCD (Store_GCD):
Once Vx is zero, the current value of Ux is the GCD and is stored in the memory location labeled gcd.
End of Program (Done):
The program enters an infinite loop after storing the GCD to prevent it from running indefinitely or executing unintended code.
Notes:
Register Allocation:
Ux, Vx, and temp are allocated in general-purpose RAM starting at address 0x20. Adjust these addresses if necessary based on your specific microcontroller's memory map.
Configuration Bits:
The __CONFIG directives set up the microcontroller's configuration bits. Ensure these are set appropriately for your hardware setup. The provided configuration is a common setup for many applications but may need adjustments.
GCD Storage Location:
The GCD is stored at memory location 0x25. Make sure this location does not overlap with other used memory regions in your specific application.
Interrupts:
Interrupts are not handled in this program. The interrupt vector simply leads to an infinite loop. If your application requires interrupt handling, additional code will be necessary.
Adjustments:
Replace p=18F4520 and the included header file with the specific PIC18FXX2 variant you are using.
Ensure that the gcd memory location (0x25) does not conflict with other parts of your program or memory usage.
Simulation and Testing:
Before deploying this program to actual hardware, simulate it using a PIC simulator like MPLAB X to verify its correctness.
To test with different initial values for Val1 and Val2, modify the VAL1 and VAL2 constants accordingly and reassemble the program.
This program adheres to the requirements outlined in your submission expectations, including thorough documentation and clear labeling of each step in the algorithm.